Failed to parse input string in BigQuery with parse_date - sql

I have a column 'appointment_date' in string formate representing a date dd.mm.yyyy.
In BiqQuery I am using the following query to find all appointments dates lying in the future:
SELECT appointment_date
FROM `appointments`
where parse_date('%d.%m.%Y', appointment_date) > current_date()
BiqQuery returns the following error message: Failed to parse input string ""
Please advice.
Thanks,
Janine

Use safe.parse() to avoid the error:
where safe.parse_date('%d.%m.%Y', appointment_date) > current_date()
This will return NULL for invalid formats rather than an error.

Related

Bigquery cannot parse date from datetime

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

How to parse CURRENT_DATETIME() at ("%m/%d/%Y %H:%M:%S") in BigQuery

I am trying to parse using the format_date("%m/%d/%Y %H:%M:%S", CURRENT_DATETIME()) as date_mod but it gives me error:
No matching signature for function FORMAT_DATE for argument types: STRING, DATETIME. Supported signature: FORMAT_DATE(STRING, DATE) at [2:9]
I used the FORMAT_DATETIME, without result.
Help pls.
Thx!
As indicated in the error message that you are getting, FORMAT_DATE() is there to format values of the DATE datatype.
If you have a DATETIME (which CURRENT_DATETIME() returns), then you want FORMAT_DATETIME():
format_datetime("%m/%d/%Y %H:%M:%S", CURRENT_DATETIME())

Oracle error message input value not long enough

I need help with the below query. I am getting an error message :
ERROR: Error fetching from cursor. ORACLE error is ORA-01840: input
value not long enough for date format.
What input value is this referring to not being in date format? I can't figure this out. I do see where it refers to AND Removed>= TO_DATE('08162011', 'MMDDYYYY').
Removed
Probably you have a value like TO_DATE('0816', 'MMDDYYYY') for
TR_EFF_DT input, and that does not fit with respect to the date
format, as in the following statement :
with tab(TR_EFF_DT) as
(
select TO_DATE('0816', 'MMDDYYYY') from dual
)
select *
from tab
where TR_EFF_DT>= TO_DATE('08162011', 'MMDDYYYY');
Error:
ORA-01861: literal does not match format string
OR you probably have a mismatch for your DB server's Date Format with your current session's Date Format. In this case you
may issue :
ALTER SESSION SET nls_date_format='MMDDYYYY';

sql teradata filtering on date - database version Teradata 15.10.06.02 and provider version Teradata.Net 15.11.0.0

my table has a date column. its data type is date. I confirmed it by going to table name>>columns and it says MTH_END_DT [DATE, Not NULL]
I want to filter my data for a particular date. If I put a condition where MTH_END_DT = '6/1/2018' I get an error select failed [3535] A character string failed conversion to a numeric value.
I followed this page. I used where MTH_END_DT = date '6/1/2018' and i get an error syntax error invalid date literal
I tried where cast(timestamp_column as date) = date '2013-10-22'; something like this and it throws error too
How should i filter my data?
There's only one reliable way to write a date, using a date literal, date 'yyyy-mm-dd'
where MTH_END_DT = DATE '2018-06-01'
For a Timestamp it's
TIMESTAMP '2018-06-01 15:34:56'
and for Time
TIME '15:34:56'
In SQL Assistant it's recommended to switch to Standard SQL format YYYY-MM-DD in Tools-Options-Data Format-Display dates in this format
I did have the similar problem when I was filtering a particular date for my query with Teradata. First method I tried was putting 'DATE' term as the following:
WHERE saledate = DATE'04/08/01' but this did not solve the problem.
I then used an approach I stumbled upon when surfing, finally it worked.
WHERE extract(year from saledate)=2004 AND extract(MONTH from saledate)=8 AND extract(DAY from saledate)= 1 source
I think this really should not be this long, but it worked.
It seems to me it’s most likely you have input the date format incorrectly? Maybe it includes a time by default.
For example
where MTH_END_DT = ‘2013-10-22-00:00:00:00’

Convert STRING to DATE (error 2666)

in Teradata SQL I need convert a string to date.
Currently the string looks like this: 2017-02-28T14:41:32.817Z
But I need it in this format as DATE: DD.MM.YYYY HH:SS
Any idea how to do this? Whenever I try to cast, I get the error 2666 ( Invalid date supplied for mytable.mycolumn)
Hope someone can help!
Best regards,
Both input and expected result are timestamps, not dates.
SELECT '2017-02-28T14:41:32.817Z' AS mycol,
-- string to timestamp
Cast(mycol AS TIMESTAMP(3)),
-- back to string with a different format
To_Char(Cast(mycol AS TIMESTAMP(3)), 'DD.MM.YYYY HH:SS')