Teradata - Timestamp conversion - teradata

I am having a timestamp in the format YYYY-MM-DD HH:MI:SS and I want to convert it in the format MM/DD/YYYY HH24:MI:SS.
What is the easiest way to achieve it?

The format of a timestamp is only applied when it's casted from/to a string
-- using Teradata's format
Cast(Cast(ts AS Format 'MM/DD/YYYYBHH:MI:SS') AS VARCHAR(19))
-- shorter using Oracle's To_Char
To_Char(ts, 'MM/DD/YYYY HH24:MI:SS')

Related

Time format is not recognized in snowflake

I have a table field in below format in snowflake. While trying to_date(), to_timestamp() function, its erring out with error message as - Timestamp '8/05/2018 9:03:53 PM' is not recognized.
Format - '8/05/2018 9:03:53 PM'
Specify the format in the TO_TIMESTAMP as follows:
to_timestamp('8/05/2018 9:03:53 PM','MM/DD/YYYY HH12:MI:SS AM') -- assumes MM/DD/YYYY
or
to_timestamp('8/05/2018 9:03:53 PM','DD/MM/YYYY HH12:MI:SS AM') -- assumes DD/MM/YYYY
The format you are using is ambiguous, is it 8th May or August 5th?
Due to the above we don't support that date format.
For more information have a look here

Teradata - Date format conversion from INT(yyyymmdd) type to date(mm/dd/yyyy) in Teradata SQL Assistant

I have dates in integer format in a column. The length is 11.
Example values
current format integer (11) --> date format required
yyyymmdd --> dd/mm/yyyy
20121203 --> 03/12/2012
20090403 --> 03/04/2009
Can someone suggest a solution keeping in mind that the change need to reflect across the entire column in the table?
Use STR_TO_DATE :
select STR_TO_DATE(col1, "%Y%m%d") as my_date
from test_tbl;
Result:
my_date
2012-12-03
2009-04-03
Demo
Or DATE_FORMAT as previous answer:
select DATE_FORMAT(col1, "%d/%m/%Y") as my_date
from test_tbl;
Result:
my_date
03/12/2012
03/04/2009
Demo
Maybe using both:
select DATE_FORMAT(STR_TO_DATE(col1, "%Y%m%d"),'%d/%m/%Y') as my_date
from test_tbl;
my_date
03/12/2012
03/04/2009
Demo
The easiest way is based on Teradata's storage of dates:
Cast(intdate - 19000000 AS DATE)
Of course this only works if there are no bad dates, as your source seems to be MySQL there might be 20220200, etc.
For Teradata SQL Assistant, you can use TO_CHAR and then CAST it to a DATE format like this:
SEL CAST(TO_CHAR(20211015) AS DATE FORMAT 'YYYYMMDD')
Result: 10/15/2021 (mm/dd/yyyy)
Docummentation: https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_date-format
DATE_FORMAT("20121203", "%d/%m/%Y")

How can I convert 12 hour format hours into 24 hour format time in ORACLE SQL like 01-FEB-18 01.00.21.645000000 PM should be 05-01-0018 13:12:44

