Converting a numeric value into Date value in DB2 - sql

I have a DB2 table where NUM column is defined as INTEGER in DB2 and the query result is shown below,
NUM columns have numeric values which needs to be converted to date format. This numeric values are nothing but duration from 01.01.1850. Example : 01.01.1850 + 57677 days = 01.12.2007.
So Is it possible to convert or cast the numeric value into date fields in DB2 , so that the select query from the table can result as shown below after converting a numeric field into date field,

You may use the scalar ADD_DAYS function:
SELECT EMP_ID, ADD_DAYS('1850-01-01', NUM) AS NUM
FROM yourTable;

Not all Db2 products & versions have the ADD_DAYS function.
The following expression works for all of them.
You may optionally add DAY or DAYS at the end.
DATE ('1850-01-01') + 57677

Related

Null result when converting String to Date format in Databricks SQL

I have a String column from a temp view, were dates values are stored in format of '2020/01/01'.
I do need to convert that field to a date value to be stored into a Delta table in 'YYY-MM-DD' format.
I've been trying different kind of SQL functions but none of them seems to works, as a result, every function retrieves a null value:
select to_date(dateField, 'yyyy-MM-dd'),
CAST(dateField as date),
etc..
from TMP_table
Every function results in a null value, and there's no information about that into Databricks documentation.
As Usagi said above, select to_date(dateField, 'yyyy/MM/dd') AS date works perfectly.

Convert Number type to Date in postgres SQL

I have a numeric data in a column 20170930, need help in converting it into Date in PostgreSQL , tried multiple ways but non seems to work
You can convert to a string and then to a date:
select column::text::date
You can also express this using explicit cast() syntax:
select cast(cast(20170930 as text) as date)
Use one of the following :
SELECT cast(yourcol::varchar as date ) as dt1, yourcol::varchar::date as dt2
where dt1 and dt2 values of type date, and yourcol is a numeric value such as 20170930
Demo
The best thing is to change column datatype into Date type,
ALTER TABLE table_name
ADD column_name Date;
As shown above, PostgreSQL supports a full set of SQL date and time types, as shown in the table below. Dates are counted according to the Gregorian calendar. Here, all the types have a resolution of 1 microsecond / 14 digits except date type, whose resolution is day.
Please try below query
SELECT to_date(column::varchar,'YYYYMMDD')
For anybody who fell into my pitfall I tried this but my numeric was like a 'seconds past 01-01-1970 format' rather than YYYYMMDD
This worked
SELECT to_timestamp(yourcol) as numeric_column_now_date
from yourtable
see here
https://www.postgresql.org/docs/current/functions-datetime.html#FUNCTIONS-DATETIME-ZONECONVERT

Incorrect date returning when casting from varchar to date

When casting from a varchar data type to a date datatype, my query results are altering the day of the original field. For example, the below two queries
select to_Date('2017-12-15 00:11:10.167664+00', 'YYYY-MM-DD')
select '2017-12-15 00:11:10.167664+00'::date
return a value of "2017-12-14". I am querying a vertica database using DataGrip.
You can just take the leftmost 10 characters of the string and then convert to date, e.g.:
SELECT TO_DATE(LEFT('2017-12-15 00:11:10.167664+00',10), 'YYYY-MM-DD')

How to convert a binary date column date into gregorian date format in sql

This i got to convert a single binary date to Gregorian date ,
Select to_char(to_date(sum(2415020+42744),'J'),'MMDDYYYY') Last_arrear_date from dual;
But I'm not able to convert a binary date column date into Gregorean in xyz table
For Example
Suppose i have a table borm,That has account_open_date column that stores data in binary format , I want to convert that account_open_date column data into Gregorian date format. Please let me know how to convert that
While you have to change the entire column, Don't use the sum function.
Try the below SQL command it will work.
select acct_no, to_char(to_date((account_open_date + 2415020), 'J'),'DD-MON-YYYY')
from borm;
To Change Binary to Gregorian
select column1 ,...columnN, to_char(to_date((Column_name + 2415020), 'J'),'DD-MON-YYYY')
from Table_Name
WHERE Condition;
To change Gregorian to Binary:
select column1,columnN, to_char(to_date(Column_Name,'YYYYMMDD'),'J')-2415020
from Table_Name
where condition ;

