Portal | Manuals | References | Downloads | Info | Programs | JCLs | Master the Mainframes
IBM Mainframe Computers Forums Index
 
Register
 
IBM Mainframe Computers Forums Index Mainframe: Search IBM Mainframe Forum: FAQ Memberlist Usergroups Profile Log in to check your private messages Log in
 

 

Extracting Year and Month value from a Date

 
Post new topic   Reply to topic    IBMMAINFRAMES.com Support Forums -> DB2
View previous topic :: :: View next topic  
Author Message
ksk

Active User


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

PostPosted: Fri Nov 07, 2008 2:43 pm    Post subject: Extracting Year and Month value from a Date
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    Post subject:
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: 356
Location: New York

PostPosted: Fri Nov 07, 2008 5:31 pm    Post subject:
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    Post subject:
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

Senior Member


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

PostPosted: Sat Nov 08, 2008 1:41 am    Post subject:
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.

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

Senior Member


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

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

Hi,

One other link:

http://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: 356
Location: New York

PostPosted: Mon Nov 10, 2008 12:22 pm    Post subject:
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    Post subject: Reply to: Extracting Year and Month value from a Date
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    IBMMAINFRAMES.com Support Forums -> DB2 All times are GMT + 6 Hours
Page 1 of 1

 

Search our Forum:

Similar Topics
Topic Author Forum Replies Posted
No new posts Validate date and numeric fields and ... Rick Silvers DFSORT/ICETOOL 6 Thu May 11, 2017 6:51 pm
No new posts LISTIDR compiled date/time jerryte IBM Tools 3 Thu Apr 20, 2017 7:37 pm
No new posts SUBSTR for Date functions Shaheen Shaik DB2 4 Thu Apr 06, 2017 2:14 pm
No new posts Date and time format in CICS Chandru3183 CICS 2 Sat Mar 18, 2017 12:46 pm
No new posts SORT to append Month and YEAR in MMYY... tisamf DFSORT/ICETOOL 1 Wed Mar 08, 2017 4:46 pm


Facebook
Back to Top
 
Mainframe Wiki | Forum Rules | Bookmarks | Subscriptions | FAQ | Tutorials | Contact Us