I have a columnn of timestamps stored as text with no spaces, hyphens, slashes or decimal points, e.g. 20140328160335880258. I want to convert this text to a timestamp in Teradata (v15).
If cut off the microseconds, the following works:
SELECT CAST('20140328160335' AS TIMESTAMP(0) FORMAT 'yyyymmddhhmiss')
However, I can't find a format string that allows the partial seconds to be included in the timestamp:
SELECT CAST('20140328160335880258' AS TIMESTAMP(6) FORMAT 'yyyymmddhhmiss')
> SELECT Failed. 6760: Invalid timestamp
SELECT CAST('20140328160335880258' AS TIMESTAMP(6) FORMAT 'yyyymmddhhmissssssss')
> SELECT Failed. 3350: Invalid FORMAT string
I've tried 'yyyymmddhhmiss.ssssss', 'yyyymmddhhmiss.s(6)', 'yyyymmddhhmisss(6)' and 'yyyymmddhhmissffffff', but all are invalid.
Is the only option to insert a decimal point into the text version of the timestamps?
There's no way using Teradata's FORMAT without adding a separating period.
But there's also TO_CHAR:
TO_TIMESTAMP('20140328160335880258', 'yyyymmddhh24missff6')
Related
i have the followoing format of a timestamp which is saved as string.
2022-09-15T11:07:28.872
I've tried to convert it to timestamp like this
PARSE_TIMESTAMP("%Y-%m-%d-%H:%M:%S",JSON_VALUE(DATA,'$.payload.onHoldUntil'))
but i get the following error
Mismatch between format character '-' and string character 'T'
How can i solve this issue ?
The format string you provide in the PARSE_TIMESTAMP function should align to the format of your string. In your case it does not as you are missing a T and the milliseconds.
Try the following:
select '2022-09-15T11:07:28.872' as s_ts
, PARSE_TIMESTAMP('%FT%R:%E3S', '2022-09-15T11:07:28.872')
In Postgres, I'm trying to do a date/time based query in my WHERE predicate.
When I try to select with this date/time format SQL error says the value needs to be an integer. I'm not sure why it does not think my minute of 17 is not an integer or why it only see it as a 1 and not a 17?
SELECT *
FROM history
WHERE create_time > TO_TIMESTAMP('2018-10-08T23:17:44.728','yyyy-MM-dd''T''HH:mm:ss.SSS');
ERROR: invalid value ":1" for "HH"
DETAIL: Value must be an integer.
SQL state: 22007
You're trying to consume a date value that contains a T, and it looks like you're trying to declare to TO_TIMESTAMP that the T is a literal value to be ignored. Problem is you're doing this by putting 'T' (apostrophe-T-apostrophe, escaped) which is bumping the parser on by 3 characters and it is then encountering ':1' from 23:17 when it is expecting HH:
--your date, and underneath it, the format you gave
2018-10-08T23:17:44.728
yyyy-MM-dd'T'HH:mm:ss.SSS
Can you see how the HH aligns (vertically) with :1? Postgres is complaining that it was expecting an integer that it could parse to 23, but it encountered the string :1 which isn't an integer.
This question:
Postgres- have to_timestamp() ignore/not read a specific character in middle of date/time string
Implies you can put a space in the format where the T is, or just cast the string you have to a Timestamp - postgres can apparently parse that string as a Timestamp without you having to literally lay the format out for it explicitly
Try:
SELECT *
FROM history
WHERE create_time > TIMESTAMP '2018-10-08T23:17:44.728'
SELECT *
FROM history
WHERE create_time > cast('2018-10-08T23:17:44.728' as timestamp)
SELECT *
FROM history
WHERE create_time > TO_TIMESTAMP('2018-10-08T23:17:44.728','yyyy-MM-dd HH:mm:ss.SSS');
You might even find this works:
SELECT *
FROM history
WHERE create_time > TO_TIMESTAMP('2018-10-08T23:17:44.728', 'yyyy MM dd HH mm ss SSS')
The numbers align with the format fields and space is used for everything else you want to ignore (hyphens, colons, dots etc)
The problem is due to using ''T'' which's before HH, and DB signals that, you might use
TO_TIMESTAMP('2018-10-08 23:17:44.728','yyyy-mm-dd HH24:MI:SS.MS')
instead.
My table in hive has a filed of date in the format of '2016/06/01'. but i find that it is not in harmory with the format of '2016-06-01'.
They can not compare for instance.
Both of them are string .
So I want to know how to make them in harmory and can compare them. Or on the other hand, how to change the '2016/06/01' to '2016-06-01' so that them can compare.
Many thanks.
To convert date string from one format to another you have to use two date function of hive
unix_timestamp(string date, string pattern) convert time string
with given pattern to unix time stamp (in seconds), return 0 if
fail.
from_unixtime(bigint unixtime[, string format]) converts the
number of seconds from unix epoch (1970-01-01 00:00:00 UTC) to a
string representing the timestamp of that moment in the current
system time zone.
Using above two function you can achieve your desired result.
The sample input and output can be seen from below image:
The final query is
select from_unixtime(unix_timestamp('2016/06/01','yyyy/MM/dd'),'yyyy-MM-dd') from table1;
where table1 is the table name present in my hive database.
I hope this help you!!!
Let's say you have a column 'birth_day' in your table which is in your format,
you should use the following query to convert birth_day into the required format.
date_Format(birth_day, 'yyyy-MM-dd')
You can use it in a query in the following way
select * from yourtable
where
date_Format(birth_day, 'yyyy-MM-dd') = '2019-04-16';
Use :
unix_timestamp(DATE_COLUMN, string pattern)
The above command would help convert the date to unix timestamp format which you may format as you want using the Simple Date Function.
Date Function
cast(to_date(from_unixtime(unix_timestamp(yourdate , 'MM-dd-yyyy'))) as date)
here is my solution (for string to real Date type):
select to_date(replace('2000/01/01', '/', '-')) as dt ;
ps:to_date() returns Date type, this feature needs Hive 2.1+; before 2.1, it returns String.
ps2: hive to_date() function or date_format() function , or even cast() function, cannot regonise the 'yyyy/MM/dd' or 'yyyymmdd' format, which I think is so sad, and make me a little crazy.
I'm trying to parse 2015-07-09T12:22:29 using TO_DATE using the following date format YYYY-MM-DDTHH:MI:SS, but I'm getting back the message as the date format is not recognized.
You have to put the T in double quotes, like YYYY-MM-DD"T"HH:MI:SS.
select to_date( '2015-07-09T12:22:29', 'YYYY-MM-DD"T"HH:MI:SS' ) from dual;
TO_DATE('
---------
09-JUL-15
Here's the relevant documentation. (Thanks Alex)
Be aware that the "T" in your date string indicates that it is in UTC format, not as standard date. Just ignoring the "T" character is not going to result in a correct datetime value, as you are not accounting for your local timezone.
Check out this post for some additional information:
Convert timestamp/date time from UTC to EST Oracle SQL
I'm sure this is quite simple, but I've been stuck on it for some time. How can I convert a varchar field (YYYYMM) to a date (MM/01/YY) in SQL?
Thanks.
Edit: I'm using Open Office Base (HSQL), not MySQL; sorry for the confusion.
Try the str_to_date and date_format functions. Something like:
select date_format( str_to_date( my_column, '%Y%c' ), '%c/01/%y' ) from my_table
try :
SELECT STR_TO_DATE(CONCAT(myDate,'01'),'%Y%m%d')
FROM myTable
Use STR_TO_DATE:
From mysql.com:
STR_TO_DATE(str,format)
This is the inverse of the DATE_FORMAT() function. It takes a string str and a format string format. STR_TO_DATE() returns a DATETIME value if the format string contains both date and time parts, or a DATE or TIME value if the string contains only date or time parts.
The date, time, or datetime values contained in str should be given in the format indicated by format. For the specifiers that can be used in format, see the DATE_FORMAT() function description. If str contains an illegal date, time, or datetime value, STR_TO_DATE() returns NULL. Starting from MySQL 5.0.3, an illegal value also produces a warning.
Range checking on the parts of date values is as described in Section 11.3.1, “The DATETIME, DATE, and TIMESTAMP Types”. This means, for example, that “zero” dates or dates with part values of 0 are allowed unless the SQL mode is set to disallow such values.
mysql> SELECT STR_TO_DATE('00/00/0000', '%m/%d/%Y');
-> '0000-00-00'
mysql> SELECT STR_TO_DATE('04/31/2004', '%m/%d/%Y');
-> '2004-04-31'
Get the year:
SUBSTRING(field FROM 2 FOR 2)
Get the month:
SUBSTRING(field FROM -2 FOR 2)
Compose the date:
CONCAT(SUBSTRING(field FROM -2 FOR 2), '/01/', SUBSTRING(field FROM 2 FOR 2))
This will convert from YYYYMM to MM/01/YY.
To be clear: if you're looking for method to convert some value of type Varchar/Text to value of type Date than solutions are:
using CAST function
CAST(LEFT('201205',4)||'-'||SUBSTRING('201205' FROM 5 FOR 6)||'-01' AS DATE)
starting from OpenOffice 3.4 (HSQLDB 2.x) new Oracle-like function TO_DATE supposed to be available
TO_DATE('201205','YYYYMM')
in addition to the written i can mention that you also can construct a string with ANSI/ISO 'YYYY-MM-DD' formatted representation of the date,- Base will acknowledge that and succesfully convert it to the Date type if necessary (e.g. INSERTing in Date typed column etc.)
Here is doc's on HyperSQL and highly recommended OO Base guide by Andrew Pitonyak