Convert Decimal to Date in DB2 - sql

I have a column in my table having date value as Decimal like 20180715 for the date 15-07-2018.
I want to convert it to MMDDYYYY format.
Example:
Given decimal value 20180715 is converted to 07152018.
How to do it ?

try somthing like this:
select
VARCHAR_FORMAT( TIMESTAMP_FORMAT(cast(yourcolumn as varchar(8)), 'YYYYMMDD') , 'MMDDYYYY')
from yourtable
but you you want a really date do it:
select
DATE( TIMESTAMP_FORMAT(cast(yourcolumn as varchar(8)), 'YYYYMMDD'))
from yourtable

Try this query for the date
select
date(timestamp_format(char(yourcolumn+19000000), 'YYYYMMDD'))
from yourtable
To get the time
select
cast( substr( right( '00' || yourcolumn, 6) ,1,2) || ':' || substr( right( '00' || yourcolumn, 6) ,3,2) || ':' || substr( right( '00' || yourcolumn, 6) ,5,2) as time)
from yourtable

Related

Progress SQL : Convert Julian Date into datetime

I working for a client who uses PROGRESS database SQL (i didn't knew this kind of database, so it's my first time that i work on it).
My problem it's all the dates are in Julian-date, and i want to convert them into datetime. But i haven't found any document or help online that deals with this.
The only document i found it's:
https://knowledgebase.progress.com/articles/Article/How-to-Obtain-a-Julian-Date-in-Progress
But i want to do exactly the opposite.
For example in postgresql:
select to_timestamp(column1::text,'J')
from table1
But on PROGRESS it's harder and there is less information and examples than the others databases on the web
Thank you in advance for your help
Character
In case your Julian date is stored in a field with the character data type, you can use instr to pull it apart and then reassemble it into a timestamp:
select
-- my character field containing 92182.3966
descr,
-- get the year
floor( cast( left( descr, instr( descr, '.' ) - 1 ) as integer ) / 1000 ) as 'year',
-- get the day
mod( cast( left( descr, instr( descr, '.' ) - 1 ) as integer ), 1000 ) as 'day',
-- get the time
cast(
'0' + right( descr, length( descr ) - instr( descr, '.' ) + 1 )
as float
) as 'time',
-- combine all to timestamp
cast(
-- get first day of year
cast(
to_char(
floor( cast( left( descr, instr( descr, '.' ) - 1 ) as integer ) / 1000 )
+ 1900 -- !!! beware
)
+ '-01-01'
as date
)
-- add days
+ mod( cast( left( descr, instr( descr, '.' ) - 1 ) as integer ), 1000 )
as timestamp
)
-- add milliseconds
+ cast(
cast(
'0' + right( descr, length( descr ) - instr( descr, '.' ) + 1 )
as float
) * 86400 * 1000
as integer
) as 'timestamp'
from pub.ddcapp
where application = 'JULIAN'
This reports that the time is 1992-07-01 09:31:06.24 which is 1 second later than what your link states it was translated from.
Decimal
If on the other hand your field is a decimal, it is a lot simpler:
select
-- my decimal field containing 92182.3966
open_bal,
-- get the year
floor( open_bal / 1000 ) as 'year',
-- get the day
mod( open_bal, 1000 ) as 'day',
-- get the time
open_bal - floor( open_bal ) as 'time',
-- combine all to timestamp
cast(
cast(
to_char(
floor( open_bal / 1000 )
+ 1900 -- !!!
)
+ '-01-01'
as date
)
+ mod( open_bal, 1000 )
as timestamp
)
+ ( open_bal - floor( open_bal ) ) * 86400 * 1000
as 'timestamp'
from pub.ledbal
where adm_nr = 0

Convert single digit months to double digit

I have a date column which have dates in these formats dd/mm/yyyy, d/m/yyyy.
So basically if the month is January it will have it as 1 instead of 01.
But if the month is October it will have it as 10.
How can i convert that column to be in yyyy/mm/dd format?
For example convert 1/1/2021 to 2021/01/01.
You can do it with string functions and implicit conversions of strings to integers:
SELECT SUBSTR(datecol, -4) || '/' ||
SUBSTR('0' || (SUBSTR(datecol, INSTR(datecol, '/') + 1) + 0), -2) || '/' ||
SUBSTR('0' || (datecol + 0), -2) date
FROM tablename
Change datecol to the name of your column.
If you want to update the column it is better to use the format 'YYYY-MM-DD' which is the only valid text date format for SQLite:
UPDATE tablename
SET datecol = SUBSTR(datecol, -4) || '-' ||
SUBSTR('0' || (SUBSTR(datecol, INSTR(datecol, '/') + 1) + 0), -2) || '-' ||
SUBSTR('0' || (datecol + 0), -2)
See a simplified demo.

