Bigquery cannot parse datetime from a date in spite of providing right format. Dates in my column are of the same format I used in the string:
SELECT EXTRACT(DATE FROM PARSE_TIMESTAMP('%Y-%m-%dT%H:%M:%S%z',"2021-03-22T14:00:00-03:00"))
This gives this error:
Failed to parse input string "2021-03-22T14:00:00-03:00"
I am trying to make this work by this answer
You should use %Ez instead of %z as in below
SELECT EXTRACT(DATE FROM PARSE_TIMESTAMP('%Y-%m-%dT%H:%M:%S%Ez',"2021-03-22T14:00:00-03:00"))
See Supported format elements for TIMESTAMP for more details
Related
Edited: Want to convert the format.
I am kinda new in BigQuery, recently I was working on a project. I want to convert above type of format into yyyy/mm/dd format. How will I do that?
You can combine PARSE_DATE with FORMAT_DATE to get the desired output:
SELECT FORMAT_DATE("%Y/%m/%d",PARSE_DATE("%B %d, %Y","June 10, 2014")) AS date_str
PARSE_DATE will parse the provided string into a DATE type value in BQ format (YYYY-MM-DD) and then FORMAT_DATE will format this DATE to a string with the desired output.
In case someone is wondering how to convert the whole column;
FORMAT_DATE("%Y/%m/%d",PARSE_DATE("%B %d, %Y",ColumnName)) as DesiredColumnName
I'm trying to cast the results of an entire column in BigQuery in MM/DD/YYYY format
Time_Column
2022-05-26T17:32:41.000Z
2022-05-28T06:34:23.000Z
Results:
We can try to use FORMAT_DATE function to make it, we can refer to this link Supported Format Elements For DATE to use your expected date format from the datetime type.
SELECT FORMAT_DATE("%m/%d/%Y", Time_Column)
FROM T
Based on the example mentioned here in the Snowflake documentation, why are the date and timestamp values returning different values just by changing the ORDER BY clause? Also, I am trying to convert a string to a date format which is not returning correct results in Snowflake while this works fine in other SQL based Engines. Need help from experts on this.
This query
SELECT '20200710', TO_DATE('20200710');
is returning the following output
20200710 | 1970-08-22
Also tried:
SELECT TO_DATE('20200710', 'YYYY-MM-DD');
and got the error:
Can't parse '20200710' as date with format 'YYYY-MM-DD'
To convert to a date data type, you would use:
SELECT TO_DATE('20200710', 'YYYYMMDD')
I would recommend just keeping the date data type. But if you want a string in the format YYYY-MM-DD:
SELECT TO_CHAR(TO_DATE('20200710', 'YYYYMMDD'), 'YYYY-MM-DD')
So I have a varchar that I am converting to a DateTime and I'm using the below syntax but I get the following error in Snowflake "Timestamp '23-Jan-2015 23:02:39' is not recognized". Since I'll be getting my data in this format what do you suggest?
select '23-Jan-2015 23:02:39'::timestamp_tz;
Your timestamp string literal is not in a standard format which Postgres can directly use. Instead, you may use TO_TIMESTAMP to convert:
SELECT TO_TIMESTAMP('23-Jan-2015 23:02:39', 'DD-Mon-YYYY HH24:MI:SS');
In the demo tool I used, this returned: 2015-01-23 23:02:39+00
Demo
You need to let snowflake know how to make sense of the literal that you are passing.
Use the to_timestamp() to instruct it to convert to a timestamp
Give it a way to map the day-month-year while it parses the literal to map it to values
select to_timestamp('23-Jan-2015 23:02:39', 'DD-MON-YYYY HH24:MI:SS');
Please check the supported Date ,Time , Timestamp etc formats supported by Snowflake here:
https://docs.snowflake.com/en/user-guide/date-time-input-output.html#date-formats
I have a table with the following data:
logs.ip logs.fecha logs.metodo
66.249.93.79 19/Nov/2018:03:46:33 GET
All data columns are string and I want to convert logs.fecha into date with the following format: YYYY-MM-dd HH:mm:ss
I try the following query:
SELECT TO_DATE(from_unixtime(UNIX_TIMESTAMP(fecha, 'yyyy-MM-dd'))) FROM logs
Results of the query are NULL in all rows.
How can I make the conversion string to date for all rows? I know I must use ALTER TABLE but I don't know how to do it.
Thanks
The reason you get null is because the format of the input string is different from the input passed to unix_timestamp. The second argument to unix_timestamp should specify the string format of the first argument. In from_unixtime you can specify the output format desired. If nothing is specified, a valid input to from_unixtime returns an output in yyyy-MM-dd format.
The error can be fixed as below.
from_unixtime(unix_timestamp(fecha,'dd/MMM/yyyy:HH:mm:ss'),'yyyy-MM-dd HH:mm:ss')
You just have to tell Oracle the date format you are reading with TO_DATE.
Try:
SELECT TO_DATE(fecha,'DD/MON/YYYY:HH:MI:SS') FROM logs