Home » SQL & PL/SQL » SQL & PL/SQL » To get start and end date of month
To get start and end date of month [message #639343] Mon, 06 July 2015 07:08 Go to next message
rohit_shinez
Messages: 139
Registered: January 2015
Senior Member
Hi,

I need to take the start and end date of month by referring to ref_table

TYPE	         CODE	            VALUE
MISSING_RECORD	AUG_2010	AUGUST 2010



CREATE TABLE "REF_TABLE"
  (
  "TYPE"  VARCHAR2(20 BYTE) NOT NULL ENABLE,
  "CODE"  VARCHAR2(20 BYTE) NOT NULL ENABLE,
  "VALUE" VARCHAR2(100 BYTE)
  );
 
Insert into Ref_table (TYPE,CODE,VALUE) values ('MISSING_RECORD','AUG_2010','AUGUST 2010');


I Need to derive first date and last date from ref_table where value = AUGUST 2010

output
01-AUG-2010 31-AUG-2010
Re: To get start and end date of month [message #639345 is a reply to message #639343] Mon, 06 July 2015 07:17 Go to previous messageGo to next message
John Watson
Messages: 8931
Registered: January 2010
Location: Global Village
Senior Member
I would use TO_DATE with an appropriate format string to convert either CODE or VALUE into DATE, then LAST_DAY to derive the month end date.
Re: To get start and end date of month [message #639346 is a reply to message #639345] Mon, 06 July 2015 07:19 Go to previous messageGo to next message
rohit_shinez
Messages: 139
Registered: January 2015
Senior Member
I tried with but getting an error

SELECT TRUNC(VALUE , 'Month'),LAST_DAY(TRUNC(VALUE , 'Month')) MONTH FROM 
ref_table where code =  'MISSING_RECORD';
Re: To get start and end date of month [message #639347 is a reply to message #639346] Mon, 06 July 2015 07:31 Go to previous messageGo to next message
gazzag
Messages: 1118
Registered: November 2010
Location: Bedwas, UK
Senior Member
Care to show us this error?
Re: To get start and end date of month [message #639348 is a reply to message #639347] Mon, 06 July 2015 07:31 Go to previous messageGo to next message
rohit_shinez
Messages: 139
Registered: January 2015
Senior Member

SELECT TRUNC(VALUE , 'Month'),LAST_DAY(TRUNC(VALUE , 'Month')) MONTH FROM 
ref_table where code =  'MISSING_RECORD';

ORA-00932: inconsistent datatypes: expected DATE got NUMBER
00932. 00000 -  "inconsistent datatypes: expected %s got %s"
*Cause:    
*Action:
Error at Line: 15 Column: 47
Re: To get start and end date of month [message #639349 is a reply to message #639348] Mon, 06 July 2015 07:34 Go to previous messageGo to next message
pablolee
Messages: 2882
Registered: May 2007
Location: Scotland
Senior Member
value is not a date column, is that right? As John suggested, you need to use to_date.
Re: To get start and end date of month [message #639350 is a reply to message #639349] Mon, 06 July 2015 07:36 Go to previous messageGo to next message
rohit_shinez
Messages: 139
Registered: January 2015
Senior Member
even with to_date i am not able to achieve because my value is 'AUGUST 2010' which is a varchar value and need to extract the date in order to avoid the data typ error, can some one help
Re: To get start and end date of month [message #639351 is a reply to message #639346] Mon, 06 July 2015 07:38 Go to previous messageGo to next message
Solomon Yakobson
Messages: 3273
Registered: January 2010
Location: Connecticut, USA
Senior Member
Obviously you are getting errors. You are storing date in VARCHAR2 column - bad design. Change column VALUE type to DATE. Otherwise:

SELECT  TO_CHAR(TRUNC(TO_DATE(VALUE,'Month YYYY'),'mm'),'fmMonth DD, YYYY'),
        TO_CHAR(LAST_DAY(TRUNC(TO_DATE(VALUE,'Month YYYY'),'mm')),'fmMonth DD, YYYY')
  FROM  ref_table
  WHERE code =  'MISSING_RECORD';


SY.
Re: To get start and end date of month [message #639352 is a reply to message #639350] Mon, 06 July 2015 07:40 Go to previous messageGo to next message
gazzag
Messages: 1118
Registered: November 2010
Location: Bedwas, UK
Senior Member
Oracle date format models.
Re: To get start and end date of month [message #639355 is a reply to message #639351] Mon, 06 July 2015 07:45 Go to previous messageGo to next message
rohit_shinez
Messages: 139
Registered: January 2015
Senior Member
can the above be used to compare the dates from ref_table

select 1 from Main_table
where col_1 between '01-AUG-2010' and '31-AUG-2010'

using the below values

SELECT  TRUNC(TO_DATE(VALUE,'Month YYYY'),'mm'),
        LAST_DAY(TRUNC(TO_DATE(VALUE,'Month YYYY'),'mm'))  FROM 
ref_table where type =  'MISSING_RECORD';

[Updated on: Mon, 06 July 2015 07:46]

Report message to a moderator

Re: To get start and end date of month [message #639361 is a reply to message #639355] Mon, 06 July 2015 07:59 Go to previous messageGo to next message
Lalit Kumar B
Messages: 3174
Registered: May 2013
Location: World Wide on the Web
Senior Member
Once again, '01-AUG-2010' and '31-AUG-2010' are not DATEs, they are strings.

Use ANSI DATE lieral, DATE 'YYYY-MM-DD' or TO_DATE with proper format model.

[Updated on: Mon, 06 July 2015 08:00]

Report message to a moderator

Re: To get start and end date of month [message #639365 is a reply to message #639361] Mon, 06 July 2015 08:15 Go to previous messageGo to next message
rohit_shinez
Messages: 139
Registered: January 2015
Senior Member
yeah i agree with you i wanted to compare with output from below query from the query

select 1 from Main_table
where col_1 not between values from second query




SELECT  TRUNC(TO_DATE(VALUE,'Month YYYY'),'mm'),
        LAST_DAY(TRUNC(TO_DATE(VALUE,'Month YYYY'),'mm'))  FROM 
ref_table where type =  'MISSING_RECORD';


[Updated on: Mon, 06 July 2015 09:29]

Report message to a moderator

Re: To get start and end date of month [message #639371 is a reply to message #639365] Mon, 06 July 2015 09:13 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
Is col_1 a date column?
Re: To get start and end date of month [message #639375 is a reply to message #639371] Mon, 06 July 2015 09:30 Go to previous messageGo to next message
rohit_shinez
Messages: 139
Registered: January 2015
Senior Member
Yes its a date column and i wanted to exclude sorry for previous post i need to ignore if col_1 falls between '01-AUG-2010' and '31-AUG-2010'
Re: To get start and end date of month [message #639376 is a reply to message #639375] Mon, 06 July 2015 09:47 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
>select 1 from Main_table where col_1 not between values from second query

what is the desired/expected results from query above?
Re: To get start and end date of month [message #639381 is a reply to message #639376] Mon, 06 July 2015 10:05 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
Easily done, include ref_table in from clause, add ref_table restriction to where clause, make col_1 between the 2 expressions from the other select.
Re: To get start and end date of month [message #639383 is a reply to message #639376] Mon, 06 July 2015 10:06 Go to previous messageGo to next message
rohit_shinez
Messages: 139
Registered: January 2015
Senior Member
i would like to exclude the records from main_table

CREATE TABLE "MAIN_TABLE"
  (
  "INT_AMT"           NUMBER(12,2),
  "CHARGE_END_DATE"   DATE,
  "NO_OF_PARTIES"     NUMBER(3,0),
  "CHARGE_START_DATE" DATE
  );
 
Insert into MAIN_TABLE (INT_AMT,CHARGE_END_DATE,NO_OF_PARTIES,CHARGE_START_DATE) values (0,to_date('30/09/2005','DD/MM/YYYY'),2,to_date('31/10/2005','DD/MM/YYYY'));
Insert into MAIN_TABLE(INT_AMT,CHARGE_END_DATE,NO_OF_PARTIES,CHARGE_START_DATE) values (0,to_date('01/12/2005','DD/MM/YYYY'),2,to_date('29/12/2005','DD/MM/YYYY'));
Insert into MAIN_TABLE (INT_AMT,CHARGE_END_DATE,NO_OF_PARTIES,CHARGE_START_DATE) values (0,to_date('01/11/2005','DD/MM/YYYY'),2,to_date('30/11/2005','DD/MM/YYYY'));



i need to exclude those records where charge_start_date falling between '01-AUG-2010' and '31-AUG-2010'

[Updated on: Mon, 06 July 2015 10:07]

Report message to a moderator

Re: To get start and end date of month [message #639384 is a reply to message #639383] Mon, 06 July 2015 10:13 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
So do as I suggested above
Re: To get start and end date of month [message #639385 is a reply to message #639384] Mon, 06 July 2015 10:22 Go to previous messageGo to next message
rohit_shinez
Messages: 139
Registered: January 2015
Senior Member
something like below where i am struck actually


select * from summary sum,
(SELECT  TRUNC(TO_DATE(VALUE,'Month YYYY'),'mm') START_DATE,
        LAST_DAY(TRUNC(TO_DATE(VALUE,'Month YYYY'),'mm')) END_DATE  FROM 
Ref_table ref WHERE TYPE =  'MISSING_RECORD') ps
and sum.charge_start_date not between ps.start_date and ps.end_date



i am not getting if there are two records in ref_table like below

Insert into Ref_table (TYPE,CODE,VALUE) values ('MISSING_RECORD','SEP_2010','SEPTEMBER 2010');

Re: To get start and end date of month [message #639386 is a reply to message #639385] Mon, 06 July 2015 10:27 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
Please read and follow the forum guidelines, to enable us to help you:

http://www.orafaq.com/forum/t/88153/0/

please provide us Test Case & show us desired/expected results
Re: To get start and end date of month [message #639388 is a reply to message #639386] Mon, 06 July 2015 10:47 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
So you can have multiple months to be excluded?
Between is no use in that case, simpler to convert the date column to the same format as the data in ref_table and do a simple NOT IN
Re: To get start and end date of month [message #639389 is a reply to message #639388] Mon, 06 July 2015 11:07 Go to previous messageGo to next message
rohit_shinez
Messages: 139
Registered: January 2015
Senior Member
How you can use not in when there are start and end dates i am confused here
Re: To get start and end date of month [message #639390 is a reply to message #639389] Mon, 06 July 2015 11:11 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
You don't have start and end dates, you created them from the actual data because you thought it'd make the problem easier.
Don't use start/end dates just use month/year.
Re: To get start and end date of month [message #639391 is a reply to message #639390] Mon, 06 July 2015 11:15 Go to previous messageGo to next message
rohit_shinez
Messages: 139
Registered: January 2015
Senior Member
so how i can extract using month/year can you guide on this, because i am struck and moreover i cant change the datatype in ref_table which holds other varchar data as well
Re: To get start and end date of month [message #639392 is a reply to message #639391] Mon, 06 July 2015 11:18 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
You don't need to extract month year from ref_table, it holds that already.
You need to extract month year from the date column on main_table using to_char with the correct format mask.
Re: To get start and end date of month [message #639393 is a reply to message #639392] Mon, 06 July 2015 11:22 Go to previous messageGo to next message
rohit_shinez
Messages: 139
Registered: January 2015
Senior Member
Yes i have got it thanks
Re: To get start and end date of month [message #639395 is a reply to message #639393] Mon, 06 July 2015 12:40 Go to previous message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
do #12 of Posting Giodelines
Previous Topic: Oracle SQL Customization
Next Topic: SQL query help
Goto Forum:
  


Current Time: Wed Apr 24 10:24:12 CDT 2024