there are multiple timestamps column in oracle Sql , Now in want to convert 12 hour format time into 24 hour format time like 01-FEB-18 01.00.21.645000000 PM should be 05-01-0018 13:12:44. but I need to convert hours into 24 hours time format , I am using the below statement.
SELECT TO_CHAR(TO_DATE(PERFORMED_TIMESTAMP,'DD-MON-YYYY hh:mi:ss AM'),'DD-MM-YYYY hh24:mi:ss')
FROM FACT_WORK_ITEM_ACTION
WHERE TRUNC(PERFORMED_TIMESTAMP)>= '05-JUN-18'
But still i am getting the below error .
Error Code....
ORA-01855: AM/A.M. or PM/P.M. required
01855. 00000 - "AM/A.M. or PM/P.M. required"
*Cause:
*Action:
SELECT to_char( TO_timestamp('01-FEB-18 01.21.01.645','dd-mon-yy hh12.mi.ss.ff'),'dd-mm-yyyy hh24:mi:ss')
from dual;
Your PERFORMED_TIMESTAMP is already a timestamp, which doesn't have any intrinsic human-readable format - Oracle uses its own internal representation when storing the values.
You are just seeing that timestamp displayed by your client with formatting taken from the session NLS settings (or, possibly, overridden by the client itself).
The error is because you are doing unnecessary data type conversions and relying on implicit conversion and NLS session settings. When you do
TO_DATE(PERFORMED_TIMESTAMP,'DD-MON-YYYY hh:mi:ss AM')
you are first implicitly converting the PERFORMED_TIMESTAMP to a string, again using your session NLS settings, so you're effectively doing:
TO_DATE(TO_CHAR(PERFORMED_TIMESTAMP),'DD-MON-YYYY hh:mi:ss AM')
which given the string values in your question is actually:
TO_DATE(TO_CHAR(PERFORMED_TIMESTAMP, 'DD-MON-RR HH.MI.SS.FF AM'),'DD-MON-YYYY hh:mi:ss AM')
The inner part of that will give a string like "01-FEB-18 01.00.21.645000000 PM" which is what you see when you query the table and the client does its own formatting. Passing that string back into to_date() gives the error you see, because the fractional seconds are appearing in the string where it's expecting to see the AM/PM marker:
SELECT TO_DATE('01-FEB-18 01.00.21.645000000 PM','DD-MON-YYYY hh:mi:ss AM')
FROM DUAL;
Error report -
ORA-01855: AM/A.M. or PM/P.M. required
You could replace the implcit conversion and session values with explicit conversion and format masks:
TO_CHAR(TO_DATE(TO_CHAR(PERFORMED_TIMESTAMP, 'DD-MON-YYYY hh:mi:ss AM'),'DD-MON-YYYY hh:mi:ss AM'), 'DD-MM-YYYY hh24:mi:ss')
But hopefully you can tell that is doing a lot more work than it needs to -and having to use the same format mask twice is also a sign that you're doing something wrong.
The real solution is just to simplify it. You don't need to convert to a string and back to a date at all. Just do:
SELECT TO_CHAR(PERFORMED_TIMESTAMP, 'DD-MM-YYYY HH24:MI:SS')
FROM FACT_WORK_ITEM_ACTION
WHERE PERFORMED_TIMESTAMP >= TIMESTAMP '2018-06-05 00:00:00';
Notice that I've also removed the trunc() and the comparison with a string; this now compares your timestamp column as a timestamp, which will make any index on that happier too.
Quick demo with a CTE to provide dummy data:
with FACT_WORK_ITEM_ACTION(PERFORMED_TIMESTAMP) as (
select timestamp '2018-06-01 13:00:21.645000000' from dual
union all select timestamp '2018-06-06 13:00:21.645000000' from dual
)
SELECT TO_CHAR(PERFORMED_TIMESTAMP, 'DD-MM-YYYY HH24:MI:SS')
FROM FACT_WORK_ITEM_ACTION
WHERE PERFORMED_TIMESTAMP >= TIMESTAMP '2018-06-05 00:00:00';
TO_CHAR(PERFORMED_T
-------------------
06-06-2018 13:00:21

What date format use in sqlite

I have a sqlite table within a column type DATE. Why doesn't this query work? I save my date in this format: dd-MM-YYYY
SELECT * FROM scadenze where data < strftime ('%d-%m-%Y', '31-03-2017') ;
This is not one of the supported date formats. Better use yyyy-mm-dd.

How can I convert datetime to date format in SQLite?

I want to show a date in DD-MM-YYYY format in a datagrid. By default, SQLite stores the data in date-time format. So how can I convert date-time format to date format in flex by using SQLite?
You can use strftime, from Date And Time Functions.
Example:
SELECT strftime('%d-%m-%Y', 'now')
output:
10-06-2010
Look up Java until Date. It has methods to convert and is supported in SQLite. However it is mostly deprecated and replaced with Calender.

Resources