How to pass format to to_timestamp in postgresql? - sql

I have utc epocha in millisecond and I would like my sql return resulting dates in a specific date format.
This works
SELECT to_timestamp(timestamp / 1000) as date
FROM data
This does not work
SELECT to_timestamp(timestamp / 1000, 'YYYY-MM-DD HH:MI:SS') as date
FROM data
Could you tell me what is wrong in my script and how to fix it please? Thanks

to_timestamp() does not format your output. The version where to_timestamp() accepts a format mask is used to convert a string value to a proper timestamp.
You need to format the result of your conversion (which is a proper timestamp) using to_char().
to_char(to_timestamp(timestamp / 1000), 'yyyy-mm-dd hh24:mi:ss') as date

Related

correct to_char date syntax to have trailing zeroes after milliseconds

My current query in oracle sql for getting a timestamp format is TO_CHAR(c2.start_on,'DD-MM-YY HH:MI:SS.FF PM'), it outputs the timestamp like this 25-11-20 07:00:13.36 PM
However I want it to display the date in this way 25-11-20 07:00:13.360000000 PM
What should I add in the timestamp format for this to be possible ?
I have tried doing it like this HH:MI:SS.FM00000 as suggested here
but it gives me the error. ORA-01821: date format not recognized
what is the correct way to get the date in the desired format ?
If you want fractional seconds, you don't want a DATE, you want a TIMESTAMP. So here's a timestamp formatted with 6 digits of precision
select to_char(systimestamp, 'HH:MI:SS.FF6') from dual;
If you have a date, you could convert it to a TIMESTAMP (using CAST AS TIMESTAMP), but better to look at updating your data model to use the proper type for the source column as starters.

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.

PL SQL - Convert timestamp to datetime/date

select
to_timestamp(SCHEDULED_TIME,'YYYY-MM-DD HH24:MI:SS.FF') as SCHEDULED_TIME,
TRUNC(to_date(to_timestamp(SCHEDULED_TIME,'YYYY-MM-DD HH24:MI:SS.FF'),'YYYY-MM-DD HH24:MI:SS'))
from S_TIDAL_STATUS
The error was:
ORA-01830: date format picture ends before converting entire input string
01830. 00000 - "date format picture ends before converting entire input string"
The goal is to return something like
2017-07-91 23:14:00
(without the content after the dot).
Here's what the SCHEDULED_TIME (timestamp) looked like:
The problem in your attempt is the function TO_DATE() applied to a timestamp. TO_DATE() takes a VARCHAR2 (string) input, not a timestamp. So Oracle converts the timestamp to a string first, implicitly, using your NLS_TIMESTAMP_FORMAT parameter, and then attempts to convert this string to a date. Depending on your NLS_TIMESTAMP_FORMAT, you may get different errors.
The way to convert a timestamp to a date (datetime) - while truncating off the fractions of a second - is with the CAST function. Example:
select systimestamp,
cast (systimestamp as date) as ts_cast_to_date
from dual
;
Alternatively, if all your strings are EXACTLY in that format, you can truncate the strings first and apply TO_DATE directly:
to_date(substr(scheduled_time, 1, 19), 'yyyy-mm-dd hh24:mi:ss')
This should do the trick:
select
to_char(SCHEDULED_TIME,'YYYY-MM-DD HH24:MI:SS.FF') as time_to_csecs,
to_char(SCHEDULED_TIME,'YYYY-MM-DD HH24:MI:SS') as time_to_secs,
TRUNC(to_date(to_char(SCHEDULED_TIME,'YYYY-MM-DD HH24:MI:SS'),'YYYY-MM-DD HH24:MI:SS')) as time_to_day
from S_TIDAL_STATUS
Please review the docs to see the difference between to_timestamp and to_char.

How to convert date to datetime in Oracle?

i have a date in oracle with this format DD-MM-YYY and i want to convert it to datetime with this other format DD-MM-YYY HH24:MI how can i proceed?
I've tried this but nothing is working :
to_date(the_date,'DD-MM-YYY HH24:MI')
and also this:
to_date(to_char(date_debut_p),'DD-MM-YYY HH24:MI')
i have a date in oracle with this format DD-MM-YYY and i want to convert it to datetime with this other format DD-MM-YYY HH24:MI
No, you are confused. Oracle does not store dates in the format you see. It is internally stored in 7 bytes with each byte storing different components of the datetime value.
DATE data type always has both date and time elements up to a precision of seconds.
If you want to display, use TO_CHAR with proper FORMAT MODEL.
For example,
SQL> select to_char(sysdate, 'mm/dd/yyyy hh24:mi:ss') from dual;
TO_CHAR(SYSDATE,'MM
-------------------
11/25/2015 22:25:42
Oracle DATE datatype ALWAYS contains (stores) time.
If you want to see it, you can use function TO_CHAR.
If you want to add, for example, 1 hour, you can just use date_debut_p+1/24.
If you want to covert to timestamp, you can do the following:
Select to_timestamp(date_column, 'DD-MM-YYY') from table;
However, if you want in the required format, you can do the following:
Select to_char(to_timestamp(date_column, 'DD-MON-YY'), 'DD-MM-YYY HH24:MI')
from table;
Hope it helps..

convert string literal to a date

I have a varchar2 field in my db with the format of for example -
2015-08-19 00:00:01.0
2014-01-11 00:00:01.0
etc.
I am trying to convert this to a date of format DD-MON-YYYY. For instance, 2015-08-19 00:00:01.0 should become 19-AUG-2015. I've tried
select to_date(upgrade_date, 'YYYY-MM-DD HH24:MI:SS') from connection_report_update
but even at this point I'am getting ORA-01830 date format ends before converting the entire input string. Any ideas?
You have details upto milli seconds, for which, you have to use TO_TIMESTAMP() with format model 'FF'
select to_timestamp('2015-08-19 00:00:01.0' ,'YYYY-MM-DD HH24:MI:SS.FF') as result from dual;
RESULT
---------------------------------------------------------------------------
19-AUG-15 12.00.01.000000000 AM
And Date doesn't have a format itself, only the date output can be in a format. So, when you want it to be printed in a different format, you would need to again use a TO_CHAR() of the converted timestamp;
select to_char(to_timestamp('2015-08-19 00:00:01.0' ,'YYYY-MM-DD HH24:MI:SS.FF'),'DD-MON-YYYY') as result from dual;
RESULT
-----------
19-AUG-2015
Why do you store datetimes in a string???
Anyhow. To get from '2015-08-19 00:00:01.0' to a datetime with milliseconds (which is a TIMESTAMP in Oracle) use to_timestamp:
to_timestamp('2015-08-19 00:00:01.0', 'yyyy-mm-dd hh24:mi:ss.ff')
Then to get the desired output format, use to_char:
to_char(thedate, 'DD-MON-YYYY')
Together:
to_char(to_timestamp('2015-08-19 00:00:01.0', 'yyyy-mm-dd hh24:mi:ss.ff'), 'DD-MON-YYYY')
You should be specifying the format that you want in the call of to_date not the current format:
select to_date(upgrade_date, 'DD-MM-YYYY') from connection_report_update