Converting from VARCHAR to DATE

I have a db where I have converted 2 date columns to varchar for the purpose of getting 1 column substringed into another. However, now I don't seem to be able to convert the datatype when I try to use:
ALTER TABLE datacomplete
ALTER COLUMN yearmonth TYPE DATE; /*Can't find a way to specify a format*/
It throws this error:
ERROR: column "yearmonth" cannot be cast automatically to type date
Hint: You might need to specify "USING yearmonth::date".
I'm not sure how to use that command at all, could anyone potentially assist?
My first column is in the format of yyyy-mm-dd, however I'd like it to be yyyymm only, but I'm guessing this is easier once I convert the datatype to date and I can somehow switch formats.
The second column only shows the year so I need to convert it to date as format 'yyyy'.
UPDATE: The first one was solved, now I need to convert the second to 'yyyy'
ALTER TABLE pscomplete_1 ALTER COLUMN "year" TYPE DATE USING "year"::date;
It throws this error
15:12:51 [ALTER - 0 rows, 1.062 secs] [Code: 0, SQL State: 22007] ERROR: >invalid input syntax for type date: "2016"
... 1 statement(s) executed, 0 rows affected, exec/fetch time: 1.062/0.000 sec >[0 successful, 1 errors]
The USING keyword allows you to give the translation function to PostgreSQL.
For your first column it is easy for you already have a correct DATE format:
ALTER TABLE datacomplete ALTER COLUMN yearmonth TYPE DATE USING yearmonth::DATE;
For your second column it is unclear for PostgreSQL which exact date you want. Let's say we want the first of January of the given year:
ALTER TABLE datacomplete ALTER COLUMN year TYPE DATE USING (year || '-01-01')::DATE;
I understood you were starting from a VARCHAR column in the format 'YYYY-MM-DD'.
So I'll do the same.
And you want a column in the date format, and you want a yearmonth in all-digits format.
If I'm not forced to use leading zeroes in an all-digits column, I prefer INT to string columns.
And I prefer to make the same derivation only once if I can do that.
This is why I use a WITH clause (global table expression) to cast the varchar to date, and then I use the resulting date for the DATE_PART() function I use to create the yearmonth column. I have seen very often that date arithmetics are safer and often faster than subtring-ing the date literal (remember, the Americans format dates differently from the Europeans, there are different formats within Europe, and also in Asia, and not all like the ISO date format). So I derive yearmonth as the year multiplied by 100, plus the month, and as DATE_PART() returns a float, I cast the whole expression to INT.
So here goes:
WITH foo(varchar_dt) AS (
SELECT '2017-01-11'
UNION ALL SELECT '2016-12-11'
UNION ALL SELECT '2016-11-11'
UNION ALL SELECT '2016-10-11'
)
, foo_with_date AS (
SELECT
varchar_dt
, CAST(varchar_dt AS DATE) AS the_date
FROM foo
)
SELECT
varchar_dt
, the_date
, CAST(DATE_PART('year',the_date)*100 + DATE_PART('month',the_date) AS INT) AS yearmonth
FROM foo_with_date
;
varchar_dt|the_date |yearmonth
2017-01-11|2017-01-11| 201,701
2016-12-11|2016-12-11| 201,612
2016-11-11|2016-11-11| 201,611
2016-10-11|2016-10-11| 201,610
I can't help myself - I find this much cleaner, and filtering by yearmonth would become filtering by an integer, which is always at least a little bit faster than strings.
Happy playing
Marco the Sane