Formatting date when using CAST

I would like to use CAST to convert a DATE type to a VARCHAR2 type.
DBUSER >SELECT CAST(CURRENT_DATE AS VARCHAR2(20)) THE_DATE from DUAL;
THE_DATE
--------------------
09-AUG-17
However, I need the VARCHAR2 result to be formatted as 'YYYYMM'. I know that I can achieve this effect by changing the session date format, but I would rather not do that.
DBUSER >ALTER SESSION SET NLS_DATE_FORMAT = 'YYYYMM';
Session altered.
DBUSER >SELECT CAST(CURRENT_DATE AS VARCHAR2(20)) THE_DATE from DUAL;
THE_DATE
--------------------
201708
I would like to avoid using Oracle's proprietary TO_CHAR() function. Does anyone have a suggestion on how to do that?
I am trying to standardize on ANSI SQL to the degree possible and avoid proprietary vendor nonstandard implementations.
There is no function specified in the ANSI SQL92 standard which formats DATETIME datatypes as a string.
The simplest solution is to use the functions Oracle provides for that purpose:
SELECT TO_CHAR( yourdate, 'YYYYMM' ) FROM yourtable;
However, you can get the year and month components using the EXTRACT function (which is in the ANSI standard):
SELECT EXTRACT( YEAR FROM yourdate ),
EXTRACT( MONTH FROM yourdate )
FROM yourtable;
Then you need to convert the numbers to a string and concatenate the strings:
SELECT TO_CHAR( EXTRACT( YEAR FROM yourdate ) )
|| TO_CHAR( EXTRACT( MONTH FROM yourdate ) )
FROM yourtable
but you were trying to avoid TO_CHAR so you could do:
SELECT CAST( EXTRACT( YEAR FROM yourdate ) AS VARCHAR2(4) )
|| CAST( EXTRACT( MONTH FROM yourdate ) AS VARCHAR2(2) )
FROM yourtable
or, using an implicit cast
SELECT EXTRACT( YEAR FROM yourdate )
|| EXTRACT( MONTH FROM yourdate )
FROM yourtable
However, if the year is not 4-digits or the month is not 2-digits then you need to pad the values; again, the simple solution is TO_CHAR:
SELECT TO_CHAR( EXTRACT( YEAR FROM yourdate ), 'FM0000' )
|| TO_CHAR( EXTRACT( MONTH FROM yourdate ), 'FM00' )
FROM yourtable
or LPAD:
SELECT LPAD( EXTRACT( YEAR FROM yourdate ), 4, '0' )
|| LPAD( EXTRACT( MONTH FROM yourdate ), 4, '0' )
FROM yourtable
But neither of those are in the ANSI standard so:
SELECT CASE
WHEN EXTRACT( YEAR FROM yourdate ) < 10 THEN '000'
WHEN EXTRACT( YEAR FROM yourdate ) < 100 THEN '00'
WHEN EXTRACT( YEAR FROM yourdate ) < 1000 THEN '0'
ELSE NULL
END
|| EXTRACT( YEAR FROM yourdate )
|| CASE
WHEN EXTRACT( MONTH FROM yourdate ) < 10 THEN '0'
END
|| EXTRACT( MONTH FROM yourdate )
FROM yourtable;
And we've managed to transform a single Oracle function into a behemoth of an ANSI compatible expression.
But, Oracle's DATE datatype does not comply to the ANSI standard (it is a concatenation of the ANSI DATE and TIME datatypes) so I'll ask whether it is worth it - especially if you then consider displaying the time component of a date (which EXTRACT will not extract unless you first use CAST to convert the DATE to a TIMESTAMP).
SELECT TO_CHAR( yourdate, 'YYYYMMDDHH24MISS' ) FROM yourtable
or
SELECT CASE
WHEN EXTRACT( YEAR FROM yourdate ) < 10 THEN '000'
WHEN EXTRACT( YEAR FROM yourdate ) < 100 THEN '00'
WHEN EXTRACT( YEAR FROM yourdate ) < 1000 THEN '0'
ELSE NULL
END
|| EXTRACT( YEAR FROM yourdate )
|| CASE
WHEN EXTRACT( MONTH FROM yourdate ) < 10 THEN '0'
END
|| EXTRACT( MONTH FROM yourdate )
|| CASE
WHEN EXTRACT( DAY FROM yourdate ) < 10 THEN '0'
END
|| EXTRACT( DAY FROM yourdate )
|| CASE
WHEN EXTRACT( HOUR FROM CAST( yourdate AS TIMESTAMP ) ) < 10 THEN '0'
END
|| EXTRACT( HOUR FROM CAST( yourdate AS TIMESTAMP ) )
|| CASE
WHEN EXTRACT( MINUTE FROM CAST( yourdate AS TIMESTAMP ) ) < 10 THEN '0'
END
|| EXTRACT( MINUTE FROM CAST( yourdate AS TIMESTAMP ) )
|| CASE
WHEN EXTRACT( SECOND FROM CAST( yourdate AS TIMESTAMP ) ) < 10 THEN '0'
END
|| EXTRACT( SECOND FROM CAST( yourdate AS TIMESTAMP ) )
FROM yourtable;
[TL/DR] Just use TO_CHAR
This may be help you:
SELECT extract(year from CURRENT_DATE) || case when extract(month from CURRENT_DATE) <10 THEN '0' || extract(month from CURRENT_DATE) END THE_DATE from DUAL;

