Db2 Convert Number To Date
Solution 1:
1) How do I convert the OAORDT field to Date?
Simplest is to use TIMESTAMP_FORMAT
:
SELECTDATE(TIMESTAMP_FORMAT(CHAR(OAORDT),'YYYYMMDD'))
2) Being in the UK I need it to be [...] in Date format 21/12/2017
:
SELECT VARCHAR_FORMAT(DATE(TIMESTAMP_FORMAT(CHAR(OAORDT),'YYYYMMDD')),'DD/MM/YYYY')
Solution 2:
Note, you didn't specify where you are doing this, but since you tagged as ibm-midrange, I am answering for embedded SQL. If you want JDBC, or ODBC, or interactive SQL, the concept is similar, just the means of achieving it is different.
Make sure SQL is using dates in the correct format, it defaults to *ISO
. For you it should be *EUR
. In RPG, you can do it this way:
execsqlset option *datfmt =*EUR;
Make sure that set option
is the first SQL statement in your program, I generally put it immediately between D and C specs.
Note that this is not an optimal solution for a program. Best practice is to set the RPG and SQL date formats both to *ISO. I like to do that explicitly. RPG date format is set by
ctl-opt DatFmt(*ISO);
SQL date format is set by
execsqlset option *datfmt =*ISO;
Now all internal dates are processed in *ISO format, and have no year range limitation (year can be 0001 - 9999). And you can display or print in any format you please. Likewise, you can receive input in any format you please.
Edit Dates are a unique beast. Not every language, nor OS knows how to handle them. If you are looking for a Date value, the only format you need to specify is the format of the string you are converting to a Date. You don't need to (can't) specify the internal format of the Date field, and the external format of a Date field can be mostly anything you want, and different each time you use it. So when you use TIMESTAMP_FORMAT()
as @Stavr00 mentioned:
DATE(TIMESTAMP_FORMAT(CHAR(OAORDT),'YYYYMMDD'))
The format provided is not the format of the Date field, but the format of the data being converted to a Timestamp
. Then the Date()
function converts the Timestamp
value into a Date
value. At this point format doesn't matter because regardless of which external format you have specified by *DATFMT
, the timestamp is in the internal timestamp format, and the date value is in the internal date format. The next time the format matters is when you present the Date value to a user as a string or number. At that point the format can be set to *ISO
, *EUR
, *USA
, *JIS
, *YMD
, *MDY
, *DMY
, or *JUL
, and in some cases *LONGJUL
and the *Cxxx
formats are available.
Solution 3:
Since none of variants suited my needs I've came out with my own.
It is as simple as:
select * from yourschema.yourtable where yourdate = int(CURRENT DATE - 1 days) - 19000000;
This days
thing is leap year-aware and suits most needs fine.
Same way days
can be turned to months
or years
.
No need for heavy artillery like VARCHAR_FORMAT
/TIMESTAMP_FORMAT
.
Post a Comment for "Db2 Convert Number To Date"