search date and time in oracle using to_char - sql

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' )

Related

SQL Date Time Format including Time

Need help with Oracle SQL Formatting. I have the below code which outputs the data (MINNEEDTIME) in DD-MM-YY format. Need to change this to MM/DD/YYYY HH:MI AM format. Tried TO_CHAR for converting and that threw an error. Any assistance will be appreciated. Thank you
min(case when ve.REASON in ('Need Time', 'WAITING Time') then ve.EVENT_DTIME end) as MINNEEDTIME
You can use TO_CHAR(<date>, <format>) as in:
select to_char(current_date, 'MM/DD/YYYY HH:MI AM') from dual
In your case replace ve.EVENT_DTIME with:
to_char(ve.EVENT_DTIME, 'MM/DD/YYYY HH:MI AM')
In Oracle, a DATE is a binary data type consisting of 7 bytes that represent: century, year-of-century, month, day, hour, minute and second. It ALWAYS contains those components and it is never stored with any human-readable format.
What you are seeing is the client application you are using to access the database receiving the (unformatted) binary data and trying to be helpful and applying its own format to the date.
You need to either:
Explicitly convert the value from a DATE to a string and apply a format using the TO_CHAR function:
TO_CHAR(
min(
case
when ve.REASON in ('Need Time', 'WAITING Time')
then ve.EVENT_DTIME
end
),
'MM/DD/YYYY HH12:MI AM'
) as MINNEEDTIME
Or, you need to change how the client application you are using formats dates. You can either look for the settings/preferences in the application or some applications (for example, SQL*Plus or SQL Developer) will use the NLS_DATE_FORMAT session parameter which you can change using:
ALTER SESSION SET NLS_DATE_FORMAT = 'MM/DD/YYYY HH:MI AM';

SQL Query Error: date format picture ends before converting entire input into string

I'm getting an Error when I run the query below:
to_date('30-APR-19 09.53.35.000000 AM', 'DD-Mon-yy hh24.mi.ss')
Date format picture ends before converting entire input into string
Can I get an assistance please
The major problem you've got is that your date-and-time string can't be parsed using TO_DATE - you'll need to use TO_TIMESTAMP. The issue is that TO_DATE doesn't recognize the FFn format specifier, which is used to process fractional seconds. This makes sense because DATE values are only accurate to the second. So you'll need to use
TO_TIMESTAMP('30-APR-19 09.53.35.000000 AM', 'DD-MON-YY HH.MI.SS.FF6 AM')
Which will return a TIMESTAMP value. If you really need this to be a DATE rather than a TIMESTAMP you can cast the value to DATE by using
CAST(TO_TIMESTAMP('30-APR-19 09.53.35.000000 AM', 'DD-MON-YY HH.MI.SS.FF6 AM') AS DATE)
dbfiddle here
You can directly use to_date function and miliseconds can be ignored using # as following:
to_date('30-APR-19 09.53.35.000000 AM', 'DD-MON-YY HH.MI.SS.###### AM')
Number of # is equal to number of 0s after dot(.)
db<>fiddle demo
Cheers!!

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 search for the records between specific CREATE_DATE range

I have time stamp in the CREATE_DATE column of my table in the below format.
9/8/2016 5:37:35 AM
I need to search the records between specific CREATE_DATE range. Please help me on how to do the same.
I have used the below query but getting
ORA-01843: not a valid month
select *
from gdf.msg_pyld
where dbms_lob.instr(pyld_clob,'4861615654')>=1
and CREATE_DATE BETWEEN '09/08/2016 5:59:17 AM' AND '09/08/2016 5:59:17 PM';
What you tried is a comparison of a date (a numeric type with a very special meaning) against STRINGS. This will never work.
If CREATE_DATE is indeed a DATE in Oracle, then this should work:
... AND CREATE_DATE BETWEEN to_date('09/08/2016 5:59:17 AM', 'mm/dd/yyyy hh:mi:ss AM')
AND to_date('09/08/2016 5:59:17 PM', 'mm/dd/yyyy hh:mi:ss PM')
assuming 09/08 means September 8; if instead it is supposed to mean 9 August, then change mm/dd to dd/mm in the format models. Good luck!

Select date from between two timestamps

I am facing the following problem.
I have a database with a table which saves Dates (with its time).
Now I would like to know all the tables information where the date is in between two timestamps, but I am getting the following error:
01830. 00000 - "date format picture ends before converting entire input string".
What I did so far is this query:
SELECT * FROM ARBEITSBLOCK WHERE STARTZEIT BETWEEN '30.11.2015 19:00:00'
and '01.12.2015 19:05:00';
And this which doesn't give me any result but there should be:
SELECT * FROM ARBEITSBLOCK
WHERE TO_CHAR(STARTZEIT,'DD.MM.YYYY H24:MM:SS') BETWEEN '30.11.2015 13:00:00'
and '01.12.2015 19:05:00';
Try this statement (using Oracle syntax)
SELECT *
FROM ARBEITSBLOCK
WHERE STARTZEIT BETWEEN TO_DATE ('12/04/2015 09:00:00 AM', 'mm/dd/yyyy hh:mi:ss AM')
AND TO_DATE ('12/04/2015 10:00:00 AM', 'mm/dd/yyyy hh:mi:ss AM');
If STARTZEIT is a DATE column, then why are you trying to compare it to a string?
By doing that, you are relying on Oracle being able to say "aha! This string is really a date, so I will attempt to convert it for you!". That's all well and good, but how will Oracle know how the date-in-the-string is formatted?
Well, there's the nls_date_format parameter which is defaulted to 'DD-MON-RR', and I think you can now see why you're getting the "date format picture ends before converting entire input string" error, since 'DD-MON-RR' is a lot shorter than '30.11.2015 19:00:00'.
Instead of relying on this implicit conversion and the bugs that go right along with that (as you've discovered!), you should explicitly convert the string into a date, which you can easily do with the to_date() function.
E.g.:
select *
FROM ARBEITSBLOCK
WHERE STARTZEIT BETWEEN to_date('30.11.2015 19:00:00', 'dd.mm.yyyy hh24:mi:ss')
and to_date('01.12.2015 19:05:00', 'dd.mm.yyyy hh24:mi:ss');
Oracle does not store dates in the format you see. It stores it internally in 7 bytes with each byte storing different components of the datetime value.
You must use TO_DATE with proper FORMAT MODEL to explicitly convert the literal to DATE.
SELECT *
FROM ARBEITSBLOCK
WHERE STARTZEIT BETWEEN
TO_DATE('30.11.2015 19:00:00', 'DD.MM.YYYY HH24:MI:SS')
AND
TO_DATE('01.12.2015 19:05:00', 'DD.MM.YYYY HH24:MI:SS');
Remember, the DATE data type has both date and time elements, TIMESTAMP is an extension to DATE data type.