how to get today date in YYYYMMDD in firebird

how to get today date in YYYYMMDD in firebird, I had a look on following but could not figured how to write this.
I think you can do:
select replace(cast(cast('Now' as date) as varchar(10)), '-', '')
from rdb$database
IN FIREBIRD v2.5.5:
SELECT LPAD( EXTRACT( YEAR FROM CURRENT_TIMESTAMP ), 4, '0' ) ||
LPAD( EXTRACT( MONTH FROM CURRENT_TIMESTAMP ), 2, '0' ) ||
LPAD( EXTRACT( DAY FROM CURRENT_TIMESTAMP ), 2, '0' ) || ' ' ||
LPAD( EXTRACT( HOUR FROM CURRENT_TIMESTAMP ), 2, '0' ) ||
LPAD( EXTRACT( MINUTE FROM CURRENT_TIMESTAMP ), 2, '0' ) ||
LPAD( TRUNC( EXTRACT( SECOND FROM CURRENT_TIMESTAMP ) ), 2, '0' )
FROM rdb$database
OUTPUT IS: YYYYMMDD HHMMSS
This is a fully version (in Integer)
select Extract(year FROM cast('NOW' as date))*10000 +
Extract(month FROM cast('NOW' as date))*100 +
Extract(day FROM cast('NOW' as date)) from rdb$database
This is a fully version (in VARCHAR)
select CAST(Extract(year FROM cast('NOW' as date))*10000 +
Extract(month FROM cast('NOW' as date))*100 +
Extract(day FROM cast('NOW' as date)) AS VARCHAR(8)) from rdb$database
This Should work.
CREATE TABLE tab( t time, d date, ts timestamp );
INSERT INTO tab(t,d,ts) VALUES ('14:59:23', '2007-12-31', '2007-12-31 14:59');
SELECT CAST(CAST(d as varchar(10)))
FROM tab;

convert integer to Days

I'm looking to convert an integer to Days using a db2 database. The integers are in this format 20130101 or YYYYMMDD. I believe you have to write a custom function after converting the integer to a char but I was unsure of how to do the second conversion to DAYS. I'm looking for a returned format January, 1, 2013 from 20130101.
WITH
/*****************************************************
*** Sample Data ***
*****************************************************/
sample_data
( START_DATE , END_DATE ) AS
(
VALUES
(20130101, 20131227 )
, (20130930, 20131230 )
, (20130411, 20130912 )
, (20130410, 20140101 )
)
,
t2(START_DATE, END_DATE) AS
( SELECT
CAST(SUBSTR(START_DATE, 1,4) CONCAT '-'
CONCAT SUBSTR(START_DATE, 5,2) CONCAT '-'
CONCAT SUBSTR(START_DATE, 7,2) AS CHAR(15)),
CAST(SUBSTR(END_DATE, 1,4) CONCAT '-'
CONCAT SUBSTR(END_DATE, 5,2) CONCAT '-'
CONCAT SUBSTR(END_DATE, 7,2) AS CHAR(15))
FROM SAMPLE_DATA
)
SELECT
START_DATE,
END_DATE
FROM t2
You can use this:
select monthname(to_date(20130101, 'YYYYMMDD')) || ', ' ||
day(to_date(20130101, 'YYYYMMDD')) || ', ' ||
year(to_date(20130101, 'YYYYMMDD')) from sysibm.sysdummy1
The result is:
January, 1, 2013
Replace the integer 20130101 by your field name.
If you will be using the conversion in several places, it's probably better to create a function to avoid repeating the field and conversions.
You could also cut a few corners using aritmetics to get year and day, such as this:
select monthname(to_date(20130101, 'YYYYMMDD')) || ', ' ||
mod(20130101, 100) || ', ' || to_char(20130101 / 10000) from sysibm.sysdummy1
The result is the same.
It's a lot of casting, but you can use the TIMESTAMP_FORMAT function:
date(timestamp_format(char(start_date),'YYYYMMDD'))
Keep in mind that this just gets you a value that is an actual DATE, not necessarily in the "pretty" format that you list above.