IBM Mainframe Forum Index
 
Log In
 
IBM Mainframe Forum Index Mainframe: Search IBM Mainframe Forum: FAQ Register
 

Extracting Year and Month value from a Date


IBM Mainframe Forums -> DB2
Post new topic   Reply to topic
View previous topic :: View next topic  
Author Message
ksk

Active User


Joined: 08 Jun 2006
Posts: 355
Location: New York

PostPosted: Fri Nov 07, 2008 2:43 pm
Reply with quote

Hi,

We have a reuqirement to compare date format YYYY-MM with the
YYYY-MM of a given date (given date will be in YYYY-MM-DD format).

I can do reference modification and extract Year and Month thru COBOL-DB2 program and can use it in SQL query. But is there any function to extract both YEAR and MONTH from a particular date in SQL query like YEAR for year part, MONTH for month part?

E.g,

BOOK_DT = '2008-11-01' and YEAR-MO = '2008-11'. I want to select Account number if YEAR-MO is equal to Year and Month part of BOOK_DT. I want to to this thru SQL query.
Back to top
View user's profile Send private message
trilokdewangan

New User


Joined: 19 Jun 2008
Posts: 39
Location: USA

PostPosted: Fri Nov 07, 2008 5:01 pm
Reply with quote

Hi KSK

you can use SUBSTR function. Syntax is SUBSTR(variable name, starting position, length)


Example: BOOK_DT = '2008-11-01'

SUBSTR(BOOK_DT,1,7) = 2008-11
Back to top
View user's profile Send private message
ksk

Active User


Joined: 08 Jun 2006
Posts: 355
Location: New York

PostPosted: Fri Nov 07, 2008 5:31 pm
Reply with quote

Trilok,

Did you execute your solution? I tried in SPUFI before posting this question, it's not working for me.
Back to top
View user's profile Send private message
Suresh Ponnusamy

Active User


Joined: 22 Feb 2008
Posts: 107
Location: New York

PostPosted: Fri Nov 07, 2008 8:40 pm
Reply with quote

Hi

Please try the following

Code:

SELECT ACCT_NO
  FROM TABLE
 WHERE YEAR(BOOK_DT)  = YEAR(YEAR-MO)
   AND MONTH(BOOK_DT) = MONTH(YEAR-MO)
Back to top
View user's profile Send private message
Anuj Dhawan

Superior Member


Joined: 22 Apr 2006
Posts: 6250
Location: Mumbai, India

PostPosted: Sat Nov 08, 2008 1:41 am
Reply with quote

Hi,

Given a date, time, or timestamp, you can extract (where applicable) the year, month, day, hour, minutes, seconds, and microseconds portions independently using the appropriate function:
Code:
YEAR (current timestamp)
MONTH (current timestamp)
DAY (current timestamp)
HOUR (current timestamp)
MINUTE (current timestamp)
SECOND (current timestamp)
MICROSECOND (current timestamp)


You can replace "current timestamp" with the column name of intrest from your table. Here is a related link.

www.ibm.com/developerworks/db2/library/techarticle/0211yip/0211yip3.html
Back to top
View user's profile Send private message
Anuj Dhawan

Superior Member


Joined: 22 Apr 2006
Posts: 6250
Location: Mumbai, India

PostPosted: Sat Nov 08, 2008 1:42 am
Reply with quote

Hi,

One other link:

publib.boulder.ibm.com/infocenter/dzichelp/v2r2/index.jsp?topic=/com.ibm.db29.doc.sqlref/db2z_bif_extract.htm

Chek for embedded links of YEAR, MONTH etc.
Back to top
View user's profile Send private message
ksk

Active User


Joined: 08 Jun 2006
Posts: 355
Location: New York

PostPosted: Mon Nov 10, 2008 12:22 pm
Reply with quote

Anuj,

I have tried with EXTARCT function before posting this question but it's not working for me. And tutorial is showing DB 9.1 version and probably mine is older version.
Back to top
View user's profile Send private message
tarun_bhardwaj

New User


Joined: 18 Jul 2003
Posts: 39
Location: delhi

PostPosted: Mon Nov 10, 2008 3:48 pm
Reply with quote

KSK,

The following query should work for you. I just tried it in SPUFI
Code:

SELECT CONCAT(CONCAT(SUBSTR(CHAR(YEAR(BOOK_DT)),1,4),'-')   
    ,SUBSTR(CHAR(MONTH(BOOK_DT)),1,2))                       
       FROM     BOOK_TABLE;                                 
Back to top
View user's profile Send private message
View previous topic :: :: View next topic  
Post new topic   Reply to topic View Bookmarks
All times are GMT + 6 Hours
Forum Index -> DB2

 


Similar Topics
Topic Forum Replies
No new posts Replacing 'YYMMDD' with date, varying... SYNCSORT 3
No new posts Populate last day of the Month in MMD... SYNCSORT 2
No new posts Extracting Variable decimal numbers f... DFSORT/ICETOOL 17
No new posts Modifying Date Format Using DFSORT DFSORT/ICETOOL 9
No new posts JCL sort card - get first day and las... JCL & VSAM 9
Search our Forums:

Back to Top