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

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

Related

convert int to date in redshift

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;

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.

In oracle SQL how to get todays date in the format of lets say 01-JAN-15?

Just want to know In oracle SQL how to get todays date in the format of lets say 01-JAN-15?
So it should return 06-DEC-15
Use to_char(). This is how you convert dates to a string with any format you like:
select to_char(sysdate, 'DD-MON-YY')
The explanation of the format is here.

ORA-01722 INVALID NUMBER in oracle

I am getting invalid number error message while executing the below select statement.Can any one have an idea about the issue..Please let me know.
select TO_DATE(TO_CHAR('2015/01/22 00:00:00','YYYY/MM/DD'),'YYYY/MM/DD')
actually i want oracle standard date format without time stamp for this date '2015/01/22 00:00:00'
select to_date('2015/01/22 00:00:00','YYYY/MM/DD HH24:MI:SS') as dt
from dual
Fiddle - http://sqlfiddle.com/#!4/6a3a6/1/0
As an FYI, the Oracle DATE data type does include the time component (just not down to fractional seconds, as is the case with the TIMESTAMP data type).
If you are converting values and want to bring all the time values to zero you can use the trunc function like this (which changes 12:07:00 to 00:00:00):
select trunc(to_date('2015/01/22 12:07:00','YYYY/MM/DD HH24:MI:SS'),'DD') as dt_with_time_zerod
from dual
Fiddle - http://sqlfiddle.com/#!4/6a3a6/2/0
If the source is itself a date and you want to convert the date to a string in the Oracle default date format ('DD-MON-RR') you can achieve that by running:
select to_char(trunc(to_date('2015/01/22 12:07:00','YYYY/MM/DD HH24:MI:SS'),'DD'),'DD-MON-RR') as dt_with_time_zerod
from dual
Fiddle - http://sqlfiddle.com/#!4/6a3a6/3/0
If it's a date field, to_char without a mask will give you what you say you want.
actually i want oracle standard date format without time stamp for this date '2015/01/22 00:00:00'
I'm not sure what you mean by "Oracle standard date format." The format in which a date would appear would be based on your NLS settings (in particular, NLS_DATE_FORMAT). If you are just trying to format this string representing a date, then you might want something like the following:
SELECT TO_CHAR(TO_DATE('2015/01/22 00:00:00','YYYY/MM/DD HH:MI:SS'), 'YYYY/MM/DD')
FROM dual;
That is, you have the TO_CHAR() and TO_DATE() functions in the wrong order, and an incomplete date mask for the call to TO_DATE().
Try using date literals with the standard ISO 8601 format.
date '2015-01-22'
I suggest you not to give hour-minute-second if you do not want to show the time.
This is my simplest answer :
SELECT TO_DATE('2015/01/22','YYYY/MM/DD') FROM dual

Formatting dates in PostgreSQL

I have a field which its format is date with time as: YYYY-MM-DD HH-MM-SS for example: 2000-08-12 00:00:00 I want to get just the date part and change its format to DD/MMM/YYYY for example the expected result of the previous example will be: 12/Aug/2000
The field definition is: Ddate timestamp without time zone DEFAULT now()
I read the whole page of Date/Time Functions and Operators and other sources as well but I couldn't find any information that is helpful.
You can use the to_char function to format your column:
SELECT TO_CHAR(ddatte, 'dd/Mon/yyyy') FROM mytable
try with:
to_char(your_Field, 'dd/mm/yyyy')