Unable to convert string to Timestamp - sql

I'm trying to convert a sample 12 hour date with AM/PM into a timestamp, however, Snowflake is throwing an error that it's not parsable:
SELECT TO_TIMESTAMP('7/16/2021 4:52:25 AM', 'MM/dd/yyyy HH12:mm:ss AM')
The error message returned:
Can't parse '7/16/2021 4:52:25 AM' as timestamp with format 'MM/dd/yyyy HH12:mm:ss AM'
I've tried the hours as both HH and HH12 to no avail.

If you want to extract only the time part you can use the below statement:
SELECT TO_TIME(TO_TIMESTAMP('7/16/2021 4:52:25 AM', 'MM/dd/yyyy HH12:mi:ss AM'))
If you want to convert it to a timestamp format, you can use the below statement:
SELECT TO_TIMESTAMP('7/16/2021 4:52:25 AM', 'MM/dd/yyyy HH12:mi:ss AM')
PFB the documentation links for date extract:
https://docs.snowflake.com/en/sql-reference/functions/date_part.html
https://docs.snowflake.com/en/sql-reference/functions/date_trunc.html

Related

Oracle Query With (BETWEEN Two Dates) Returns Empty Result Set

I have an Oracle SQL Query I am trying to run, but it keeps returning null value for the second query below but when I use the first query it returns a value. Please can someone help me check what I may be doing wrong in query 2? Note that the date column is in the Format 21/09/2020 10:00:00 AM and I want to get all records from 22-SEP-2020 11:00:00 AM to the current datetime for query 2. I am implementing query 2 for a Service, reason why I want to stick to it.
SELECT query_date from Users where query_date > TO_DATE('21-SEP-2020 10:00:00 AM', 'DD-MON-YYYY HH:MI:SS AM')
SELECT query_date from Users where query_date BETWEEN TO_DATE('21-SEP-2020 10:00:00 AM', 'DD-MON-YYYY HH:MI:SS AM') AND TO_DATE('22-SEP-2020 5:46:00 PM', 'DD-MON-YYYY HH:MI:SS PM')
If you have to use BETWEEN, why not use sysdate?
WHERE query_date BETWEEN TO_DATE('21-SEP-2020 10:00:00 AM', 'DD-MON-YYYY HH:MI:SS AM') AND sysdate

problem with using to_date in oracle query

i simply wanna change a string to a date format using to_date
SELECT TO_DATE('20-APR-20 09.50.06 AM' , 'DD-MOM-YY HH24:MI:SS AM') FROM DUAL;
and also i want to change to 24 format
when i run this i get the ORA-01821: date format not recognized error .
The correct format for converting your string to a date is:
SELECT TO_DATE('20-APR-20 09.50.06 AM' , 'DD-MON-YY HH.MI.SS AM')
FROM DUAL;
If you want it as a string, then you can use TO_CHAR() after converting to a date. That said, I recommend keeping the value as a date.
The correct format is
SELECT TO_CHAR(TO_DATE('20/APR/20 09.50.06 AM' , 'DD-MON-YY HH:MI:SS AM'),'DD-MON-YY HH:MI:SS AM') FROM DUAL;

Oracle - to_date() parse of 00:00:00

