How to enter in DateTime in Oracle - oracle11g

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

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)

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

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

TeradataSQL: Time to String, Add to Date and Compare to Another Time and Data

I'm trying to figure out the cleanest way to do a comparison in Teradata SQL Assistant. I have the scheduled start date (TimeStamp), the Schedule start time (varchar), actual start and end times (TimeStamp). I need to consolidate the scheduled start date and time and be able to compare it to the actual start and end date and time without modifying the original data (because it's not mine). I realize that the Scheduled Start Time [SST] is in a 24 hour time format with a AM/PM suffix, but like I said before, I can't change that.
I tried to do select cast(substr(scheduled_start_date,1,5) as TIMESTAMP(0)) from DB.TBL but am getting the "Invalid timestamp" error. There is example table data below.
Sch Start Date Sch Start Time Actual Start Actual End
09/11/2017 00:00:00 11:30 AM 09/11/2017 11:34:16 09/11/2017 11:58:00
05/26/2017 00:00:00 15:30 PM 05/26/2017 15:40:00 05/26/2017 15:55:15
11/06/2017 00:00:00 19:30 PM 11/06/2017 21:25:00 11/06/2017 21:45:00
Thanks!
You need to cast the schedule start time as an Interval, then you can easily add it to the start date:
scheduled_start_date
+ Cast(Substr(scheduled_start_time, 1,5) AS INTERVAL HOUR TO MINUTE)
A start date which is a timestamp seems to indicate this was ported from Oracle/SQL Server?
And a 24 hour time format with a AM/PM suffix is also quite strange.
A couple things to try:
Convert the separate Scheduled Date and Scheduled Time fields into strings, concatenate them, and feed that into a TIMESTAMP CAST. Something like:
SELECT
CAST(CAST(Scheduled_Date AS DATE) AS VARCHAR(25)) AS Date_String,
CAST(CAST(Scheduled_Time AS TIME FORMAT 'HH:MM BB') AS VARCHAR(25)) AS Time_String,
CAST(TRIM(Date_String) || ' ' || TRIM(Time_String) AS TIMESTAMP(0)) AS MyTimestamp
Cast the Scheduled Time field as a TIME data type. Cast the Scheduled Date field as a DATE data type. Then somehow combine the two into a TIMESTAMP field -- either with a CAST or some kind of timestamp constructor function (not sure if this is possible)
Option 1 should work for sure as long as you properly format the strings. Try to avoid using SUBSTRING and instead use FORMAT to cast as DATE/TIME fields. Not sure about Option 2. Take a look at these link for how to format DATE/TIME fields using the FORMAT clause:
https://www.info.teradata.com/HTMLPubs/DB_TTU_16_00/index.html#page/SQL_Reference%2FB035-1143-160K%2Fmuq1472241377538.html%23wwID0EPHKR
https://www.info.teradata.com/HTMLPubs/DB_TTU_16_00/index.html#page/SQL_Reference/B035-1143-160K/cmy1472241389785.html
Sorry, I don't have access to a TD system to test it out. Let me know if you have any luck.

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.

SQLite3 DateTime comparison in linux

As per seen in image my first query return 5 rows but my second query does not return any rows.
It shoud be return 3 rows.
I also have tried with
Store my all datetime data in format of 'yyyy-MM-dd hh:mm:ss'
"SELECT billheaderid,billheadercode,billtotalitem,billtotalamount,createdby,createdon WHERE cretedon >= Datetime('2014-08-19 12:26:32')"
Date values with "AM/PM" fields cannot be compared correctly with string comparisons
(1 is larger than 0).
You have to change all the values in the database to the correct format yyyy-MM-dd hh:mm:ss.
(And it is not necessary to call the datetime function.)
Store your data in form of
'yyyy-MM-dd hh:mm:ss'
And please care that '2014-08-19 03:45 PM' must be store as '2014-08-19 15:45:23' not as '2014-08-19 03:45:23'.
After that you don't need use datetime function. I am sure it'll work 100%.

Resources