Query for results between now and 7 days from now - sql

I know there are similar answers to this, but none of them have worked for me. I have a query with a date field in it. I want to filter the results for the next 7 days. It works if I specify the dates i.e date between '2019-07-18' AND '2019-07-25' but I need it to be more generic.
Thanks in advance!
Here are a few things I've tried:
CAST( datefield AS DATE ) > DATEADD( DAY, +7, CAST( GETDATE() AS DATE ))
WHERE GETDATE()+7
AND DATEADD(dd, -7, datefield) <= CAST(datefield AS DATETIME)

You can use logic like this:
where datefield >= dateadd(day, 1, cast(getdate() as date)) and
datefield < dateadd(day, 8, cast(getdate() as date))
This assumes you are using SQL Server (based on your syntax) and that you don't want today's data ("next 7 days").

You didnt specify what SQL you are using so this might not be fine for you.
WHERE datefield BETWEEN SYSDATE AND SYSDATE + 7

datefield between today and next 7 days
WHERE
CAST(datefield AS DATE) <= DATEADD(DAY, 7, CAST(GETDATE() AS DATE ))
AND CAST(datefield AS DATE) >= CAST(GETDATE() AS DATE )

Related

Filtering in SQL between evening previous day and morning current day

I am trying to filter a table between 4 pm of previous day and 4am of current day but am at loss of how to query that.
Something like:
WHERE
dateColumn >= DATEADD(DAY, -1, GETDATE()) AND dateColumn <= GETDATE()
AND DATEPART(hh, dateColumn) >= 16 AND DATEPART(hh, dateColum) <= 4
I realize the second line in WHERE statement is obviously incorrect and will not return any results but that is to give an idea of what I am trying to do. All help is appreciated!
There are various ways you could do this, this gets the datetime at 1600 yesterday and 1600 today.
WHERE dataColumn >= dateadd(hour, -8, convert(datetime, convert(date, getdate())))
AND dateColumn <= dateadd(hour, 16, convert(datetime, convert(date, getdate())));

What is SQL Server statement to get day and month ignoring the current year?

I have table HD_Case_Master with a column cm_date_created.
The example value is 2011-05-13 10:07:36.000.
I manage to get the current year but how to get record for all day and month.
SQL statement:
SELECT
cm_create_date
FROM
HD_Case_Master
WHERE
YEAR(cm_create_date) = YEAR(GETDATE()) AND ???
If you want the current date:
SELECT cm_create_date
FROM HD_Case_Master
WHERE cm_create_date >= CAST(GETDATE() as date) AND
cm_create_date < CAST(DATEADD(day, 1, GETDATE()) as date)
If you want to check if cm_date_created is equal to the current date, ignoring the time component, you just need to CAST both dates to DATE:
SELECT *
FROM HD_Case_Master
WHERE
CAST(cm_create_date AS DATE) = CAST(GETDATE() AS DATE)
Alternatively, you can use >= and < to check if cm_date_created falls within the current date:
SELECT *
FROM HD_Case_Master
WHERE
cm_create_date >= CAST(GETDATE() AS DATE)
AND cm_create_date < CAST(DATEADD(DAY, 1, GETDATE()) AS DATE)
You can use DATEPART function of tsql.
... Where DATEPART(day, cm_create_date) = DATEPART(day, getdate()) AND DATEPART(month, cm_create_date) = DATEPART(month, getdate())
You were really close! To add just the month (but ignoring the year), just use this:
SELECT
cm_create_date
FROM
HD_Case_Master
WHERE
YEAR(cm_create_date) = YEAR(GETDATE())
AND MONTH(cm_create_date) = MONTH(GETDATE())

how to get records who is reaching todays date

I want to get records who are about reach todays date with in 2 days ProjectEnddt. i want to show Projects who are going to end with in 2 days. want to show 2 days before.
Query
SELECT ProjectEndDt
, ProjectRenewalDt
, ProjectUpgradeDt
FROM tbl_project
WHERE CONVERT(VARCHAR(10), DATEADD(DAY, 2, ProjectEndDt)) >= CONVERT(VARCHAR(10), GETDATE());
Try this:
SELECT ProjectEndDt
,ProjectRenewalDt
,ProjectUpgradeDt
FROM tbl_project
WHERE CONVERT(DATE,[ProjectEndDt]) < DATEADD(DAY, 3,CONVERT(DATE,GETDATE()))
Try this:
SELECT ProjectEndDt
,ProjectRenewalDt
,ProjectUpgradeDt
FROM tbl_project
WHERE CAST(ProjectEndDt AS DATE) < CAST(DATEADD(DAY, 3, GETDATE()) AS DATE)

