If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > ANSI SQL > Last_day and First_Day of the month

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 12-05-03, 10:55
mkasem mkasem is offline
Registered User
 
Join Date: Oct 2002
Location: Hartford, CT
Posts: 103
Last_day and First_Day of the month

Hi,

Can I use Last_day(table_date) and first day? I have the query below and it is not working. If not is there any suggestions? I error is pointing at the "First_day".

SQL> SELECT to_char(to_date(first_day(s.key_date),'YYYYMMDD'), 'MM/DD/YYYY') START_DATE,
2 to_char(to_date(last_day(s.key_date),'YYYYMMDD'),' MM/DD/YYYY') END_DATE
3 FROM schedule s;
SELECT to_char(to_date(first_day(s.key_date),'YYYYMMDD'), 'MM/DD/YYYY') START_DATE,
*
ERROR at line 1:
ORA-00904: invalid column name

Thanks a lot
__________________
Mo
Reply With Quote
  #2 (permalink)  
Old 12-05-03, 11:24
andrewst andrewst is offline
Moderator.
 
Join Date: Sep 2002
Location: UK
Posts: 5,171
Re: Last_day and First_Day of the month

There is an Oracle function called LAST_DAY that returns last day of month for given date, but not FIRST_DAY. For first day of month use TRUNC(s.key_date,'MM')
__________________
Tony Andrews
http://tinyurl.com/tonyandrews
Reply With Quote
  #3 (permalink)  
Old 12-05-03, 11:44
mkasem mkasem is offline
Registered User
 
Join Date: Oct 2002
Location: Hartford, CT
Posts: 103
What about formatting it like this ('MM/DD/YYYY'), remeber the actual table date is in 8 bytes varchar2.

Thanks
__________________
Mo
Reply With Quote
  #4 (permalink)  
Old 12-05-03, 14:12
andrewst andrewst is offline
Moderator.
 
Join Date: Sep 2002
Location: UK
Posts: 5,171
My first reponse to that is: "why?" Why would anyone store dates as VARCHAR2 and not as DATE? Does not compute...

Anyway, you must convert the VARCHAR2 to a DATE before applying any other functions that work with dates:

LAST_DAY( TO_DATE(s.key_date,'MM/DD/YYYY') )

TRUNC( TO_DATE(s.key_date,'MM/DD/YYYY'), 'MM' )
__________________
Tony Andrews
http://tinyurl.com/tonyandrews
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On