Select date from between two timestamps - sql

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.

Related

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!!

01830. 00000 - "date format picture ends before converting entire input string"

select to_date('13/03/17 05:43:29,000000000 PM -05:00DD/MM/YY HH24:MI:SS') from
irregularities;
How to convert this date to 24-hour format?
You can convert a string to a timestamp with time zone using:
select to_timestamp_tz('13/03/17 05:43:29,000000000 PM -05:00',
'DD/MM/RR HH:MI:SS,FF9 AM TZH:TZM')
from dual;
If you only want a date data type then you can cast it:
select cast(
to_timestamp_tz('13/03/17 05:43:29,000000000 PM -05:00',
'DD/MM/RR HH:MI:SS,FF9 AM TZH:TZM')
as date)
from dual;
If you really only want the string version you can convert it back, which you would usually only do for display:
select to_date(
to_timestamp_tz('13/03/17 05:43:29,000000000 PM -05:00',
'DD/MM/RR HH:MI:SS,FF9 AM TZH:TZM'),
'DD/MM/YYYY HH24:MI:SS')
from dual;
If the original string is coming from a table then just replace the text literal with the column name, and dual with your table name. Of course, that assumes the column is actually a string. If it is actually already a timestamp and your client is just displaying it in a way you don't like, you only need theto_char() part.
Read more about these things in the documentation: to_timestamp_tz, format models, cast() and to_char().

Oracle, finding data between two different dates

