I've got 2 date columns in my table (start_date, end_date).
I've tried Datediff(day, start_date, end_date), but I was prompt with:
invalid column name
How can I calculate the date difference between these 2 columns?
select DATEDIFF (day,start_date,end_date) from yourtablename;
Should be Datediff(day, start_date, end_date). There is no 's' at the end of the day
http://technet.microsoft.com/en-us/library/ms189794.aspx
It should be "day"
Datediff(day, start_date, end_date)
be certain that your columns are formated correctly to either be a DATE or DATETIME
Related
Two date columns need to compare with current system date, if those falls into it, display into table
You are working with dates, so I suspect you want:
where start_date <= convert(date, getdate()) and
end_date >= convert(date, getdate())
SQL: how do you fetch a first day of the month?
Hi,
I need to fetch a first day of every month from tbl.date from column start_date.
11/1/2017
12/1/2017
1/1/2018
2/1/2018
ETC.
Thanks
One solution in ANSI SQL looks like this:
select dte - (1 - extract(day from dte)) * interval '1 day'
In SQL Server, this would be:
select dateadd(day, 1 - day(dte), dte)
Similar functionality is available in almost any database.
If you just want the rows that are first days of the month, then:
where extract(day from dte) = 1
In SQL Server, this would be:
where day(dte) = 1
Of course, the syntax might be different depending on the database.
Another solution, using Postgres, in case you need to select the first row for each month, if there's no guarantee that that will be the first day of the month:
CREATE TABLE dates (
id BIGSERIAL PRIMARY KEY,
date TIMESTAMP NOT NULL
);
INSERT INTO dates(date)
VALUES ('2017-05-05'::timestamp), ('2017-05-02'::timestamp), ('2017-05-30'::timestamp), ('2017-05-25'::timestamp), ('2017-05-15'::timestamp), ('2017-05-01'::timestamp), ('2017-06-10'::timestamp), ('2017-06-02');
SELECT DISTINCT ON(EXTRACT(MONTH FROM date), EXTRACT(YEAR FROM date)) date
FROM dates
ORDER BY EXTRACT(MONTH FROM date), EXTRACT(YEAR FROM date), date ASC;
Results:
date
--------------------
2017-05-01T00:00:00Z
2017-06-02T00:00:00Z
Another solution, using Sql server
declare #FirstDOM date, #LastDOM datetime
set #FirstDOM = (select dateadd(d,-1,dateadd(mm,datediff(m,0,getdate()),1 )))
SELECT CONVERT( varchar, #FirstDOM,101);
I want to select day after tomorrow date in sql. Like I want to make a query which select date after two days. If I select today's date from calender(29-04-2015) then it should show date on other textbox as (01-05-2015). I want a query which retrieve day after tomorrow date. So far I have done in query is below:
SELECT VALUE_DATE FROM DLG_DEAL WHERE VALUE_DATE = GETDATE()+2
thanks in advance
Note that if you have a date field containing the time information, you will need to truncate the date part using DATEADD
dateadd(d, 0, datediff(d, 0, VALUE_DATE))
To compare 2 dates ignoring the date part you could just use DATEDIFF
SELECT VALUE_DATE FROM DLG_DEAL
WHERE datediff(d, VALUE_DATE, getdate()) = -2
or
SELECT VALUE_DATE FROM DLG_DEAL
WHERE datediff(d, getdate(), VALUE_DATE) = 2
Try like this:
SELECT VALUE_DATE
FROM DLG_DEAL WHERE VALUE_DATE = convert(varchar(11),(Getdate()+2),105)
SQL FIDDLE DEMO
SELECT VALUE_DATE FROM DLG_DEAL WHERE datediff(d, VALUE_DATE, getdate()) = -2
** I think you should try this**
SELECT DATEADD(day,2,VALUE_DATE) AS DayAfterTomorrow
FROM DLG_DEAL WHERE VALUE_DATE= GETDATE();
DATEADD(choiceToAdd, interval, date)
This function allows you to add or substract day,month, year,etc from date. In this interval is nothing but numeric value which you want to add or substract.
My database has a table in which it has two date columns (Start Date and End Date)
How can I list all the dates where the difference of Start Date and End Date is no more than 15 days?
This is what i tried so far:
SELECT homeworkID, subject, startdate, dateadd(day,15,startdate) as enddate
FROM homework;
Just add the filter predicate as per your rule/logic :
Select start_date,
end_date,
other_columns
From table
Where end_date - start_date <= 15
How can you get today's date and convert it to 01/mm /yyyy format and get data from the table with delivery month 3 months ago? Table already contains delivery month as 01/mm/yyyy.
SELECT *
FROM TABLE_NAME
WHERE Date_Column >= DATEADD(MONTH, -3, GETDATE())
Mureinik's suggested method will return the same results, but doing it this way your query can benefit from any indexes on Date_Column.
or you can check against last 90 days.
SELECT *
FROM TABLE_NAME
WHERE Date_Column >= DATEADD(DAY, -90, GETDATE())
Latest Versions of mysql don't support DATEADD instead use the syntax
DATE_ADD(date,INTERVAL expr type)
To get the last 3 months data use,
DATE_ADD(NOW(),INTERVAL -90 DAY)
DATE_ADD(NOW(), INTERVAL -3 MONTH)
I'd use datediff, and not care about format conversions:
SELECT *
FROM mytable
WHERE DATEDIFF(MONTH, my_date_column, GETDATE()) <= 3
Last 3 months
SELECT DATEADD(dd,DATEDIFF(dd,0,DATEADD(mm,-3,GETDATE())),0)
Today
SELECT DATEADD(dd,DATEDIFF(dd,0,GETDATE()),0)
Last 3 months record
SELECT *, DATEDIFF(NOW(),`created_at`)as Datediff from `transactions` as t having Datediff <= 90