Subtracting two date fields - sql

I have two date fields and I am trying to subtract them to come up with the difference in days. I am using Oracle SQL server.
select LST_MOD_TS-EFF_DT as DATE_DIFF from TABLE1;
This gives me the following output:
+01 04:08:33.000000
I would like it formatted as '1' so I do not need the '+' or '-' sign, and I do not need the date portion in the return.
What is the best way to accomplish this?

Use the EXTRACT Function
SELECT EXTRACT(day FROM LST_MOD_TS)-(day FROM EFF_DT) AS DATE_DIFF
FROM TABLE1;

Related

Getting not valid month in Oracle sql

I have a table called Transactions that has a column called trans_date. I am just trying to do a simple query in the SQL*Plus command window
The query is
SELECT * FROM transactions WHERE
trans_date BETWEEN to_date('09/11/2021','mm/dd/yyyy') AND to_date('09/12/2021','mm/dd/yyyy');
When I run this query I get not valid month and there is a little * under trans_date. Most of what I have read suggests the query is right but I am not sure what the problem is. The data type is varchar2(20).
Since trans_date is a varchar and you're trying to query whether it's between two dates, you need to convert it to a date too. Assuming it has the same format as the literals in your query:
SELECT *
FROM transactions
WHERE to_date(trans_date, 'mm/dd/yyy') BETWEEN
to_date('09/11/2021','mm/dd/yyyy') AND to_date('09/12/2021','mm/dd/yyyy');
Seems like problem is columns data type, Try convert it to date,
SELECT * FROM transactions
WHERE to_date(trans_date,'mm/dd/yyyy') BETWEEN to_date('09/11/2021','mm/dd/yyyy') AND to_date('09/12/2021','mm/dd/yyyy');
You need to convert trans_date to a date. However, you can use date constants for the comparisons:
SELECT *
FROM transactions
WHERE to_date(trans_date, 'mm/dd/yyyy') BETWEEN DATE '2021-09-11' AND DATE '2021-09-12';
You should fix your data model so the dates are stored correctly, using Oracle's built-in data type.

Oracle SQL: Transpose Columns to Rows After Using Extract

I have a table with column DATE. Date is 'dd/mm/yyyy' and I want only days. So I try with extract and return what I need, but I what using transpose for column to row.
The select statement is:
select EXTRACT (DAY FROM "DATE") DAY
from people;
Is this thing possible?
Thank you!
If you have a string, then just use the leftmost two characters:
select substr("DATE", 1, 2) as day
That said, you should not be storing dates as strings. It is wrong, wrong, wrong. You cannot use the built-in date/time functions. You cannot use inequality comparisons either. Fix your data model.
The date format doesn't matter. It is linked to your NLS local settings and this is how you see this.
To have it generic and extract DAY from the date do this:
select to_char(sysdate, 'DD') from dual;
Would return 07 since it's September 7th 2020.

Eliminate hours, minutes and seconds from a date in SQL ORACLE

I'm trying to remove the hours, minutes and seconds from a date using the following code :
TRUNC(column_date, 'YY')
But I get this : 01JAN2008:00:00:00, while I want this : 01JAN2008.
What should I do?
TRUNC() works as expected, and returns the original date truncated to year, as a date datatype. You want to format this date before displaying it (this actually means converting it to a string). For this, you can use the TO_CHAR() function.
You probably want:
TO_CHAR(TRUNC(column_date, 'YY'), 'ddmonyyyy')
Please note that this expression could be simplified to avoid the use of TRUNC(), as follows:
'01JAN' || TO_CHAR(column_date, 'yyyy')
I think you want to_char: https://docs.oracle.com/database/121/SQLRF/functions216.htm#SQLRF06129
Try using it in this way:
SELECT TO_CHAR(column_date, 'DD/MON/YYYY')
FROM x;
Where x is the table you are trying to query.
you only need to use
select to_char(column_date,'ddMONyyyy') FROM yourTable
Even you can invoke with
select to_char(column_date,'ddMONyyyy','nls_date_language=english') FROM yourTable
to guarantee to see the abbreviation JAN in the case your session/system date language value is different than english
to display as expected.

Oracle sql not grabbing day for the current date

I am trying to get the day of the week for the asofdate field. I am receiving an error using Oracle SQL that states: 'a non numeric character was found where a numeric character was expected.' That error is for this query:
select to_char(to_date(max(distinct(asofdate)), 'mm/dd/yyyy'), 'DY') from PS_Z_EXS251AE_EMP
the below query returns '1/6/2015'
select max(distinct(asofdate)) from PS_Z_EXS251AE_EMP
Does anyone know what I'm doing wrong?
If asofdate is stored as a date/time data type, then why are you converting it to a date. Also, why are you using max():
select to_char(asofdate, 'DY')
If asofdate is stored as a string, then you should probably fix the data. Why are you storing a date as a string. That is the wrong type.
If you want the day of the week of the maximum date, which your question suggests, then just do:
select to_char(max(asofdate), 'DY')
You can simply do
select distinct max(extract(day from asofdate)) from from PS_Z_EXS251AE_EMP;

MONTHS_BETWEEN error and to create flags

SELECT months_between(TO_DATE(SYSDATE,'DD-MM-YYYY'),TO_DATE(DATE_COLUMN,'DD-MM-YYYY')) FROM TABLE_A;
1.I got an error which says, 'a non-numeric character was found where a numeric was expected'
Also, how to handle nulls.. I want to put a default date if the date_column is null in table_A.
2.
SELECT MONTHS_BETWEEN(TO_DATE(SYSDATE,'DD-MM-YYYY'),TO_DATE(DATE_COLUMN,'DD-MM-YYYY')) FROM TABLE_A;
After calculating months between dates, I want to categorize the records with date range with a case statement..
For example..
if months_between for set of records is 22..I want to put a flag,'0-24', for all those records..
similarly, if months_between is 34.. I want to put a flag,'24-48', for all the records which fall under this range..
To add to previous answer you can avoid null like this:
SELECT months_between(trunc(SYSDATE), nvl(TO_DATE(DATE_COLUMN,'DD-MM-YYYY'), sysdate))
If your DATE_COLUMN is date type than You don't need to use TO_DATE()
So You'll get somthing like this
SELECT months_between(trunc(SYSDATE), nvl(trunc(DATE_COLUMN), sysdate))
For the second part of Your question You may try something like this:
SELECT
trunc(months_between(trunc(SYSDATE), trunc(DATE_COLUMN))),
(trunc(months_between(trunc(SYSDATE), trunc(DATE_COLUMN))/24)*24)||'-'||((trunc(months_between(trunc(SYSDATE), trunc(DATE_COLUMN))/24)*24)+24)
FROM your_table
You don't need to convert sysdate to a date. Try this:
SELECT months_between(trunc(SYSDATE), TO_DATE(DATE_COLUMN,'DD-MM-YYYY'))
The problem is that the default format for a date in Oracle uses the month name. When you say to_date(sysdate, . . .), the first argument is converted to a string and then back to a date.
By the way, the trunc() is irrelevant, but you seemed to want to extract the day portion of sysdate so I left it in.
EDIT:
If date_column is already a date, just use:
SELECT months_between(trunc(SYSDATE), DATE_COLUMN)
This works, if you have null values in the date_column and want to pass a default date.
select months_between(SYSDATE,coalesce(DATE_COLUMN,to_date('01-01-2014','DD-MM-YYYY'))) DIFF from TABLE_A;