I am trying to format into Date from Date string by following the way,
select to_date('11/19/2019 00:00:00', 'MM/DD/YYYY HH24:MI:SS') as currentdate from dual;
The output should be,
11/19/2019 12:00:00 AM
But, I am getting the output as,
11/19/2019
When I execute following,
select to_date('11/19/2019 00:00:01', 'MM/DD/YYYY HH24:MI:SS') as currentdate from dual;
The correct output I am getting,
11/19/2019 12:00:01 AM
My nls_date_format is DD-MON-RR.
My nls_time_format is HH.MI.SSXFF AM.
I want output in the mentioned format. For all other values except than 00:00:00 is working fine.
Why 00:00:00 is not converting into the required format? Is there any way to achieve this?
You are getting the correct output. Only it is displayed in the default date format of your session (or database), which seems to be mm/dd/yyyy.
You control the default date format of your session with paramter nls_date_format:
alter session set nls_date_format = 'MM/DD/YYYY HH24:MI:SS';
select to_date('11/19/2019 00:00:00', 'MM/DD/YYYY HH24:MI:SS') as currentdate from dual;
Alternatively, you can use to_char() to format your date to a specific format:
select to_char(
to_date('11/19/2019 00:00:00', 'MM/DD/YYYY HH24:MI:SS'),
'MM/DD/YYYY HH24:MI:SS'
) as currentdate from dual;
If you want the AM format you will need this:
select to_char(to_date('19/11/2019 12:00:00 AM','dd/mm/yyyy hh:mi:ss AM'),'dd/mm/yyyy hh:mi:ss AM')
from dual;
If you run it like this:
select to_char(to_date('19/11/2019 00:00:00','dd/mm/yyyy hh:mi:ss AM'),'dd/mm/yyyy hh:mi:ss AM')
from dual;
You will get an error.
When your date format is not 24 hours(hh24) then your value for hour needs to be between 1 and 12.
Here is the DEMO
After some research I have realised what OP wants(from his comments and after he entered some more details).
Solution for OP is:
Step 1:
ALTER session SET NLS_DATE_FORMAT = 'DD-MON-RR HH.MI.SS AM';
Step 2 now this will work:
select to_date('11/19/2019 00:00:00', 'MM/DD/YYYY HH24:MI:SS') as currentdate from dual;

Oracle querying date and time

I am trying to get data from a table where start_date is less than or equal to a particular date and time value:
SELECT * FROM Table1 WHERE START_DATE <= TO_DATE('2/21/2018 2:40:20 PM', 'MM/dd/yyyy hh:mm:ss tt')
The error I am getting is format code appears twice
I have tried different formats but still cannot get it right
You have two problems.
1) The format model for minutes is "mi", not "mm".
2) The format model for AM/PM is "AM", not "tt".
So,
TO_DATE('2/21/2018 2:40:20 PM', 'MM/dd/yyyy hh:mi:ss AM')
Or, easier,
TO_DATE('2/21/2018 14:40:20', 'MM/dd/yyyy hh24:mi:ss')
(i.e., a 24-hour clock)

How to convert time stored in varchar2 to 24-hour format in Oracle?

I have time as HH:MI:SS AM/PM stored in a varchar2 column in a table. How can I convert this to 24-hour format?
To convert to a DATE:
to_date(<text field>, 'DD/MM/YYYY HH:MI:SS AM')
To convert to another string:
to_char(to_date(<date field>, 'DD/MM/YYYY HH:MI:SS AM'), 'DD/MM/YYYY HH24:MI:SS')
e.g. (with NLS_DATE_FORMAT set to YYYY-MM-DD HH24:MI:SS):
select to_date('09/08/2013 5:13:07 PM', 'DD/MM/YYYY HH:MI:SS AM'),
to_char(to_date('09/08/2013 5:13:07 PM', 'DD/MM/YYYY HH:MI:SS AM'),
'DD/MM/YYYY HH24:MI:SS')
from dual;
TO_DATE('09/08/2013 TO_CHAR(TO_DATE('09
------------------- -------------------
2013-08-09 17:13:07 09/08/2013 17:13:07
If you only have the time portion:
select to_date('5:13:07 PM', 'HH:MI:SS AM'),
to_char(to_date('5:13:07 PM', 'HH:MI:SS AM'), 'HH24:MI:SS')
from dual;
TO_DATE('5:13:07PM' TO_CHAR(
------------------- --------
2013-08-01 17:13:07 17:13:07
Notice that if you don't provide the date part of the value it defaults to the first day of the current month (mentioned in the documentation for datetime literals; but if you only have the time you probably want to keep it as a string anyway.
Use this: 2013-08-01 17:13:07 17:13:07
Notice that if you don't provide the date part of the value it defaults to the first day of the current month (mentioned in the documentation for datetime literals), but if you only have the time you probably want to keep it as a string anyway.