Calculating time difference between 2 dates in Teradata - sql

I'm trying to calculate time difference (in months) between the current date, and the date the customer opened his account (only for those who joined in January 2012).
I try to use current_date and cast, but I think my problem is in my date field which is in the following format: dd/mm/yyyy
I'm working on Teradata.
Your help will be appreciated.

You can try like this:
SELECT CURRENT_DATE - CAST('2016-06-06' AS DATE) MONTH(4);
and if your date is in dd/mm/yyyy format then you can try like
SELECT CURRENT_DATE - cast(myDate as date format 'YYYY-MM-DD') MONTH(4);

Related

Date Format syntax in where clause in Teradata

I have a table with date column in which date is updated in this format - 11/21/2022.
How can I get the results for the last 15 days using this date column in Teradata? Looks like need to change the date format in where clause.
I was using below query which does not work with this format
select *
from table_A
WHERE date BETWEEN (CURRENT_DATE - INTERVAL '30' DAY)
AND CURRENT_DATE
This does not give any results.
Assuming your date column be text, then you must first convert it to a bona fide date using the TO_DATE() function:
SELECT *
FROM table_A
WHERE TO_DATE("date", 'MM/DD/YYYY') BETWEEN (CURRENT_DATE - INTERVAL '30' DAY) AND CURRENT_DATE;
Note that ideally you should be storing your dates in actual date columns, not as text. This would avoid the need to call the clunky TO_DATE() function.

how to select all entries having date 25-11-20 in oracle 11g?

sql table
here in the table above named carpooling contains a column name start_on which has date time as timestamp i have to write a query to select all the rows having date as 25-11-20 using to_char and to_date.
You write a timestamp literal like this:
timestamp '2020-11-25 00:00:00'
so the full filtering condition will be
where start_on >= timestamp '2020-11-25 00:00:00'
and start_on < timestamp '2020-11-26 00:00:00'
Note that dates and timestamps are different in Oracle, and dates include times down to the second (this is for historical reasons - originally there was only the date type, and timestamp was added much later).
Use the TRUNC function, along with date and interval literals:
SELECT *
FROM CARPOOLING
WHERE START_ON BETWEEN DATE '2020-11-25'
AND (DATE '2020-11-26' - INTERVAL '0.000001' SECOND)
You can simply use to_date, but it's recommended to remove the time when comparing the dates. Otherwise, rows having the same date, but a different time will not be selected. Removing the time can be done using TRUNC.
So you can do something like this:
SELECT * FROM carpooling
WHERE TRUNC(start_on) = TO_DATE('2020-11-25','yyyy.mm.dd');
If you don't want to check the 25th of November 2020, but another data, change the date to match your goal.

Query that will select a timeframe from specific date to today

I'm having issues in my WHERE clause selecting data from a specific day to today's date. The day/time format in my date column is '7/2/2020 3:12:08 PM'.
I've tested a couple options but keep getting this error - 'literal does not match format string'.
Any idea's of how I can select all data from March 1, 2020 to current date?
Thanks!
In Oracle date columns are not strings, they are exactly in date datatype, so you don't need to convert/cast it. Just use simple date literals:
https://docs.oracle.com/en/database/oracle/oracle-database/19/sqlrf/Literals.html#GUID-8F4B3F82-8821-4071-84D6-FBBA21C05AC1
select * from table where your_date_columg >= date'2015-12-31'
or with to_date function for your string:
select * from table
where
your_date_columg >= to_date('2019-11-25 13:57:52',
'yyyy-mm-dd hh24:mi:ss')

Bigquery standardSQL: Current date minus a previous date with a result in number of days?

I would like to the current date minus a previous begin date with the result with the result being the number days there is a difference of the two?
I have attempted the following: date_sub(Begindt, INTERVAL current_date)
Also, will I have to cast things differently?
Below is for BigQuery Standard SQL
DATE_DIFF(CURRENT_DATE(), Begindt, DAY)
See more for DATE_DIFF()
Above assumes the Begindt field is of DATE type
If not, you should cast to DATE type via CAST or PARSE_DATE functions
Are you finding something like below
DATE_DIFF(Begindt, CURRENT_DATE, day)

turnaround time calculation in oracle SQL developer

I am trying to calculate turnaround time in days between 2 dates for each record.
the first date (ORDERDATE), which is a string that I converted to date format
using To_char(to_date) function
the second date is (CURRENT_DATE) which has proper date format.
SELECT
SPECCODE,
SOURCECODE,
SOURCEDESCRIPTION,
**TO_CHAR( TO_DATE (PATCASE.ORDEREDDATE, 'YYYYMMDD'))"ORDER_DATE",
CURRENT_DATE**
FROM ...........
You could use simple substraction:
SELECT
TRUNC(TO_DATE (PATCASE.ORDEREDDATE, 'YYYYMMDD') - SYSDATE) AS days_diff
FROM ...;
DBFiddle Demo