SQL convert numeric to date format
245 pts.
0
Q:
SQL convert numeric to date format
How can I convert an 8 digit numeric (format ccyymmdd) into a real datefield to be used in date arithmetics?

I need to calculate the number of months between
month(current date
and a date represented in my DB as an 8 digit numeric with format "ccyymmdd". Therefor (I assume) I need to convert my 8 digit column into a date field to be able to use date arithmetics.

 

ASKED: Sep 6 2009  2:13 PM GMT
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
0
24610 pts.
0
A:
 RATE THIS ANSWER
0
Click to Vote:
  •   0
  •  0
  • AddThis Social Bookmark Button
Dane
I RPG it's %date(mydt80:*iso0)
but I don't think that SQL has an ISO0
so it's
Date(substr(char(mydt90).1.4) || '-'||
substr(char(mydt90).5.2) || '-'||
substr(char(mydt90).7.2) , ISO)

Phil

-------------- kccrosser

Cast and Convert allow converting to/from datetime and strings easily, and supports multiple different ISO and non-ISO formats.

Convert(datetime, <inputvar>, 114) will convert a string of the form ccyymmdd to a datetime value.

SQL Books Online has all the codes used to convert back and forth.

Then, once you have two dates, the months between them is easily obtained with DateDiff and GetDate:

declare @datestring varchar(8)
declare @datethen datetime
declare @monthssince int
...
set @datestring = '20060724'
set @datethen = convert(datetime, @datestring, 114)
set @monthssince = datediff(m, @datethen, getdate())

--------------------------------
Sorry, finally got to test this.
Had a couple of periods and didn't need ISO

Date(substr(char(mydt80),1,4) || '-'||
substr(char(mydt80),5,2) || '-'||
substr(char(mydt80),7,2) )

Select Fld1, Fld2,
Date(substr(char(mydt80),1,4) || '-'||
substr(char(mydt80),5,2) || '-'||
substr(char(mydt80),7,2) )

from myfile

Phil
Last Answered: Sep 8 2009  8:25 PM GMT by Philpl1jb   24610 pts.
Latest Contributors: Kccrosser   1850 pts.
0
0
Discuss This Answer:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _



_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

DanTheDane   245 pts.  |   Sep 7 2009  1:23PM GMT

Phil,

As far as I can see, your code leaves me with a char field; - what is need is a date-field that can be used in dta-calcs.

Thanks for your interest.
Dan

 

Carlosdl   29855 pts.  |   Sep 7 2009  2:03PM GMT

What database system do you need to do this on ?

 

DanTheDane   245 pts.  |   Sep 7 2009  2:33PM GMT

hi Carlosdl,

I’m using an IBM Power 6 System i, so opsys is OS/400

Dan

 

DanTheDane   245 pts.  |   Sep 7 2009  2:34PM GMT

hi Carlosdl,

… to be more precise: DB2.

Dan

 

Philpl1jb   24610 pts.  |   Sep 7 2009  2:53PM GMT

You were doing SQL

with a numeric field mydt80
you
1. convert it to char — char(mydt80)
2. get substrings to build ccyy-mm-dd
3. do a date(isodatestring,ISO)
to get a date. Sorry, don’t have a computer today.

Date(substr(char(mydt80).1.4) || ‘-’||
substr(char(mydt80).5.2) || ‘-’||
substr(char(mydt80).7.2) , ISO)
Phil

 

Philpl1jb   24610 pts.  |   Sep 7 2009  8:55PM GMT

convert(datetime, @datestring, 114)
is great but I don’t think it’s available in AS/400 SQL
Phil

 

Kccrosser   1850 pts.  |   Sep 8 2009  6:52PM GMT

Check the following link for discussions on date/time conversions in DB2. Unfortunately, it doesn’t have an equivalent to the SQL Convert/Cast functions for date/time, so you will need to write one. The page at the link discusses these in detail and gives some example functions.

 

0