How to get difference of 2 dates in Toad Teradata SQL - sql

I'm trying to get the difference between a due date and the system date in Teradata SQL using Toad.
SELECT
RECORD_ID,
DUE_DATE,
(DUE_DATE - CURRENT_DATE) DaysDiff
FROM TABLENAME
It returns an error:
"Invalid operation for DateTime or Interval."

Related

Filtering by using a varchar column in sql

I have a table which has a date but stored as a varchar datatype column:
my_date
metric_name
2021-09-12
CLS
2021-09-12
FID
I want to keep only 30 days back using this column.
I tried using cast(my_date as date) in the where clause, but it is not working.
Can you assist?
My code is in ANSI SQL.
You should be able to cast your column as a date. That would suggest syntax such as:
where cast(my_date as date) >= current_date - interval '30' day
That said, date/time functions are very database specific, so the exact syntax varies by database.

BigQuery TIMESTAMP TO DATETIME

Conversion rules in Google say that TIMESTAMPS can become DATETIME.
The following code works to turn a TIMESTAMP to a DATE.
SELECT CAST( DATE(CURRENT_TIMESTAMP()) AS DATE)
We can get a DATETIME using this in Standard SQL but we lose the time in the process:
SELECT CAST ( CAST( DATE(CURRENT_TIMESTAMP()) AS DATE) AS DATETIME )
How do we get a TIMESTAMP to become a DATETIME without losing the time (i.e. something like this)?
SELECT CAST( CURRENT_TIMESTAMP() AS DATETIME )
How do we do this in Legacy SQL and Standard SQL on BigQuery?
NOTE: We just discovered our example CAST works on Standard SQL (it appears our query system was using Legacy SQL).
You can simply use CAST(... as DATETIME) as in below example
#standardSQL
WITH `project.dataset.table` AS (
SELECT CURRENT_TIMESTAMP() ts_as_timestamp
)
SELECT ts_as_timestamp, CAST(ts_as_timestamp AS DATETIME) ts_as_datetime
FROM `project.dataset.table`
with result
Row ts_as_timestamp ts_as_datetime
1 2019-06-13 19:22:42.839108 UTC 2019-06-13T19:22:42.839108
For Legacy SQL
#legacySQL
SELECT ts_as_timestamp, CAST(STRING(ts_as_timestamp) AS DATETIME) ts_as_datetime
FROM (SELECT CURRENT_TIMESTAMP() ts_as_timestamp)
with same output

Hive equivalent of Teradata statement

I am trying to convert a Teradata query to Hive
WHERE visit_date BETWEEN (CURRENT_DATE-194) AND (CURRENT_DATE)
where visit_date is a string of format yyyy-mm-dd.
CURRENT_DATE is valid in Hive but CURRENT_DATE-194 is giving error.
How can I do it in Hive?
Got the solution by using
visit_date BETWEEN date_sub(CURRENT_DATE,194) AND CURRENT_DATE
To get data of past 194 days in Hive;
Try below query:
select * from table_1 where visit_date > date_sub(from_unixtime(unix_timestamp()), 194);
Note: TIMESTAMP is milliseconds
unix_timestamp is in seconds

Convert SQL Server code to Oracle please?

Having a bit of a issue with a SQL conversion from SQL Server to Oracle.
We are passing in a datetime value (in this example just 1900-01-01) and we need to select all rows that have a modified date greater than 2 days before the date passsed in. Here is the SQL syntax that works:
SELECT *
FROM TABLENAME
WHERE CAST(LAST_MODIFIED AS DATE) > CAST(DATEADD(dd, -2, '1990-01-01') AS DATE);
Where LAST_MODIFIED is a column, and the '1900-01-01' is a value being injected to the SQL String in C# prior to it being executed. Before being asked, we prefer not to subtract the 2 days from the date before passing it to the SQL :)
So what we need then is just the above query converted to Oracle syntax... We have tried a couple things and it fails :(
Thanks,
Dave
You can write this in Oracle as:
SELECT *
FROM TABLENAME
WHERE TRUNC(LAST_MODIFIED) > (DATE '1990-01-01') - 2
Notes:
In Oracle, DATE includes a time component, so casting to a date does nothing.
Oracle supports various ways to include a date/time constant. I prefer the keyword DATE with the ISO/ANSI standard date format YYYY-MM-DD.
The - 2 is perhaps more accurately written as - interval '2' day. However, the interval notation is new(ish) to Oracle.
And, it is better in either database to write this without modifying last_modified_date:
SELECT *
FROM TABLENAME
WHERE LAST_MODIFIED >= (DATE '1990-01-01') - 1
Removing the time component is not needed, with the right date comparison.
SELECT *
FROM TABLENAME
WHERE CAST(LAST_MODIFIED AS DATE) > TO_DATE('1990-01-01','YYYY-MM-DD')-2;
SELECT *
FROM TABLENAME
WHERE TRUNC( LAST_MODIFIED ) > TO_DATE( '1990-01-01', 'YYYY-MM-DD' ) - 2;
or, if you pass the value in using the :date_value bind variable:
SELECT *
FROM TABLENAME
WHERE TRUNC( LAST_MODIFIED ) > :date_value - 2;
If the value you are passing in to the query does not have a time component then you can just do:
SELECT *
FROM TABLENAME
WHERE LAST_MODIFIED >= :date_value - 1;

Convert YYYYMMDD String to Date in Impala

I'm using SQL in Impala to write this query. I'm trying to convert a date string, stored in YYYYMMDD format, into a date format for the purposes of running a query like this:
SELECT datadate,
session_info
FROM database
WHERE datadate >= NOW() - INTERVAL 5 DAY
ORDER BY datadate DESC;
Since the >= NOW() - INTERVAL 5 DAY code won't work with the YYYYMMDD string, I'd like to find a way to convert that into a date format that will work with this type of query. My thought is that it should look something like this (based on similar questions about other SQL query editors), but it's not working in Impala:
SELECT datadate,
session_info,
convert(datetime, '20141008', 102) AS session_date
FROM database
WHERE session_date >= NOW() - INTERVAL 5 DAY
ORDER BY session_date DESC;
Anyone know how to do this in Impala?
EDIT:
I finally found a working solution to the problem. None of the attempts using configurations of CAST or CONVERT would work in Impala, but the below query solves the problem and is fully operational, allowing date math to be performed on a column containing string values:
SELECT datadate,
session_info
FROM database
WHERE datadate >= from_unixtime(unix_timestamp(now() - interval 5 days), 'yyyyMMdd')
GROUP BY datadate
ORDER BY datadate DESC;
Native way:
to_timestamp(cast(date_number AS STRING), 'yyyyMMdd')
See Timestamp Literals on [Link Updated 2020-08-24]:
https://docs.cloudera.com/cdp-private-cloud-base/7.1.3/impala-sql-reference/topics/impala-literals.html
You need to add the dashes to your string so Impala will be able to convert it into a date/timestamp. You can do that with something like:
concat_ws('-', substr(datadate,1,4), substr(datadate,5,2), substr(datadate,7) )
which you can use instead of datadate in your expression.
To ignore hour/minute/second... , use from_timestamp, result 2020-01-01.
select from_timestamp(cast('2020-01-01 01:01:01.000000' as TIMESTAMP),'yyyy-MM-dd');