PL/SQL to subtract two dates - sql

I use SQL Server 2012, and in my table I have columns id, date, days.
Column date is formatted dd-mm-yyyy.
I need PL/SQL code to fetch today date and find difference between date column and store it in days column
Example:
fetch today date and find difference of stored date from the table
id date days
1 01-12-2015 1
2 30-11-2015 2
I need PL/SQL code.

You may use DATEDIFF:
SELECT DATEDIFF(day,'2015-06-05','2015-08-05') AS DiffDate
For your case it should be:
INSERT INTO table_name (days)
SELECT DATEDIFF(day,table_name.date,GETDATE());
SQL Server Date Functions

Use DATEDIFF and the LEAD function to compare against the next row.
SELECT DATEDIFF(d,date,LEAD(date) OVER (ORDER BY id))
FROM yourtable
For comparison against today's date
SELECT DATEDIFF(d,date,GETDATE())
FROM yourtable

Related

DB2 How to select dates for the previous 2 months

I am using Oracle SQL developer on DB2 and have a date field stored as an integer e.g. 20210401
I want to bring back results for the last 2 months and have tried this:
select * from table where date > add_months(sysdate, -2)
This is producing error 206 saying it is not valid in the context used.
Does anyone know how to convert the data column or have an easier way to filter for the last 2 months
Use this:
select *
from table
where date > INT (TO_CHAR (CURRENT TIMESTAMP - 2 MONTH, 'YYYYMMDD'));

Get the number of the weekday from each date in a date list

DB-Fiddle
CREATE TABLE dates (
date_list DATE
);
INSERT INTO dates
(date_list)
VALUES
('2020-01-29'),
('2020-01-30'),
('2020-01-31'),
('2020-02-01'),
('2020-02-02');
Expected Results:
Weekday
2
3
4
5
6
I want go get the number of the weekday for each date in the table dates.
Therefore, I tried to go with the solution from this question but could not make it work:
SELECT
EXTRACT(DOW FROM DATE d.date_list))
FROM dates d
How do I need to modify the query to get the expected result?
Get rid of the date keyword it is only needed to introduce a DATE constant. If you already have a DATE value (which your column is) it's not needed:
select extract(dow from d.date_list)
from dates d

date comparison oracle sql

how to compare joining date with current date and if it is less than or equal to 45 days then select those details and display in oracle sql, what to do if joining date and current date in different format like (joining date 12/03/2015 and current date 01-MAR-17)?
That would be something like this (if you need rows for those who joined 45 days or more ago):
select *
from your_table
where joining_date <= trunc(sysdate) - 45;
I luckily got output, query is
select COL_NAME from TABLE_NAME where (to_date(SYSDATE)- to_date(COL_NAME,'dd/mm/yyyy'))<=60

splitting a datetime column into year, month and week

I want to split a datetime column so that the year and the month both have their own column in a select statement output. I also want to have a column by week of the year, as opposed to specific date.
Basically, I want separate year, month, and week columns to show up in my select statement output.
Try using the DatePart function as shown in the following:
select
datepart(year,Mydate),
datepart(month,Mydate),
datepart(week,Mydate)
From
MyTable
Note: If you need to calculate the week number by ISO 8601 standards then you'll need to use datepart(iso_week,Mydate)
You could also look at the DateName function
select
datename(month,Mydate)
From
MyTable
Here is another way. Use SQL Servers YEAR() and MONTH() functions. For week, I use datepart(week,Mydate) as noted by #DMK.
SELECT YEAR(MyDate), MONTH(MyDate), DATEPART(WEEK,Mydate) From YourTable
check this
select datepart(Month,DateColumn) Mnth,datename(Month,DateColumn) MnthName,datepart(Year,DateColumn) Year1,((day(DateColumn)-1) / 7) + 1 week from dbo.Table

query in SQL to get the months b/w the dates

I have a table with the following data.
id Date of Issue Date of Travel
---------------------------------
1 10-april-2011 10-may-2011
2 25-april-2011 22-may-2011
How can I display the number of months between the Date of Issue and Date of Travel in SQL (I am using an MS Access database)?
If I understand your question, you need to use the DateDiff function:
SELECT DATEDIFF ("m", [id Date of Issue], [Date of Travel]) FROM ...
To count the months between the dates:
SELECT MONTH('Date of Travel')-MONTH('Date of Issue')+12*(YEAR('Date of Travel')-YEAR('Date of Issue'))