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

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.

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:

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

to_date conversion issue for '00000000'

I have a date field in format of 20140525 (yyyymmdd).
I use TO_DATE(20140528,'yyyymmdd') to convert date to 05/28/2014, however this date field also contains '00000000' values which will cause the TO_DATE function to error out due to the invalid month of '00' (and presumably invalid day error as well)
How can I use to_date to convert the valid date formats while ignore the invalid '00000000' values? I still want to retain the dates of '00000000' in the query
First, that's why it's a bad idea to store date information in anything other than a date data type. If your data types are correct, life is generally much easier. By storing dates as character strings, you're using more space and getting less value.
If the one and only case where you have non-convertable data is this 00000000 value, you could
(CASE WHEN column = '00000000'
THEN cast(null as date)
ELSE to_date( column, 'yyyymmdd' )
END)
In the vast majority of situations, though, once you have an incorrect data type, you're going to end up with more incorrect values. Someone is going to enter a date that doesn't exist (i.e. '20140431') which will also cause your to_date function to fail. To handle that, you'd generally need a function that catches and ignores the exception, i.e.
CREATE OR REPLACE FUNCTION my_to_date( p_str IN VARCHAR2,
p_format IN VARCHAR2 )
RETURN DATE
IS
BEGIN
RETURN to_date( p_str, p_format );
EXCEPTION
WHEN value_error
THEN
RETURN null;
END;
TO_DATE(REPLACE(date, '00000000'), 'YYYYMMDD')

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