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';
Related
I've tried multiple solutions, but I keep getting errors. I need to create a new column casting VARCHAR to TIMESTAMP that includes AM, PM or -ideally- changes it to 24 hrs format.
VARCHAR format (Start_Date column): 8/3/2022 4:58:49 PM
I found the below solution is some other post, but I'm getting error: 'Format code appears twice'
SELECT itab.*,
TO_TIMESTAMP(Start_Date, 'MM/DD/YYYY HH:MM:SS AM') AS start_TS
FROM db.info_table itab
Please advise.
You have two problems.
MI is the format for minutes, MM is for months (you have it twice, this is why you are getting that error).
Your date/time string has single digit values for month, day, etc. You can use a pretty simple regex for that.
select to_timestamp(regexp_replace('8/3/2022 4:58:49 PM', '\b([0-9])\b', '0\1'), 'MM/DD/YYYY HH:mi:SS AM')
TO_TIMESTAMP returns a TIMESTAMP(6). If you don't want microseconds you can specify the precision using
CAST(RegExp_Replace(start_date, '\b([0-9])\b', '0\1') AS timestamp(0) FORMAT 'MM/DD/YYYYbHH:Mi:SSbT')
All you need is pad day and month in Teradata (as opposed to Oracle etc). m/d/y format has not been implemented.
select '8/3/2022 4:58:49 PM' date1,
to_timestamp(
lpad(strtok(date1,'/',1),2,'0')||'/'||lpad(strtok(date1,'/',2),2,'0')||'/'||strtok(date1,'/',3)
,'mm/dd/yyyy hh24:mi:ss AM'
);
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);
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.
I've the following query and I encounter the error : not a valid month. Help please!
SELECT * FROM TABLE1
WHERE Field1 = '12345' AND
TO_DATE('4/28/2014 12:00:00 AM') BETWEEN TO_DATE(DATE1) AND TO_DATE(DATE2);
Thanks
You need to specify the format mask for the date conversion. It looks like your NLS_DATE_FORMAT is maybe DD/MM/YYYY, with or without a time part. You're passing the date in a different format. You should never rely on the NLS settings really, supply the format mask for TO_DATE() in the query:
SELECT *
FROM TABLE1
WHERE Field1 = '12345'
AND TO_DATE('4/28/2014 12:00:00 AM', 'MM/DD/YYYY HH:MI:SS AM')
BETWEEN DATE1 AND DATE2;
I'm assuming that DATE1 and DATE2 are already of type DATE, in which case there is no need to call TO_DATE; all that will do is convert them to and back from a string using the NLS_DATE_FORMAT, which is either pointless or will do the same as TRUNC(), depending on how that is set.
Of course, the date you're using here is at midnight, so specifying the time isn't adding anything; if you will never have a tie part then you could do either:
AND TO_DATE('4/28/2014', 'MM/DD/YYYY') BETWEEN DATE1 AND DATE2;
or
AND DATE '2014-04-28' BETWEEN DATE1 AND DATE2;
If field1 is numeric then you shouldn't have quotes around 12345 either, you're just adding an implicit TO_NUMBER().
But if you're storing numbers and/or dates as strings, you should really think again. Hopefully that is not in fact what you're doing...
Months for TO_DATE need to be two digits so you would need:
SELECT * FROM TABLE1 WHERE Field1 = '12345' AND TO_DATE('04/28/2014 12:00:00 AM') BETWEEN TO_DATE(DATE1) AND TO_DATE(DATE2);
Reference http://www.techonthenet.com/oracle/functions/to_date.php for more info if you haven't already. Having a format mask could help define the string in case changes are made in the future.
I want to print the timestamp from the below sql
select to_date('01/01/2011 12:00:00 AM','dd/mm/yyyy hh:mi:ss AM') from dual;
current output --> 1/1/2011 (not printing the timestamp only for 12 am. if the min is 12:01 then it is printing.
but I need the output as 1/1/2011 12:00:00 AM
TO_DATE converts a string to a DATE. A DATE is stored in a packed binary format that is not human readable. An Oracle DATE does not have a format. So when you ask a program to display a date, it has to then convert the DATE to a string. If you don't explicitly specify the format by doing an explicit TO_CHAR, a tool like SQL*Plus will convert the date to a string using the session's NLS_DATE_FORMAT. Other applications may choose different ways to convert a date to a string-- using the client's regional settings, for example, or by allowing the user to configure the format.
If you want to return a string in a particular format that represents a DATE, you'd need to use an explicit TO_CHAR. Something like
SELECT to_char( some_date_column, 'dd/mm/yyyy hh:mi:ss AM' )
FROM some_table
In the specific case you posted, since you have the string in your hand as a string, you'd simply want to select it from dual rather than doing a TO_DATE to convert it to a date and then a TO_CHAR to convert it back to a string. I'm assuming, though, that you have an actual DATE in the actual table that you are trying to select from.
The best way to control the formatting is to use to_char and explicitly specify the date format you want.
select to_char(to_date('01/01/2011 12:00:00 AM','dd/mm/yyyy hh:mi:ss AM'),'DD/MM/yyyy hh:mi:ss AM')
from dual;
You can Use
select Convert(varchar,'01/01/2011 12:00:00 AM',113)