Converting string to timestamp issues sql - sql

I'm trying to cast a string to timestamp but I'm getting the following error:
Failed to output to file. Query failed: Value cannot be cast to timestamp: 2020-03-23T05:17:44.000Z
I'm using the query below:
select CAST(purchase_date AS timestamp)
from main_table

You can use from_iso8601_timestamp function if timestamp with time zone type is acceptable.
Or, you can use date_parse function.

Try parse_datetime():
select parse_datetime('2020-03-23T05:17:44.000Z', '%Y-%m-%dT%H:%i:%s.%fZ')

Related

How to handle NULL values when using DATE or CAST functions in BigQuery

I have a string field 'ShippedDate' in the following format (1998-04-06 0:00:00) that I need to convert to datetime format. I have tried using DATE and CAST functions, but because there are NULL values in the field I am getting the following error: Invalid timestamp: 'NULL'.
I tried using those functions with IFNULL (see below), but I'm still getting the same error. How do I get around this issue? Thanks!
SELECT IFNULL(CAST(ShippedDate AS DATETIME),null)
SELECT IFNULL(DATE(ShippedDate),null)
You can use SAFE_CAST instead.
SELECT SAFE_CAST(ShippedDate AS DATETIME);

Convert pyarrow timestamp in ms to BigQuery timestamp

I have my timestamp in pyarrow in ms with this value: '2010-01-30 00:00:00.000000000'. I'm trying to convert it into a format that BigQuery will accept. I've tried:
SELECT PARSE_TIMESTAMP("%F %H:%M:%E*S %Ez", "2010-01-30 00:00:00.000000000") AS parsed;
SELECT PARSE_TIMESTAMP("%c", "2010-01-30 00:00:00.000000000") AS parsed;
but get the same error:
Failed to parse input string
I tried:
SELECT TIMESTAMP("2010-01-30 00:00:00.000000000");
got:
Invalid timestamp: '2010-01-30 00:00:00.000000000'
Tried:
SELECT FORMAT_TIMESTAMP("%c", TIMESTAMP "2010-01-30 00:00:00.000000000", "UTC") AS formatted;
Got:
Invalid TIMESTAMP literal
This query worked for me:
SELECT PARSE_TIMESTAMP("%F %H:%M:%E*S", "2010-01-30 00:00:00.000000000") AS parsed;
Output:

Can you apply BigQuery's PARSE_TIMESTAMP function to entire field?

I have a field "mytimestamp" which is currently of data type STRING, with the syntax "DD/MM/YYYY hh:mm:ss", and I'm looking to convert it in to a field of type TIMESTAMP. The function PARSE_TIMESTAMP works for a specific argument, eg
SELECT PARSE_TIMESTAMP('%d/%m/%Y %H:%M:%S', '15/04/2020 15:13:52') AS mynewtimestamp
but attempting to apply this to the entire column as follows
SELECT PARSE_DATETIME('%d/%m/%Y %H:%M:%S', mytimestamp) AS mynewtimestamp
FROM `project.dataset.table`
yields instead the error "Failed to parse input string "mytimestamp""
You probably have bad data in the column. You can find the problems using:
select mytimestamp
from `project.dataset.table`
where SAFE.PARSE_DATETIME('%d/%m/%Y %H:%M:%S', mytimestamp) is null
yields instead the error "Failed to parse input string "mytimestamp""
Error message suggests that instead of passing mytimestamp as a column name - you are passing "mytimestamp" as a string - so check your query for this

Cast timestamp to integer in Redshift

I'm trying to cast or convert a timestamp column to an integer value in a Redshift database. I know it is possible in other SQL-based databases and I've done this before, however in Redshift I can't get it to work.
I've tried some variations of cast and also convert (http://docs.aws.amazon.com/redshift/latest/dg/r_CAST_function.html).
Here is an example:
SELECT cast(finish_time as integer) FROM table;
It gives me the error message:
SQL Error Invalid operation: cannot cast type timestamp without time zone to integer;
Is it possible to get a timestamp as an integer?
try select extract('epoch' from finish_time), it will give you Unix ms timestamp

Ingres error trying to query timestamp without timezone column

I'm trying to write a query on a column that's of the time "Timestamp without timezone" and this is my where clause:
and (cv.expire_ts >= 'today' or ifnull(cv.expir_ts, '') = '')
I get the following error:
Error: 'today' is invalid format or value for timestamp without time zone or timestamp with local time zone. Please enter a valid timestamp value in yyyy-mm-dd hh:mm:ss[.fff...] format.
SQLState: 22007
ErrorCode: 4328
Error: Specified cursor is not known to the server.
SQLState: 24000
ErrorCode: 590366
How can I make this query work on this type of column?
Try using the CURRENT_DATE constant, without quotes.
I think missing part is date function for 'today'
Refer