I have a table name as business_details and column name business_date whose data type is varchar2.
Now i have to find out the data between two different dates and date format like : 12-JUN-18 21:15:13
Means, 12 Jun, 2018.
Kindly help me to write a query which can fetch the data between these two dates :12-JUN-18 21:15:13 and 25-JUN-18 18:15:32
I assume that in table business_details you have an column date or something like that.
Than use something like this:
select business_date from business_details
where date between TO_DATE ('12-JUN-18 21:15:13','dd-MM-yy hh:mi:ss')
AND TO_DATE ('25-JUN-18 18:15:32','dd-MM-yy hh:mi:ss');
Assuming your business_date is actually a string in the format you've shown (and it isn't really a date your client is just showing in that format), you need to convert that to a date type, as well as converting the string literals.
select *
from business_details
where to_date(business_date, 'DD-MON-RR HH24:MI:SS')
between to_date('12-JUN-18 21:15:13', 'DD-MON-RR HH24:MI:SS')
and to_date('25-JUN-18 18:15:32', 'DD-MON-RR HH24:MI:SS');
The format model you tried to use in a comment did this:
to_date('12-JUN-18 21:15:13', 'DD-MM-YYYY HH24:MI:SS')
is using MM rather than MON, which works anyway by default - although using month numbers is safer anyway as they aren't dependent on your session language. But more importantly it uses YYYY. If you pass a 2-digit value like 18 and try to convert with YYYY you get the wrong year:
select to_date('12-JUN-18 21:15:13', 'DD-MON-YYYY HH24:MI:SS') form dual;
TO_DATE('12-JUN-182
-------------------
0018-06-12 21:15:13
In your version your business_date was being converted implicitly so would use NLS settings, which are presumably using RR already. But that means you were comparing a date in 2018 with a range in 0018, which is why nothing matched.
You could also use timestamp literals for the fixed values (unless those strings are actually being passed in from somewhere else):
select *
from business_details
where to_date(business_date, 'DD-MON-RR HH24:MI:SS')
between cast(timestamp '2018-06-12 21:15:13' as date)
and cast(timestamp '2018-06-25 18:15:32' as date);

Using TO_DATE() with AM/PM Formatting

I am trying to select some dates from a table where the format of the dates is like this:
14-APR-14 10.35.00.0000000000 AM
01-NOV-16 02.43.00.0000000000 PM
Note that the dates can be either AM or PM, but when I try to do a simple SELECT from the table such as:
SELECT * FROM MyTable
WHERE TO_DATE(MyDate, 'DD-MON-YYYY HH:MI:SS AM') > '31-DEC-2016 08:00:00 AM';
I get the error:
ORA-01855: AM/A.M. or PM/P.M. required
I've been trying to get this work for some time but with no luck. Any help here would be appreciated.
Several problems.
Your inputs are obviously strings, since they have ten decimal places and timestamps in Oracle have at most 9. Then, strings with fractions of a second can't be converted to a date with to_date - you need to use to_timestamp or else you need to remove all the fractional parts. In the solution below I only remove the last (the tenth) decimal, since you may have non-zero fractional parts in the table - although not in the sample you posted.
Then, your format mask has yyyy but your inputs have only two digits for the year (which probably means 93 means 1993 and not 2093, so the correct thing to use would be rr rather than yy). And you use : in the format mask where your inputs use .
Finally, don't even compare dates in string format: in string comparisons, 01-JAN-2015 is before 20-NOV-2013.
You probably want something like this:
select mydate
from (
select '14-APR-14 10.35.00.0000000000 AM' as mydate from dual
union all
select '01-NOV-16 02.43.00.0000000000 PM' from dual
) mytable
where to_timestamp(substr(mydate, 1, 28) || substr(mydate, -3), 'dd-MON-rr hh.mi.ss.ff AM')
> to_timestamp('31-DEC-2016 08:00:00 AM', 'dd-MON-yyyy hh:mi:ss AM');
This query compiles correctly, and it produces no rows in the output (for obvious reasons).
NOTE: In a comment you (the OP) say the mydate field is a timestamp(6) datatype. Hard to believe (you show ten decimal places), but if indeed it is a timestamp or date, then you don't need to wrap it within any to_timestamp or to_date function, it should stand alone in the left-hand side of the inequality.
From your comment:
It's actually a timestamp; not a string. Timestamp(6) to be precise
You can just use a TIMESTAMP literal:
SELECT *
FROM MyTable
WHERE MyDate > TIMESTAMP '2016-12-31 08:00:00';

Oracle to_date SQL formatting odd behavior

I've trawled the SO website thinking this is pretty obvious but alas I have not found one yet.
Basically the setup is as follows:
I have a string in a table like so:
06/22/2010 00:00:00
I do a select on it:
SELECT To_Date(item, 'MM/DD/YYYY HH24:MI:SS') from that_table
so that effectively I am doing
SELECT To_Date('06/22/2010 00:00:00', 'MM/DD/YYYY HH24:MI:SS') from that_table
My database returns:
22.06.2010 00:00:00
Now I know I can do a
to_char(to_Date('06/22/2010 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 'MM/DD/YYYY HH24:MI:SS')
but I need the select to return a date format (MM/DD/YYYY HH24:MI:SS), not a string. How can I do that?
Edit:
Real output:
22.06.2010 00:00:00
Desired output (in Date format):
06/22/2010 00:00:00
A DATE doesn't have a format, it is only formatted when converted to a character string for display purposes. If you are using SQL Plus then when you display a DATE column (without using TO_CHAR to explicitly format it), SQL Plus itself performs a TO_CHAR conversion using the NLS_DATE_FORMAT setting:
SQL> select sysdate from dual;
SYSDATE
---------
23-JUN-10
SQL> alter session set nls_date_format = 'YYYY-MM-DD HH24:MI:SS';
Session altered.
SQL> select sysdate from dual;
SYSDATE
-------------------
2010-06-23 15:17:12
If you are not using SQL Plus, then whatever tool you are using will be doing something similar and may have a different way to specify the date format for display.
a Date is a point in time, it has no format (like numbers: they don't have format). When you display the date, obviously it has to get a format: if you don't specifically give Oracle a format, the default format for your session will be used (session parameter NLS_DATE_FORMAT).
In consequence: use the DATE datatype for date arithmetics and storage (no format). Use the to_char function or something equivalent when you need to display the date (in a report or in the GUI).