Time difference in query - sql

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)

Related

Extract date from timestamp containing time zone in Big Query

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.

SQL Error 2207 on TO_TIMESTAMP() using datetime format

In Postgres, I'm trying to do a date/time based query in my WHERE predicate.
When I try to select with this date/time format SQL error says the value needs to be an integer. I'm not sure why it does not think my minute of 17 is not an integer or why it only see it as a 1 and not a 17?
SELECT *
FROM history
WHERE create_time > TO_TIMESTAMP('2018-10-08T23:17:44.728','yyyy-MM-dd''T''HH:mm:ss.SSS');
ERROR: invalid value ":1" for "HH"
DETAIL: Value must be an integer.
SQL state: 22007
You're trying to consume a date value that contains a T, and it looks like you're trying to declare to TO_TIMESTAMP that the T is a literal value to be ignored. Problem is you're doing this by putting 'T' (apostrophe-T-apostrophe, escaped) which is bumping the parser on by 3 characters and it is then encountering ':1' from 23:17 when it is expecting HH:
--your date, and underneath it, the format you gave
2018-10-08T23:17:44.728
yyyy-MM-dd'T'HH:mm:ss.SSS
Can you see how the HH aligns (vertically) with :1? Postgres is complaining that it was expecting an integer that it could parse to 23, but it encountered the string :1 which isn't an integer.
This question:
Postgres- have to_timestamp() ignore/not read a specific character in middle of date/time string
Implies you can put a space in the format where the T is, or just cast the string you have to a Timestamp - postgres can apparently parse that string as a Timestamp without you having to literally lay the format out for it explicitly
Try:
SELECT *
FROM history
WHERE create_time > TIMESTAMP '2018-10-08T23:17:44.728'
SELECT *
FROM history
WHERE create_time > cast('2018-10-08T23:17:44.728' as timestamp)
SELECT *
FROM history
WHERE create_time > TO_TIMESTAMP('2018-10-08T23:17:44.728','yyyy-MM-dd HH:mm:ss.SSS');
You might even find this works:
SELECT *
FROM history
WHERE create_time > TO_TIMESTAMP('2018-10-08T23:17:44.728', 'yyyy MM dd HH mm ss SSS')
The numbers align with the format fields and space is used for everything else you want to ignore (hyphens, colons, dots etc)
The problem is due to using ''T'' which's before HH, and DB signals that, you might use
TO_TIMESTAMP('2018-10-08 23:17:44.728','yyyy-mm-dd HH24:MI:SS.MS')
instead.

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’

SQL Server ISDATE() Error

I have a table and need to verify that a certain column contains only dates. I'm trying to count the number of records that are not follow a date format. If I check a field that I did not define as type "date" then the query works. However, when I check a field that I defined as a date it does not.
Query:
SELECT
count(case when ISDATE(Date_Field) = 0 then 1 end) as 'Date_Error'
FROM [table]
Column definition:
Date_Field(date, null)
Sample data: '2010-06-27'
Error Message:
Argument data type date is invalid for argument 1 of isdate function.
Any insight as to why this query is not working for fields I defined as dates?
Thanks!
If you defined the column with the Date type, it IS a Date. Period. This check is completely unnecessary.
What you may want to do is look for NULL values in the column:
SELECT SUM(case when Date_Field IS NULL THEN 1 ELSE 0 end) as 'Date_Error' FROM [table]
I also sense an additional misunderstanding about how Date fields, including DateTime and DateTime2, work in Sql Server. The values in these fields are not stored as a string in any format at all. They are stored in a binary/numeric format, and only shown as a string as a convenience in your query tool. And that's a good thing. If you want the date in a particular format, use the CONVERT() function in your query, or even better, let your client application handle the formatting.
ISDATE() only evaluates against a STRING-like parameter (varchar, nvarachar, char,...)
To be sure, ISDATE()'s parameter should come wrapped in a cast() function.
i.e.
Select isdate(cast(parameter as nvarchar))
should return either 1 or 0, even if it's a MULL value.
Hope this helps.
IsDate takes a character string or exression that yeilds a character string as it's argument
The problem is this method ISDATE() only admits arguments of type datetime and smalldatetime within the "time" types, so it won´t work if you are using date type.
Also if you use date as type for that field, you won´t have to check the information there because it won´t admit other type of field.
You shoul only check for null values in your column, that´s all.

Error comparing dates in SQL query

Within a ASP.NET/C# class I am trying to run a query where I compare dates:
select * from table1 CreatedDate >='DATEADD(d,-500,GETDATE())';
Basically I am trying to select rows from the the last 500 days.
The problem is I am getting the following error:
Syntax error converting datetime from character string.
An example of the CreatedDate field in the database is 2003-09-19 15:32:23.283 . The field is set to type = datetime, default = getdate().
If I run the query SELECT DATEADD(d,-500,GETDATE()); it returns 2008-09-17 23:41:34.710
The 2 values look the same so I am surprised am getting the error message. An idea on how I need to modify my query?
select * from table1 CreatedDate >= DATEADD(d,-500,GETDATE())
Lose the quotes around DATEADD(d,-500,GETDATE()). These make the expression varchar
Datatype precedence means you are trying to convert a string starting DATEADD to datetime...
have you tried without the single quotes?
CreatedDate >=DATEADD(d,-500,GETDATE())
You have put apostrophes around the dateadd expression, so it's not an expression, it's a string. That's the string that it fails to convert to a datetime value.
select * from table1 CreatedDate >= DATEADD(d,-500,GETDATE());
just remove '' quotes