conversion from string to particular date format in sql - sql

trunc(to_date('datefield1','mm/dd/yyyy hh24:mi:ss'))
between
trunc(to_date(to_char('05/18/2016 08:57','mm/dd/yyyy hh24:mi:ss'),'mm/dd/yyyy,hh24:mi:ss'))
and
trunc(to_date(to_char('05/20/2016 08:57','mm/dd/yyyy hh24:mi:ss'),'mm/dd/yyyy,hh24:mi:ss'));
I need to convert the string '05/18/2016 08:57' into date

Why are you converting a character string to a character string? You can do:
trunc(to_date(datefield1, 'mm/dd/yyyy hh24:mi:ss'))
between trunc(to_date('05/18/2016 08:57', 'mm/dd/yyyy hh24:mi:ss'), 'mm/dd/yyyy,hh24:mi:ss') and
trunc(to_date('05/20/2016 08:57', 'mm/dd/yyyy hh24:mi:ss'), 'mm/dd/yyyy hh24:mi:ss');
It is very strange to have hh24:mi:ss when the string itself has no seconds. But Oracle allows it.
That said, I would write the logic as:
datefield1 >= date '2016-05-18' and
datefield1 < date '2016-05-21'

Related

Unable to convert string to Timestamp

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

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)

PLSQL Convert String to Datetime

The 'closed_date' column and 'submit_date' are loaded into Oracle as strings, they look like this:
8/17/2017 12:41 (in 24hrs)
How can I convert this string format into date in the format of
mm/dd/yyyy hh24:mi:ss
Thank you!
Given your date format, you don't want seconds:
select to_date(CLOSED_DATE, 'mm/dd/yyyy hh24:mi') as CLOSED_DATE,
to_date(SUBMIT_DATE, 'mm/dd/yyyy hh24:mi') as SUBMIT_DATE
from s_daily_ops
Tack on a trailing ':00', or remove ':ss' from your format string.

search date and time in oracle using to_char

In oracle, when I search using below query, it is fetching wrong records (check the attached screenshot), can someone suggest the correct format for 12 hour time.
to_char(a.created, 'MM/DD/YYYY HH12:MI:SS') >='05/23/2012 12:00:00'
Thanks,
Kiran.
Don't search based on a string. Search based on a date. If you search on a string, you'll get string comparison semantics which are not what you want. The string '06/01/1900' is alphabetically after the string '05/23/2012' despite the date that it represents being much earlier.
a.created >= to_date('05/23/2012 12:00:00', 'mm/dd/yyyy hh24:mi:ss' )
or using a 12-hour clock
a.created >= to_date('05/23/2012 03:15:00 pm', 'mm/dd/yyyy hh:mi:ss am' )