How to add a Date Range in the WHERE clause?

I have a query in SQL Server that I want to run every week which would display the data from the last 7 days.
I tried using:
SELECT something FROM tbl_name
WHERE DATE_SUB(CURDATE(),INTERVAL 7 DAY) <= date_col;
However, the above code is for MYSQL but I have SQL Server installed and I was wondering if anyone had a solution for this problem.
Try this:
SELECT something
FROM tbl_name
WHERE date_col >= dateadd(day, -7, getdate());
If you want to be sure the time component is removed (as suggested by CURDATE()):
SELECT something
FROM tbl_name
WHERE date_col >= cast(dateadd(day, -7, getdate()) as date);
EDIT:
The answer to the question in your comment is:
SELECT something
FROM tbl_name
WHERE date_col >= cast(dateadd(day, -7, getdate()) as date) and
date_col < cast(dateadd(day, 1, getdate()) as date);
Note the second condition is <, not <=. If date_col has no time component, then:
WHERE date_col >= cast(dateadd(day, -7, getdate()) as date) and
date_col <= cast(getdate() as date)
also works.

SQL statement to select all rows from previous day

I am looking for a good SQL Statement to select all rows from the previous day from one table. The table holds one datetime column. I am using SQL Server 2005.
get today no time:
SELECT dateadd(day,datediff(day,0,GETDATE()),0)
get yestersday no time:
SELECT dateadd(day,datediff(day,1,GETDATE()),0)
query for all of rows from only yesterday:
select
*
from yourTable
WHERE YourDate >= dateadd(day,datediff(day,1,GETDATE()),0)
AND YourDate < dateadd(day,datediff(day,0,GETDATE()),0)
To get the "today" value in SQL:
convert(date, GETDATE())
To get "yesterday":
DATEADD(day, -1, convert(date, GETDATE()))
To get "today minus X days": change the -1 into -X.
So for all yesterday's rows, you get:
select * from tablename
where date >= DATEADD(day, -1, convert(date, GETDATE()))
and date < convert(date, GETDATE())
It's seems the obvious answer was missing. To get all data from a table (Ttable) where the column (DatetimeColumn) is a datetime with a timestamp the following query can be used:
SELECT * FROM Ttable
WHERE DATEDIFF(day,Ttable.DatetimeColumn ,GETDATE()) = 1 -- yesterday
This can easily be changed to today, last month, last year, etc.
SELECT * from table_name where date_field = DATE_SUB(CURRENT_DATE(),INTERVAL 1 DAY);
Its a really old thread, but here is my take on it.
Rather than 2 different clauses, one greater than and less than. I use this below syntax for selecting records from A date. If you want a date range then previous answers are the way to go.
SELECT * FROM TABLE_NAME WHERE
DATEDIFF(DAY, DATEADD(DAY, X , CURRENT_TIMESTAMP), <column_name>) = 0
In the above case X will be -1 for yesterday's records
This should do it:
WHERE `date` = CURDATE() - INTERVAL 1 DAY
Can't test it right now, but:
select * from tablename where date >= dateadd(day, datediff(day, 1, getdate()), 0) and date < dateadd(day, datediff(day, 0, getdate()), 0)
In SQL Server do like this:
where cast(columnName as date) = cast(getdate() -1 as date)
You should cast both sides of the expression to date to avoid issues with time formatting.
If you need to control interval in more detail, then you should try something like:
declare #start datetime = cast(getdate() - 1 as date)
declare #end datetime = cast(getdate() - 1 as date)
set #end = dateadd(second, 86399, #end)
Another way to tell it "Yesterday"...
Select * from TABLE
where Day(DateField) = (Day(GetDate())-1)
and Month(DateField) = (Month(GetDate()))
and Year(DateField) = (Year(getdate()))
This conceivably won't work well on January 1, as well as the first day of every month. But on the fly it's effective.
Well, its easier to cast the datetime column to date and than compare.
SELECT * FROM TABLE_NAME WHERE cast(COLUMN_NAME as date) =
dateadd(day,0, convert(date, getdate(), 105))
A simple alternative
Select GETDATE() - 1
Change 1 to go back that many number of days
PS : This gives you timestamp accuracy.
This worked a charm:
SELECT * FROM mytable WHERE date(mydate) = DATE_SUB(CURDATE(), INTERVAL 1 DAY);
subdate(now(),1) will return yesterdays timestamp
The below code will select all rows with yesterday's timestamp
Select * FROM `login` WHERE `dattime` <= subdate(now(),1) AND `dattime` > subdate(now(),2)