How can I convert 12 hour format hours into 24 hour format time in ORACLE SQL like 01-FEB-18 01.00.21.645000000 PM should be 05-01-0018 13:12:44 - oracle11g

there are multiple timestamps column in oracle Sql , Now in want to convert 12 hour format time into 24 hour format time like 01-FEB-18 01.00.21.645000000 PM should be 05-01-0018 13:12:44. but I need to convert hours into 24 hours time format , I am using the below statement.
SELECT TO_CHAR(TO_DATE(PERFORMED_TIMESTAMP,'DD-MON-YYYY hh:mi:ss AM'),'DD-MM-YYYY hh24:mi:ss')
FROM FACT_WORK_ITEM_ACTION
WHERE TRUNC(PERFORMED_TIMESTAMP)>= '05-JUN-18'
But still i am getting the below error .
Error Code....
ORA-01855: AM/A.M. or PM/P.M. required
01855. 00000 - "AM/A.M. or PM/P.M. required"
*Cause:
*Action:

SELECT to_char( TO_timestamp('01-FEB-18 01.21.01.645','dd-mon-yy hh12.mi.ss.ff'),'dd-mm-yyyy hh24:mi:ss')
from dual;

Your PERFORMED_TIMESTAMP is already a timestamp, which doesn't have any intrinsic human-readable format - Oracle uses its own internal representation when storing the values.
You are just seeing that timestamp displayed by your client with formatting taken from the session NLS settings (or, possibly, overridden by the client itself).
The error is because you are doing unnecessary data type conversions and relying on implicit conversion and NLS session settings. When you do
TO_DATE(PERFORMED_TIMESTAMP,'DD-MON-YYYY hh:mi:ss AM')
you are first implicitly converting the PERFORMED_TIMESTAMP to a string, again using your session NLS settings, so you're effectively doing:
TO_DATE(TO_CHAR(PERFORMED_TIMESTAMP),'DD-MON-YYYY hh:mi:ss AM')
which given the string values in your question is actually:
TO_DATE(TO_CHAR(PERFORMED_TIMESTAMP, 'DD-MON-RR HH.MI.SS.FF AM'),'DD-MON-YYYY hh:mi:ss AM')
The inner part of that will give a string like "01-FEB-18 01.00.21.645000000 PM" which is what you see when you query the table and the client does its own formatting. Passing that string back into to_date() gives the error you see, because the fractional seconds are appearing in the string where it's expecting to see the AM/PM marker:
SELECT TO_DATE('01-FEB-18 01.00.21.645000000 PM','DD-MON-YYYY hh:mi:ss AM')
FROM DUAL;
Error report -
ORA-01855: AM/A.M. or PM/P.M. required
You could replace the implcit conversion and session values with explicit conversion and format masks:
TO_CHAR(TO_DATE(TO_CHAR(PERFORMED_TIMESTAMP, 'DD-MON-YYYY hh:mi:ss AM'),'DD-MON-YYYY hh:mi:ss AM'), 'DD-MM-YYYY hh24:mi:ss')
But hopefully you can tell that is doing a lot more work than it needs to -and having to use the same format mask twice is also a sign that you're doing something wrong.
The real solution is just to simplify it. You don't need to convert to a string and back to a date at all. Just do:
SELECT TO_CHAR(PERFORMED_TIMESTAMP, 'DD-MM-YYYY HH24:MI:SS')
FROM FACT_WORK_ITEM_ACTION
WHERE PERFORMED_TIMESTAMP >= TIMESTAMP '2018-06-05 00:00:00';
Notice that I've also removed the trunc() and the comparison with a string; this now compares your timestamp column as a timestamp, which will make any index on that happier too.
Quick demo with a CTE to provide dummy data:
with FACT_WORK_ITEM_ACTION(PERFORMED_TIMESTAMP) as (
select timestamp '2018-06-01 13:00:21.645000000' from dual
union all select timestamp '2018-06-06 13:00:21.645000000' from dual
)
SELECT TO_CHAR(PERFORMED_TIMESTAMP, 'DD-MM-YYYY HH24:MI:SS')
FROM FACT_WORK_ITEM_ACTION
WHERE PERFORMED_TIMESTAMP >= TIMESTAMP '2018-06-05 00:00:00';
TO_CHAR(PERFORMED_T
-------------------
06-06-2018 13:00:21

Related

How do i subtract time from datetime in snowflake?

pdt.startTime is datetime
s_first.FromTimeOfDay is a time
I want to subtract the time drom the datetime. When i run the code below, Snowflake gives me this error invalid type [CAST(S_FIRST.FROMTIMEOFDAY AS TIMESTAMP_NTZ(9))] for parameter 'TO_TIMESTAMP_NTZ'
select (pdt.StartTime - (SELECT s_first.FromTimeOfDay::datetime FROM Shift s_first))
from RAW_CPMS_AAR.POWERBI_DowntimeTable AS PDT
When i try this:
select (pdt.StartTime::TIMESTAMP_NTZ(9) - (SELECT s_first.FromTimeOfDay::TIMESTAMP_NTZ(9) FROM Shift s_first))
from RAW_CPMS_AAR.POWERBI_DowntimeTable AS PDT
I get more or less the same error: invalid type [CAST(S_FIRST.FROMTIMEOFDAY AS TIMESTAMP_NTZ(9))] for parameter 'TO_TIMESTAMP_NTZ'
How do I convert the time into a datetime format so that I can subtract the two. It doesnt seem to me that there is a clear way to convert time into datetime in snowflake.
Is this what you're after?
select current_timestamp() as sample_timestamp
, time(sample_timestamp) as sample_time
, date(sample_timestamp) as sample_date;
A user pointed me in the right direction. i didnt realize i could use "dateadd" to also subtract time.
dateadd(HOUR, - (HOUR(current_timestamp())), temp.DateTime)

Inavlid timestamp Teradata

I have Ship_Date as 12/14/2013 20:27 and defined as varachar datatype in the source table.How can I convert this to timestamp format and load as is 12/14/2013 20:27?I'm using CAST (SHIP_DATE AS TIMESTAMP(0) FORMAT 'MM/DD/YYYYHH:MI:SS') AS SHIP_DATE but terdata is throwing invalid timestamp error.Please help in resolving the issue
You were missing the B indicating a blank/space in your original cast. But Teradata casts chokes on a single digit month. You can add a leading zero using a RegEx:
Cast(RegExp_Replace(SHIP_DATE,'\b([\d])\b', '0\1') AS TIMESTAMP(0) FORMAT'dd/mm/yyyyBhh:mi')
select to_timestamp('12/14/2013 20:27','MM/DD/YYYY HH24:MI');
The problem with your cast as you have it written is you are telling Teradata you have seconds in your string when you don't. You can use:
select cast ('12/14/2014 20:27' as TIMESTAMP(0) FORMAT 'MM/DD/YYYYBHH:MI')
However, this still won't handle single digit months.
The issue here is the empty space between 2013 and 20 and the missing zeros for the seconds.
I did oreplace to remove the space and concatenation and it worked.
SELECT
CAST (
( OREPLACE('12/14/2013 20:27', ' ','') ||':00')
AS TIMESTAMP(0) FORMAT 'MM/DD/YYYYHH:MI:SS'
) AS SHIP_DATE
And the result is below:

In Teradata, trying to convert Timestamp(6) to timestamp(0)

A is in the format of timestamp(6). I need it in timestamp(0). The code I am using is the following:
cast(cast(A AS date) as timestamp(0))
FROM 'table'
where A >= '?StartDT'
After inputing the date I want for the parameter I get the 'Invalid timestamp' error.
If A is truly a Timestamp(6) then casting it first as a DATE will affectively trim off the time elements, so when you cast the result to a TIMESTAMP(0) you are going to end up with a time of 00:00:00.
You'll need to also cast the TIMESTAMP(6) field as a time and then add the results together like:
CAST(CAST(A AS DATE) AS TIMESTAMP(0)) + (CAST(A AS TIME(6)) - TIME '00:00:00' HOUR TO SECOND)
You can also use SUBSTRING() to snip off the last 6 characters of the TIMESTAMP(6) field and cast that resulting string to a TIMESTAMP(0):
CAST(SUBSTRING(CAST(A AS CHAR(26)) FROM 1 FOR 19) AS TIMESTAMP(0))
This doesn't address the INVALID TIMESTAMP error you are getting though. Are you certain that field A is a TIMESTAMP(6) and not a VARCHAR() that looks like a Timestamp? What happens when you remove the outer cast, are their any dates in the result that look like they wouldn't convert nicely to a timestamp? Something is not quite right here, and I suspect that it's in your data.

oracle 10g convert date column with timestamp to dd-mon-yyyy format

Hi am trying to convert column defined as date to date with format DD-MON-YYYY but can't get it working
select
to_date(to_char
(DESIGN_COMPLETION_DATE,'DD-MON-YYYY'),'DD-MON-YYYY') as Assessment_Completion_Date
from nbi_dates
Also Tried
Select to_date(DESIGN_COMPLETION_DATE,'DD-MON-YYYY') as Assessment_Completion_Date
from nbi_dates
What works is, but I can't do any calculations on it as it is char
Select to_char(DESIGN_COMPLETION_DATE,'DD-MON-YYYY') as Assessment_Completion_Date
from nbi_dates
Thanks
If you have a DATE that includes a time portion but are only interested in the actual date part, you can use the trunc function:
select trunc(design_completion_date) as assessment_completion_date from nbi_dates
An example of the difference using sysdate; notice the time on the trunc'd version has been set to midnight:
SQL> alter session set nls_date_format = 'DD/MM/YYYY HH24:MI:SS';
Session altered.
SQL> select sysdate, trunc(sysdate) from dual;
SYSDATE TRUNC(SYSDATE)
------------------- -------------------
11/04/2013 15:14:31 11/04/2013 00:00:00
A DATE has no inherent format. DD-MON-YYYY is a format mask applied to display the date, or to convert it to a string representation, which is usually only necessary for display anyway. What you have as your third option is right for that purpose, but not if you want to do any further date calculations with the result.
Select to_date(to_char(DESIGN_COMPLETION_DATE,'DD-MON-YYYY'),'DD-MON-YYYY') as Assessment_Completion_Date
from nbi_dates
or simply (in case you want date object for calculations but not for rendereing)
Select DESIGN_COMPLETION_DATE as Assessment_Completion_Date
from nbi_dates

How to enter in DateTime in Oracle

Some scope creep occured in the last couple of days and now I have to squeeze in date time. The Oracle stored procedure has a Date field (same with the table). Every time I try to enter in the date time value I get this refrain from the exception thrower:
ORA-01830: date format picture ends before converting entire input string
ORA-06512: at line 56
Here is what I try to enter:
SPECIALIST_APPT_DATETIMEIN := '09/Sep/1990 00:00:00'
Here is the param definition I try to squeeze it into:
PCP_APPOINTMENT_DATETIME`in `DATE`
It looks like you just need to use the TO_DATE function to convert the string to a date.
SPECIALIST_APPT_DATETIMEIN := to_date('09/Sep/1990 00:00:00',
'DD/MON/YYYY HH24:MI:SS' );
assuming that you intend to enter times in the 24-hour format (i.e. 17:30:00 for 5:30pm).

Resources