Presto epoch string to timestamp - presto

Require your help as stuck in time conversion in presto.
I have a epoch column with name timestamp as a string datatype and i want to convert this into date timestamp.
I have used the below query after reading through various blogs:
SELECT date_parse(to_iso8601(from_unixtime(CAST(timestamp AS bigint)) AS date ,
'%Y-%m-%dT%H:%i:%s.%fZ'))
FROM wqmparquet;
Everytime i run this query i get an error:
INVALID_FUNCTION_ARGUMENT: Invalid format: "2020-04-27T19:49:50.000Z" is malformed at "T19:49:50.000Z"
Can somebody please help me on this.

I might be oversimplifying this, but if you want to convert an epoch string to a timestamp datatype, you can just do:
from_unixtime(cast(timestamp as bigint))
You can generate a timestamp with time zone by passing a second argument to from_unixtime(), as a time zone string.

Related

varchar yy/mm/dd to date azure sql server

Im trying to convert a varchar value into a date format.
The varchar value is 99/01/30and the result should be 1999-01-30.
Would somebody help me with this?
I'm trying to do this but :
select convert(date, convert(varchar, '99/01/30', 11),23) as date
but this gives me an error: Conversion failed when converting date and/or time from character string.
This
select convert(date, '99/01/30', 2) as date
will get you what you want. See also this page for more information about what the 2 means

How to get year from timestamp in presto sql?

I have the following query and would like to extract the year from login_time. login_time is in timestamp format (i.e. 2019-03-12 08:51:35.000)
Select year(from_unixtime(cast(login_time as int))) from profile limit 100
There is an error - cannot cast timestamp to integer when I run this query. Does anyone know what's the reason for it? Would appreciate any advice!
There is an error - cannot cast timestamp to integer
This suggests the login_time is of timestamp data type, so you should just call year() on it:
year(login_time)

Azure Data Factory Mapping Data Flow: Epoch timestamp to Datetime

I have a JSON-based source I'd like to transform using ADF Mapping Data Flow. I have a string containing an epoch timestamp value that I want to transform to Datetime value to later sink it into Parquet file.
Do you know a way? Docs of this language are here.
Source file:
{
"timestamp":"1574127407",
"name":"D.A."
}
Use toTimestamp() and set the formatting you wish as 2nd parameter
toTimestamp(1574127407*1000l)
From string:
toTimestamp(toInteger(toString(byName('timestamp')))*1000l,'yyyy-MM-dd HH:mm:ss')
I have came across various epoch timestamp values which are of 13 digits i.e., they even have milliseconds detailed information.
In such case, converting to integer using 'toInteger' won't serve the purpose instead this will keep the values as NULL. So, to fix this issue, we need to convert it to Long using toLong as below:
toTimestamp(toLong(toString(created)),'yyyy-MM-dd HH:mm:ss')
In above expression, 'created' is a field whose value is 13-digit epoch timestamp, something like this created='1635359043307'.
Here, toTimestamp returns the Date Timestamp with above-mentioned date format.
FYI, you can use this site https://www.epochconverter.com/ to check epoch timestamp to human date.

Timestamp casting makes value null

When cast the column datatype from string to timestamp the value becomes null.
I have values in the following format
20070811T00789.167861+0100
I want to cast the type to "timestamp", when i do the following
df.withColumn('arrivetime', df['arrivetime'].cast('timestamp'))
the value is becoming null. How to cast the column to timestamp without affecting the value and its format?
I dont' know exactly what format you are going for with the 5 digits for time and the 6 (nano seconds?) at the end, but do know that timestamps in Spark are milliseconds, not nanoseconds, so you are going to lose information.
That being said, you can use Spark's unix_timestamp method to convert strings to timestamps using the SimpleDateFormat syntax.
First you probably have to get rid of the last 3 digits of the timestamp, by using Spark's regexp_replace
In Scala that would look like:
regexp_replace(df("arrivetime"), """(\.\d{3})\d*""", """$1""")
Then you could use the unix_timestamp like so:
unix_timestamp([replaced string], "yyyyMMdd'T'HHmmss.SSSz")

Oracle: How to convert a string into date format

Have done a lot of search before asking this question, like How to convert a string into date format, How to convert a string into date, but still can't figure it out.
So, here's the question, how to convert these string dates into date in Oracle:
"2016-08-15 10:45:30" (String type) -> 20160815 (Date type)
"20160815104530" (String type) -> 20160815 (Date type)
Any idea will be appreciated.
You are mixing two things here:
The first is the conversion of a String data type, in Oracle VARCHAR2 into a DATE data type.
The DATE data type has a precision of seconds, you can't change that. A DATE data type will always give you the date and time component, i.e year, month, day, hours, minutes and seconds: Oracle SQL Data Type Documentation
However, the second part of what you are asking is about how to format the date when retrieved. This is helpful when running reports, or other kinds of visual display of dates. For example, in the US you would most likely want your date columns appear in the format MM/DD/YYYY while everywhere else in the world you most likely want to stick with DD/MM/YYYY. Oracle lets you do that by telling it what NLS_DATE_FORMAT you want to use. You can set that parameter for each individual session as well as on database level, it is up to you (and your DBA) to decide where and when you want to set that. In your case you can apply this via the ALTER SESSION command:
SQL> ALTER SESSION SET nls_date_format='YYYY-MM-DD';
Session altered.
SQL> SELECT TO_DATE('2016-08-15 10:45:30', 'YYYY-MM-DD HH24:MI:SS') FROM DUAL;
TO_DATE(
----------
2016-08-15
SQL> SELECT TO_DATE('20160815104530', 'YYYYMMDDHH24MISS') FROM DUAL;
TO_DATE(
----------
2016-08-15
You use to_date():
select to_date(substr(str1, 1, 10), 'YYYY-MM-DD')
select to_date(substr(str2, 1, 8), 'YYYYMMDD')

Resources