I have a DateTime column named EXP_Date which contains date like this :
2014-07-13 00:00:00.000
I want to compare them, like this query :
SELECT COUNT(*)
FROM DB
WHERE ('2014-07-15' - EXP_DATE) > 1
I expect to see the number of customers who have their services expired for over a month.
I know this query wouldn't give me the correct answer, the best way was if I separate the Year / Month / Day into three columns, but isn't any other way to compare them as they are?
You can use DATEADD
SELECT COUNT(*)
FROM DB
where EXP_DATE < DATEADD(month, -1, GETDATE())
Try this
SELECT COUNT(*)
FROM DB
where DATEADD(month, -1, GETDATE()) > EXP_DATE
SELECT COUNT(EXPIRE)FROM
(Select CASE WHEN EXP_DATE < DATEADD(month, -1, GETDATE())THEN 1 ELSE 0 END)AS EXPIRE FROM DB
)tt
Another way using DATEDIFF
SET DATEFORMAT DMY --I like to use "dateformat"
SELECT COUNT(*)
FROM DB
WHERE (DATEDIFF(DAY,#EXP_DATE,GETDATE())) >= 30 --Remember, instead "Day" you can use week, month, year, etc
Syntax: DATEDIFF ( datepart , startdate , enddate )
Depart: year, quarter, month, day, week...
For more information you can visit MSDN
Related
I have an orders table which contains an order ID, order date and order description.
I want to run a select query which captures all orders that have been created in the last two days. so the current date minus two days. from the 14th December, I would want to select all orders where the order date is > 13th December. This needs to use a Get date function to pick up the current date and minus the days.
I have tried:
select * from orders where orderdate > getdate() - 2
but this is not producing the correct results. Any idea's how to do this please?
you should try to use dateadd function
select * from orders where orderdate > dateadd(dd,-1,cast(getdate() as date))
Now, this may be exactly what you need but then you need to understand that by casting to date we remove the time part and effectively go back to the start of the day and a day behind it(-1) gives the start of yesterday.
Try this:
select * from orders where orderdate > cast(getdate() - 1 as date)
If you want the orders of the last two days, use DATEADD to add days to today's date (in your case -2 days) then use DATEDIFF to compare the two days:
SELECT * FROM orders
WHERE DATEDIFF(DAY, DATEADD(DAY, -2, GETDATE()), orderdate) > 0
Now, assuming all orders have dates in the past and none in the future (which is what it should be), you can simply use DATEDIFF like this:
SELECT * FROM orders
WHERE DATEDIFF(DAY, orderdate, GETDATE()) <= 2
Note: you can use < 3 instead of <= 2.
Looks like dateadd and convert will solve the problem.
select o.*
from orders o
where o.orderdate >= dateadd(day, -2, convert(date, getdate()))
I want to fetch records from every Saturday. I have a column create_date (timestamp) - 2016-06-22 07:20:22.220
The below query works for me for a single day.
select * from table
where create_date > '2016-06-04'
AND creat_date < '2016-06-05'
But what if I want to fetch records for multiple days say - 2016-06-11, 2016-06-18, 2016-06-25
You can use the DATENAME() function for that:
SELECT *
FROM yourTable
WHERE DATENAME(dw, create_date) = 'Saturday'
If you want to also restrict the range of create_date you can add a check in the WHERE clause for that:
SELECT *
FROM yourTable
WHERE DATENAME(dw, create_date) = 'Saturday' AND
create_date BETWEEN '2016-06-04' AND '2016-06-25'
you can try to use datediff per a refer date.
this sample is '2016-06-11'.
select * from table
where datediff(d,'2016-06-11',create_date) in (0,7,14)
You can use DATENAME SQL function for this
SELECT *
FROM yourTable
WHERE DATENAME(weekday, create_date) = 'Saturday'
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.
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
In SQLExpress, I have a table that contains a datetime-column. It is formatted like this:
19.03.2012 00:00:00
Now, there are a lot of dates in there and I want to build a WPFChart, that shows me, how much dates are in march, in april and so on.
How can I manage this in sql that I get the count of one month?
Use:
select month(dateColumn), count(*)
from table
group by month(dateColumn)
You can extract the month of a date with Month() funciton.
than with a simple group by, you get the count for every month
To get only one month...
SELECT
COUNT(*),
SUM(valueColumn)
FROM
yourTable
WHERE
dateColumn >= '20120101'
AND dateColumn < '20120201'
To get multiple months, but grouped by month (and accounting for year).
SELECT
DATEADD(MONTH, DATEDIFF(MONTH, 0, dateColumn), 0),
COUNT(*),
SUM(valueColumn)
FROM
yourTable
WHERE
dateColumn >= '20110301'
AND dateColumn < '20120301'
GROUP BY
DATEADD(MONTH, DATEDIFF(MONTH, 0, dateColumn), 0)