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

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'))

Related

difference between two dates in sql wih year and month format

i have two date like this
-4 year and 6 month
-2 year and 3 month
i want to calculate difference between this two date ?
If you have a datetime value then you can use Datediff function to calculate the difference between two datetime values in SQL Server.
SELECT DATEDIFF(day, '2004-06-01', '2008-06-01') AS "Number of Days";

PL/SQL to subtract two dates

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

T-SQL calculate with calendar week

I have a T-SQL question - I'm using SQL-Server 2012:
I have a varchar calendar week column (1-52). In my where clause I only want to select the last 6 weeks going back from the current calendar week of the current year. I also have a year varchar column with values of "2011" , "2012" and "2013".
But now I'm struggling to set the expression for the where clause.
Or do I need a Having clause to get this done?
I've tried it like this:
Having Calendarweek BETWEEN max(Calendarweek)-6 AND max(Calendarweek) AND Year=2013
but it returns all weeks.
Hope you can help me out with this.
thank you.
Try this
where Year ='2013'
and CAST(Calendarweek AS INT) >= datepart(week,getdate())-6

Getting week number of date in SQL

Is there a way to get the week number of a date with SQL that is database independent?
For example to get the month of a date I use:
SELECT EXTRACT(MONTH FROM :DATE)
But the EXTRACT function doesn't know about weeks in SQL92.
Please pay attention to the fact that I want a solution that is database independent! Do not misinterpret this as a question regarding MS SQL Server.
There doesn't appear to be a single standard SQL function to extract the week number from a date - see here for a comparison of how different SQL dialects can extract different dateparts from date fields.
You can try this.
declare #date datetime
select #date='2013-03-15 00:00:00'
select 'Week No: '+ convert(varchar(10),datepart(wk,#date)) as weekno
Try this one :
SELECT DATENAME(yy,GETDATE())+RIGHT(DATENAME(wk,GETDATE()),2)
To understand DATENAME, please follow this link : sqltutorials.blogspot.be/2007/05/sql-datename.html and for the right, suite101.com/article/sql-functions-leftrightsubstrlengthcharindex-a209089 . There are examples to better understand ;-) It will work on MS and other sql servers normally ;-)
The only db independent solution I see is to get the number of days between today and Jan 1 of curr. year. Then divide it by 7 and round it up. There is 73 days from Jan 1 till today, which gives 10.43 as week number. The ISO week number is 11.
Correction: the number of days between last day of the current week and Jan 1 of curr. year. In this case the ISO week is 10, and 68/7 = 10 (rounded).
The best idea I found is
SELECT ROUND(((DAY(NOW())-1)/7)+1)
select (DATEPART(yyyy , CAST(GETDATE() AS datetime)) * 100 +
DATEPART(ww , CAST(GETDATE() AS datetime))) as week

Informix - extract day of month from date, and select only odd days

how to extract day of month from date, and select only odd days in Informix SQL?
Day of month is simply the DAY(date_or_datetime_column) function, related to the MONTH() and YEAR() functions. Getting only odd numbers is done with a simple modulo 2 expression.
So I think you only need:
SELECT date_col, DAY(date_col)
FROM table
WHERE MOD(DAY(date_col), 2) = 1
Hope that's useful.