convert int to date in redshift - sql

I have column named datenum which is int type. For ex. 20220208
Need to convert datenum column type to date with YYYY/MM/DD format.
Tried to_date("datenum", 'YYYY/MM/DD') but isn't working.

Dates in Redshift (or any SQL database) do not really have any internal format. To convert your input text dates to bona fide Redshift dates, use TO_DATE with the appropriate date mask:
SELECT TO_DATE(datenum, 'YYYYMMDD')
FROM yourTable;
If you really want to view your input dates in some other text format, then do a full roundtrip back to string, using TO_CHAR:
SELECT TO_CHAR(TO_DATE(datenum, 'YYYYMMDD'), 'YYYY/MM/DD')
FROM yourTable;

Related

SQL, Converting date timestamp variable into strings 'yyyymmdd' and 'yyyymm'

I would like to replace a date timestamp variable 'date' with the format yyyy-mm-dd hh:mm:ss e.g, 2021-12-28 00:00:00
with two other string variables; one named date with the format: 'yyyymmdd' e.g, 20211228, and one named month with the format: 'yyyymm' e.g, 202112.
Can you give me some suggestions using a SELECT statement?
Thanks
Just use TO_CHAR (or whatever the equivalent is for your DBMS) and the appropriate format string

HIVE select where date between, but its a string YYYYMMDD, not numeric date format?

so I'm trying to learn how to deal with this.
the key partition column is "date" as a string (YYYYMMDD).
so its a character string, not a numeric date format.
I'm having trouble understanding how I can WHERE filter off this by normal date ranges like
where date > CURRENT_DATE -30
or
WHERE date BETWEEN '20210512' AND '20210517'
How does this work? Should the date column be converted first?
This is SQL in a HIVE database.
The best way is to convert the comparison date to a string. Many databases support to_char():
where date > to_char(current_date - 30, 'YYYYMMDD')
Those that do not would have some other function to convert the date to a string.
Note that this formulation allows the use of indexes and table partitions to optimize the query.

How do I convert a date timestamp in oracle to a date?

I'm looking to convert (using Oracle) a date timestamp
[2018-01-25T00:00:00.000+00:00] to a date [2018-01-24]. I've tried several formats however I can't seem to find the right one to convert it. I'm unsure of how to handle the +00:00.
Thanks in advance
It depends on what you really ask.
It you have a real Oracle timestamp and you want a string in format 'YYYY-MM-DD', you can use to_char():
select to_char(col, 'YYYY-MM-DD') as res from mytable
If you have a string in ISO timestamp format, and you want a string as a result:
select substr(col, 1, 10) as res from mytable
If you have a timestamp column and you want to set the time portion to 00:00:00:
select trunc(col) as res from mytable;
This returns a value of datatype date.

How to convert a VARCHAR to a date in ORACLE SQL?

I have that field in my table:
2020-01-16T10:55:16..296518000
How to convert this Varchar into a date in format 'YYYY-MM-DD' ?
I tried:
select TRUNC(to_date(DATE_UPDATED ,'YYYY-MM-DD hh24:mi:ss')) from JOB_SCHEDULE_TBL
but I'm getting an error:
ORA-01830: date format picture ends before converting entire input string
Just use substr():
select to_date(SUBSTR(DATE_UPDATED, 1, 10) ,'YYYY-MM-DD')
The trunc() is unnecessary.
You are confusing the format of DATE with what you are seeing on the screen. A DATE data type has no "format". It's Oracle's internal, binary format. Even when you SELECT TO_DATE ..., the result is going to get displayed by the client, and to do so the client will have to peform (under the covers) a TO_CHAR on the resulting DATE. And that implied to_char will use the current system/session/ settings for TNS_DATE_FORMAT.

convert MM/DD/YYYY to YYYYMMDD in redshift

I have a requirement to convert MM/DD/YYYY to YYYYMMDD in amazon redshift database.
My result of this query gives me some weird result. Can some one please help me.
select to_date ('07/17/2017','YYYYMMDD');
0007-07-20
If you just wish to convert the hard-coded string into a DATE:
select to_date('07/17/2017', 'MM/DD/YYYY')
If you have a column already formatted as DATE, then use:
to_char(fieldname, 'YYYYMMDD')
Combining the two concepts:
select to_char(to_date('07/17/2017', 'MM/DD/YYYY'), 'YYYYMMDD')
TO_DATE - converts a date represented in a character string to a DATE data type.
TO_CHAR - converts a time stamp or numeric expression to a character-string data format.
select to_char(sysdate,'YYYYMMDD');
If I’ve made a bad assumption please comment and I’ll refocus my answer.