Convert string into timestamp in Hive - datetime

I have a value '2017-09-27T19:25:15.927-07:00', is there any way to convert this into a timestamp?
I use Hive 1.1.0.
select unix_timestamp("2017-09-27T19:25:15.927-07:00", "yyyy-MM-ddTHH:mm:ss.SSSX") but it trows Bad date/time conversion format
select unix_timestamp("2017-09-27T19:25:15.927-07:00", "yyyy-MM-ddTHH:mm:ss.SSSZZZ") but it returns NULL

The format is yyyy-MM-dd'T'HH:mm:ss.SSSXXX".Note the single quotes surrounding 'T'
select from_unixtime(unix_timestamp("2017-09-27T19:25:15.927-07:00", "yyyy-MM-dd'T'HH:mm:ss.SSSXXX"))

Related

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:

I am trying to find what operator should be used to extract datetime from Timestamp. The Timestamp column is showing results like 0x0000000002CAE4C0

What operator can be used to pull DateTime from a datetime Data_type. The column name is Timestamp which is showing result like
Timestamp
0x0000000002CAE4C0
0x0000000002CC022C
0x0000000002CB5982
0x0000000002CB84AD
0x0000000002CBD9EF
0x0000000002CC52E9
I tried using
Select cast (Timestamp as nvarchar(20)) as testcastDatetime
, convert (nvarchar,Timestamp,103) as testconvertDatetime
both the above operators are not working. I got an error message
Operand type clash: timestamp is incompatible with nvarchar

How to convert VARCHAR DATE to Date Format in teradata 15?

I loaded data from csv file with fastload in Tera Data Express 15. In csv file my ModifiedDate format is 6/12/2004 0:00 and in fastload script my Date type is varchar
I create a new table now I want to load data from one table to another table
How to convert varchar date to date format?
You can use a regular expression to add missing leading zeroes before casting to a timestamp:
Cast(RegExp_Replace(start_date, '\b([0-9])\b', '0\1') AS TIMESTAMP(0) Format 'dd/mm/yyyyBhh:mi')
Of course an easier way would be using TPT (instead of legacy FastLoad) which supports such formats out of the box (VarDate).

CAST() not changing to datetime format

I am using this SQL query to convert my _Submission_date column which of type nvarchar(max) to datetime format:
SELECT
CAST(PSCData._SUBMISSION_DATE AS DATETIME2)
FROM
PSCData
I have tried every possible way but still its giving this error:
Conversion failed when converting date and/or time from character string.
The data inside my _Submission_Date is in this form:
"2017-8-21 21:13:55.00000"
"2017-9-21 14:13:55.00000"
When I run this query it works fine:
SELECT CAST('2017-08-25' AS DATETIME);
but with this format :
SELECT CAST('2017-9-21 14:13:55.00000' AS DATETIME);
I get the same error mentioned above.
Any suggestions that how I can solve this?
Found the solution. The actual problem was the double quotes around the string which was causing the error it can be solved like this:
update PscData
SET _SUBMISSION_DATE = REPLACE(_SUBMISSION_DATE,'"', '')
select CAST(PSCData._SUBMISSION_DATE as DATETIME2)
FROM PSCData
i.e: first use the REPLACE method to remove double quotes around the string than you can use the cast method and can easily convert the "varchar" type data to "datetime" format without any problem. Plus u have to use "datetime2" in the cast method as "datetime" will not work with it.
Thanks for the help btw :)

DB2 Cast DateTime string with "T" separator

Help me please with the next problem.
I have date time string in the next format(ISO 8601): 1999-12-31T23:59:59
and I need to cast it to TIMESTAMP value. The main problem in 'T' separator character.
I have tried next query:
SELECT TIMESTAMP_FORMAT('1999-12-31T23:59:59','YYYY-MM-DD HH24:MI:SS') FROM ROMAN.EMPLOYEE;
and use different format strings, such as, YYYY-MM-DDTHH24:MI:SS, YYYY-MM-DD"T"HH24:MI:SS.
Could you provide me correct way to cast this type of strings without any character replacement and substrings.
Thanks in advance!
There is no built-in function that can format a timestamp in ISO-8601 format in DB2 for Linux/UNIX/Windows.
As you have probably surmised, you can do this with REPLACE:
select
TIMESTAMP_FORMAT(REPLACE('1999-12-31T23:59:59','T',' '), 'YYYY-MM-DD HH24:MI:SS')
from
ROMAN.EMPLOYEE;
It's trivial to create a user defined function (UDF) to handle this formatting for you as well so you don't have to out this long string in every query.
It may also be possible to do it via XQuery with and xs:dateTime, although this would be even more code than just embedding REPLACE in the call to TIMESTAMP_FORMAT.

Resources