I have data containing dates of the form
2020-12-14T18:58:10+01:00[Europe/Stockholm]
but I really only need the date 2020-12-14.
So, I tried:
DATE(Timestamp) as LastUpdateDate
which returned Error: Invalid time zone: +02:00[Europe/Stockholm]
So, thinking that the problem came from the time zone, I tried this instead:
TIMESTAMP(FORMAT_TIMESTAMP("%Y-%m-%d", PARSE_TIMESTAMP("%Y%m%d", Timestamp)))
which magically returned a new error, namely
Error: Failed to parse input string "2021-10-04T09:24:20+02:00[Europe/Stockholm]"
How do I solve this?
Just substring the date part from the string. Try one of these:
select left(Timestamp, 10)
select date(left(Timestamp, 10))
You should clean your data first.
select date("2020-12-14T18:58:10+01:00") as LastUpdateDate
This will work as expected.
Any chance of cleaning your data before using it in a query? Actually I think that +01:00[Europe/Stockholm] is not supported as format.
Related
I am doing just a simple conversion of timestamp column value to specific timezone and then getting the date out of it to create analytical charts based on the output of the query.
I am having the column of type timestamp in the bigquery and value for that column is in UTC. Now I need to convert that to PST (which is -8:00 GMT) and was looking straight forward to convert but I am seeing some dates up and down based on the output I get.
From the output that I was getting I took one abnormal output and wrote a query out of it as below:
select "2021-05-27 18:10:10" as timestampvalue ,
Date(Timestamp("2021-05-27 18:10:10" ,"-8:00")) as completed_date1,
Date(Timestamp("2021-05-27 18:10:10","America/Los_Angeles")) as completed_date2,
Date(TIMESTAMP_SUB("2021-05-27 18:10:10", INTERVAL 8 hour)) as completed_date3,
Date(Timestamp("2021-05-27 18:10:10","America/Tijuana")) as completed_date4
The output that I get is as below:
Based on my understanding I need to subtract 8 hours from the time in order to get the timestamp value for the timezone that I wanted and according to that completed_date3 column seems to show the correct value that should be there but if I use other timezone conversions as suggested in google documentation, the output gets changed to 2021-05-28 and I am not able to understand how that can happen.
Can anyone let me know what is the thing that I am doing wrong?
I was actually using it in a wrong way. I need to use it as below :
select "2021-05-27 18:10:10" as timestampvalue ,
Date(Timestamp("2021-05-27 18:10:10") ,"-8:00") as completed_date1,
Date(Timestamp("2021-05-27 18:10:10"),"America/Los_Angeles") as completed_date2,
Date(TIMESTAMP_SUB("2021-05-27 18:10:10", INTERVAL 8 hour)) as completed_date3,
Date(Timestamp("2021-05-27 18:10:10"),"America/Tijuana") as completed_date4
Initially I was converting that string timestamp to a specific timestamp based on the timezone and that is what I did not want.
Now if a convert a string to timestamp first without using time zone parameter and then apply timezone parameter when getting the date value out of it then it would return me correct date.
Please see the snapshot below :
I am trying to run this query to find some field form redshift and extra column as time difference. Below I have mentioned the query which I am trying to execute.
select gtfs.t_stops_gtfs.*, gtfs.t_stop_times_gtfs.departure_time, '${currentTime}' as current_time,
datediff(minute, gtfs.t_stop_times_gtfs.departure_time, '${currentTime}') as td from gtfs.t_stops_gtfs
join gtfs.t_stop_times_gtfs on gtfs.t_stops_gtfs.stop_id = gtfs.t_stop_times_gtfs.stop_id limit 3
The error which I am getting is
function pg_catalog.date_diff("unknown", character varying, "unknown") does not exist
any idea whats wrong in this.
Erorr is coming because of this line datediff(minute, gtfs.t_stop_times_gtfs.departure_time, '${currentTime}') as td
sample value in departure_time: '18:25:00', current_time: '06:34:39'`.
From the doc the datediff function need as input two timestamp or datetime. As you can see in your error, gtfs.t_stop_times_gtfs.departure_time passed is parsed as a string and '${currentTime}' is of type unknown. What you have to do is to cast them as date or timestamp like:
datediff(minute, gtfs.t_stop_times_gtfs.departure_time::date, '${currentTime}'::date)
if i used like this im not get any error
(ph.x_date >= to_date('01/05/2016') and ph.x_date <= to_date('01/04/2020'))
but if range is big like this
(ph.x_date >= to_date('01/05/2002') and ph.x_date <= to_date('01/04/2020'))
i get invalid number error.
where is my mistake. Thanks
To_date() has to be always used when you know the date data would come in a particular format always that you know before hand. Use to_date like this To_date('date', <dateformat>) this reads the date in the explicit format that you may want to make it read.
In your case it reads default but what if your order of retrieval changes in your x_date i guess your data in xdate column has some issues. Check data before 2016. Different order like mm/yy/dd or etc data different from the to_date format is there
you need to specify the format of date, e.g. to_date('01/05/2002','DD/MM/YYYY') and make sure the date format you get from the table is the same as you specified
I think your data in SELECT list is having an issue
Please check the output of the below command and see whether it returns the output
select *
from data ph
where ph.x_date >= to_date('01/05/2002') and
ph.x_date <=to_date('01/05/2016') ;
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’
I am trying to execute a query looking something like this:
create table A as
select
userid, to_date(date1, 'mm/dd/yyyy') as startDate,
to_date(date2, 'mm/dd/yyyy') as endDate
from TABLE;
I am getting the error:
ORA-01830: date format picture ends before converting entire input string
What's really strange here is that when I run only the SELECT... part of the query it works perfectly, I am only getting the error when I try to create a table. I absolutely need to make a table, so how can I get around this?
Thanks!
There's likely some bad data past the first few rows. When you run the select, it will do the conversion for the first few (less than 1000 for me) rows only. The results are paged. You'll need to clean up the data first. You could write a simple function like this to figure out which dates it's failing on.
How to handle to_date exceptions in a SELECT statment to ignore those rows?