Parse string to datetime in BigQuery - google-bigquery

I'm trying to parse a string to a date
SELECT PARSE_DATE("2021-02-03T07:19:49.661Z", '%Y-%m-%dT%H:%M:%S.000Z')
But it refuses to work:
"Mismatch between format character '2' and string character '%'"
I know it's likely to do with the second argument, but I can't find any examples.

You need to make the format string the first argument, then fix the milliseconds part:
SELECT PARSE_DATETIME('%Y-%m-%dT%H:%M:%E*SZ', "2021-02-03T07:19:49.661Z")

Related

timstamp from string to timestamp in BigQuery

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')

I have a Date in a String format and I can't convert it to date in BigQuery

I started with a date in a string format from a JSON extraction using this: json_value(answer, '$.date_created') and got an output 2020-01-02T10:26:47.056-04:00.
From there, I transformed the output (because I couldn't change it to date using a series of functions like regexp_replace, left and CAST) to a date-like string: 2020-01-02 10:26:47
I need to be able to transform this new string to a date. So far, I've tried with FORMAT_DATETIME and FORMAT_TIMESTAMP but I'm getting an error: Failed to parse input string bigquery
Your original timestamp string is just fine to do this:
select Date(timestamp("2020-01-02T10:26:47.056-04:00"))
Only thing here to check is: you have -4 offset from UTC so as long as you take care of timezone etc, above style should work fine.

How to delete "" and change it to date format

My dataset's date column (tweet_stamp) is shown as "2020-01-29 00-21-29" and it is STRING format.
I would like to have result as 2020-01-29.
How to delete "" in string and change it to date format?
I tried code as below
select to_date(from_unixtime(unix_timestamp(tweet_timestamp,'"yyyy-MM-dd 00-00-00"'), 'yyyy-MM-dd')) as tweet_date;
However, result is NULL.
You do not need unix_timestamp+from_unix_time conversion because date part of string is already in right format. Just remove double quotes, get substring and optionally convert to date:
select date(substr(regexp_replace('"2020-01-29 00-21-29"','"',''),1,10)) --returns 2020-01-29
Or even simpler using to_date function:
select to_date(regexp_replace('"2020-01-29 00-21-29"','"','')) --2020-01-29

Getting null while converting string to date in spark sql

I have dates in the format '6/30/2020'. It is a string and I want to convert it into date format.
List of methods I have tried
Cast('6/30/2020' as date) #returns null
to_date('6/30/2020','yyyy/MM/dd') #returns null
I also tried splitting the string and then concatenating it into data.
After trying all this and putting all the possible combinations in the to_date function, I am still getting the answer as null.
Now I am confused as I have used all the functions to convert string to date.
Thanks in advance for your help.
The date format you used was incorrect. Try this:
select to_date('6/30/2020', 'M/dd/yyyy')
If you want to format your result, you can use date_format:
select date_format(to_date('6/30/2020', 'M/dd/yyyy'), 'yyyy/MM/dd')
Note that to_date converts a given string from the given format, while date_format converts a given date to the given format.

Unable to convert varchar2 to Date in oracle

I am trying to convert one of the varchar2 column to date in oracle using the below query.
SELECT *
FROM login
WHERE to_date(END_DATE,'DD-MM-YY') < to_date(TRUNC(SYSDATE)-90,'DD-MM-YY');
I am converting the both side to date with a common formatter. But still I am getting the below error while executing this query.
ORA-01861: literal does not match format string
01861. 00000 - "literal does not match format string"
*Cause: Literals in the input must be the same length as literals in
the format string (with the exception of leading whitespace). If the
"FX" modifier has been toggled on, the literal must match exactly,
with no extra whitespace.
*Action: Correct the format string to match the literal
Can you please help me to sort out this problem?
to_date(END_DATE,'DD-MM-YY')
First of all, it is a bad design to store DATE as STRING. Date should always be stored as DATE data type, there is no reason to store it as characters.
If your data is stored as 14-Mar-2015 then why are you using the 'DD-MM-YY' format. Clearly the formats doesn't match. You should use proper format model.
For example,
TO_DATE(14-Mar-2015,'DD-Mon-YYYY','NLS_DATE_LANGUAGE=ENGLISH')
to_date(TRUNC(SYSDATE)-90,'DD-MM-YY')
This makes no sense. TRUNC on DATE would return you DATE after truncating the time portion.
Never ever use TO_DATE on DATE. It will implicitly convert it into string and then back to date using locale-specific NLS format. See a detailed explanation in my previous answer here https://stackoverflow.com/a/29559609/3989608
Your modified query would look like:
SELECT *
FROM login
WHERE to_date(END_DATE,'DD-Mon-YYYY','NLS_DATE_LANGUAGE=ENGLISH') < TRUNC(SYSDATE)-90;