MVSFORUMS.com Forum Index MVSFORUMS.com
A Community of and for MVS Professionals
 
 FAQFAQ   SearchSearch   Quick Manuals   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

DB2 queries regarding Date

 
Post new topic   Reply to topic   printer-friendly view    MVSFORUMS.com Forum Index -> Database
View previous topic :: View next topic  
Author Message
tommy123
Beginner


Joined: 04 May 2005
Posts: 21
Topics: 11

PostPosted: Wed May 25, 2005 9:08 pm    Post subject: DB2 queries regarding Date Reply with quote

Hai,

I am trying to work on different querries as below

1.I am using the following query in QMF to know my age :

SELECT (CURRENT DATE - '1976-05-27') "YEAR-MONTH-DAYS" FROM SYSIBM.SYSDUMMY1 and i get the display

YEAR-MONTH-DAYS
---------------
281129


When i try to add 10 days more to my age using this query i am getting error

SELECT (CURRENT DATE - '1976-05-27' + 10 DAYS )
"YEAR-MONTH-DAYS" FROM SYSIBM.SYSDUMMY1


"An arithmetic expression with a datetime value is invalid."


2.I am trying to find the difference in birthdates of two persons by giving this query

SELECT ('1976-05-27' - '1978-1-18') "YEAR-MONTH-DAYS" FROM SYSIBM.SYSDUMMY1

I am getting the error :

"Argument '2' of scalar function '-' is invalid."

3.I am trying to find elder of the two persons by using this query

SELECT MAX('1976-05-27','1978-1-18') FROM SYSIBM.SYSDUMMY1

I am getting the display as

COL1
----------
1978-1-18

where my intention is to get the person who has born earlier ie elder '1976-05-27'

4.I want to know the date ,10 days before my birthday for which i am using the query

SELECT ('1976-05-27' - 10 DAYS) FROM SYSIBM.SYSDUMMY1

I am getting error "Argument '2' of scalar function '-' is invalid."

5.I want to know how much age i am in YYMMDD format if i try to subtract 3 months from my birthday .


Can anyone tell me how to correct my querries / how to code them properly ?
Back to top
View user's profile Send private message
kolusu
Site Admin
Site Admin


Joined: 26 Nov 2002
Posts: 12376
Topics: 75
Location: San Jose

PostPosted: Wed May 25, 2005 11:45 pm    Post subject: Reply with quote

Tommy123,


Quote:

When i try to add 10 days more to my age using this query i am getting error

SELECT (CURRENT DATE - '1976-05-27' + 10 DAYS )
"YEAR-MONTH-DAYS" FROM SYSIBM.SYSDUMMY1


Answer:
Code:

SELECT  CURRENT DATE - (DATE('1976-05-27') + 10 DAYS)   
  FROM SYSIBM.SYSDUMMY1;


Quote:

2.I am trying to find the difference in birthdates of two persons by giving this query

SELECT ('1976-05-27' - '1978-1-18') "YEAR-MONTH-DAYS" FROM SYSIBM.SYSDUMMY1



Answer:
Code:

SELECT  DATE('1976-05-27') - DATE('1978-1-18')         
  FROM SYSIBM.SYSDUMMY1;                               


Quote:

I am trying to find elder of the two persons by using this query where my intention is to get the person who has born earlier ie elder '1976-05-27'


You need MIN not max to find the eldest person.

Code:

SELECT MIN('1976-05-27','1978-1-18')                   
  FROM SYSIBM.SYSDUMMY1;                               


Quote:

I want to know the date ,10 days before my birthday for which i am using the query


Code:

SELECT  DATE('1976-05-27') - 10 DAYS                   
  FROM SYSIBM.SYSDUMMY1;                               


Quote:

5.I want to know how much age i am in YYMMDD format if i try to subtract 3 months from my birthday .


I am not really sure as to what the question is here.

Check this link which explains in detail about datetime arithmetic in SQL

http://publibz.boulder.ibm.com/cgi-bin/bookmgr_OS390/BOOKS/DSNSQH11/2.20.9?SHELF=&DT=20010718164132&CASE=

Hope this helps...

Cheers

Kolusu
_________________
Kolusu
www.linkedin.com/in/kolusu
Back to top
View user's profile Send private message Send e-mail Visit poster's website
NASCAR9
Intermediate


Joined: 08 Oct 2004
Posts: 274
Topics: 52
Location: California

PostPosted: Thu May 26, 2005 9:57 am    Post subject: Reply with quote

Kolusu,
I ran a couple of the above queries and I don't understand the result, can you help?
SELECT CURRENT DATE - (DATE('1976-05-27') + 10 DAYS)
FROM SYSIBM.SYSDUMMY1;
---------+---------+---------+---------+---------+---------+

---------+---------+---------+---------+---------+---------+
281120.
DSNE610I NUMBER OF ROWS DISPLAYED IS 1
DSNE616I STATEMENT EXECUTION WAS SUCCESSFUL, SQLCODE IS 100
---------+---------+---------+---------+---------+---------+

SELECT DATE('1978-05-27') - DATE('1976-1-18')
FROM SYSIBM.SYSDUMMY1;
---------+---------+---------+---------+---------+---------+

---------+---------+---------+---------+---------+---------+
20409.
DSNE610I NUMBER OF ROWS DISPLAYED IS 1
DSNE616I STATEMENT EXECUTION WAS SUCCESSFUL, SQLCODE IS 100
Back to top
View user's profile Send private message
kolusu
Site Admin
Site Admin


Joined: 26 Nov 2002
Posts: 12376
Topics: 75
Location: San Jose

PostPosted: Thu May 26, 2005 10:04 am    Post subject: Reply with quote

Nascar9,

An extract from the link provided in the earlier post.

Subtracting dates: The result of subtracting one date (DATE2) from another (DATE1) is a date duration that specifies the number of years, months, and days between the two dates. The data type of the result is DECIMAL(8,0). If DATE1 is greater than or equal to DATE2, DATE2 is subtracted from DATE1. If DATE1 is less than DATE2, however, DATE1 is subtracted from DATE2, and the sign of the result is made negative.

Check this link for a better understanding of Date arithmetic

http://publibz.boulder.ibm.com/cgi-bin/bookmgr_OS390/BOOKS/DSNSQH11/2.20.9.1?SHELF=&DT=20010718164132&CASE=

Hope this helps...

Cheers

Kolusu
_________________
Kolusu
www.linkedin.com/in/kolusu
Back to top
View user's profile Send private message Send e-mail Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic   printer-friendly view    MVSFORUMS.com Forum Index -> Database All times are GMT - 5 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


MVSFORUMS
Powered by phpBB © 2001, 2005 phpBB Group