I use the following Query to display the entries in my tables where the date is located in the current week. I need to change it to only show from current date to end of the week, not the hole week.
SET DATEFIRST 1
SELECT distinct Initials
FROM Scheme
WHERE CONVERT(datetime, Dato, 105) >= dateadd(day, 1-datepart(dw, getdate()), CONVERT(date,getdate()))
AND convert(datetime, Dato, 105) < dateadd(day, 8-datepart(dw, getdate()), CONVERT(date,getdate()))
AND RoomId = ? AND Initials IS NOT NULL
A bit different, but more effective method:
SELECT distinct Initials
FROM Scheme
WHERE Dato >= cast(current_timestamp as date) -- currentdate
AND Dato < dateadd(d, datediff(d, -7, current_timestamp)/7*7, 0)--end of week
AND RoomId = ? AND Initials IS NOT NULL
I assume Dato is defined as a date or datetime. Otherwise you may encounter some performance issues and this will not work.
dateadd(day, 1-datepart(dw, getdate()), CONVERT(date,getdate()))
This means: add 1-datepart(dw, getdate()) days to current date.
This means 1-datepart(dw, getdate()): go to starting date of current week.
But you need just current date CONVERT(date,getdate()).
Just change your code to:
SET DATEFIRST 1
SELECT distinct Initials
FROM Scheme
WHERE CONVERT(datetime, Dato, 105) >= CONVERT(date,getdate())
AND convert(datetime, Dato, 105) < dateadd(day, 8-datepart(dw, getdate()), CONVERT(date,getdate()))
AND RoomId = ? AND Initials IS NOT NULL
Related
I checked Get the records of last month in SQL server and it did not work!
I try to get the records of last month based on my database table and column issue_date.
What's the SQL query to do this?
For clarification, today (27-April-18) I want to get all records from March-18.
I have the issue_date in the format that I convert to date but below code gives me all records from 01-March-2018 to and including today.
DATEPART(month, CONVERT (VARCHAR(11),DATEADD(day,wo_header.issue_date,`1971/12/31`),106)) = DATEPART(month, DATEADD(month, -1, getdate()))
To get firstDay and lastDay of previous month
DECLARE #FirstDayOfLastMonth DATETIME = CONVERT(DATE, DATEADD(d, -(DAY(DATEADD(m, -1, GETDATE() - 2))), DATEADD(m, -1, GETDATE() - 1))),
#LastDayOfLastMonth DATETIME = CONVERT(DATE, DATEADD(d, -(DAY(GETDATE())), GETDATE()))
SELECT #FirstDayOfLastMonth, #LastDayOfLastMonth
Your required Query
SELECT *
FROM TABLE
WHERE CAST(issue_date AS DATE) BETWEEN CONVERT(DATE, DATEADD(d, -(DAY(DATEADD(m, -1, GETDATE() - 2))), DATEADD(m, -1, GETDATE() - 1))) AND CONVERT(DATE, DATEADD(d, -(DAY(GETDATE())), GETDATE()))
Perhaps the easiest method is eomonth(). If there is no time component on issuedate:
where issuedate > eomonth(getdate(), -2) and
issuedate <= eomonth(getdate(), -1)
If there is a time component:
where issuedate >= dateadd(day, 1, cast(eomonth(getdate(), -2) as date)) and
issuedate < dateadd(day, 1, cast(eomonth(getdate(), -1) as date))
Without eomonth, I would do:
where issuedate < cast(dateadd(day, 1 - day(getdate()), getdate()) as date) and
issuedate >= dateadd(month, -1, cast(dateadd(day, 1 - day(getdate()), getdate()) as date))
The code I figured out is not pretty but it works. It first adds extra column with the month number to the SELECT portion of my code:
Month(CONVERT (VARCHAR(11),DATEADD(day,wo_header.closing_date,'1971/12/31'),106)) As Month
And than is used for WHERE statement:
Month(CONVERT (VARCHAR(11),DATEADD(day,wo_header.closing_date,'1971/12/31'),106)) = month(getdate())-1
So for anyone like me working in SQL report kind-of environment it should work.
I have a table that tracks when a user scans a logo. It tracks the DATETIME of the scan and also the points awarded to the user for the scan (between 1 and 5)
I am trying to calculate the average points awarded per day for the given week. The challenge I have is when I use GROUP BY I'm getting back every scan because DATETIME value is by the second which can't be grouped.
SET DATEFIRST 1 -- Beginning of week is Monday
SELECT SUM(Points) AS Points, DateTime
FROM SmartTappScanLog
WHERE DateTime >= dateadd(day, 1-datepart(dw, getdate()), CONVERT(date,getdate()))
AND DateTime < dateadd(day, 8-datepart(dw, getdate()), CONVERT(date,getdate()))
GROUP BY DATETIME
I've tried several things including:
GROUP BY CAST(DATETIME As DATE)
You can use DATEPART(), like:
SELECT SUM(Points) AS Points, MIN(DateTime) as DateTime
FROM SmartTappScanLog
WHERE DateTime >= dateadd(day, 1-datepart(dw, getdate()), CONVERT(date,getdate()))
AND DateTime < dateadd(day, 8-datepart(dw, getdate()), CONVERT(date,getdate()))
GROUP BY DATEPART(day,DATETIME)
I am trying to fetch data for the previous full month, but the data from the last day is not fetching.
Output: Previous Month is April and this condition only retrieving data up to 29th April. Data for 30th April is missing.
Can please someone help me to correct this.
OLH.DateStamp > CONVERT(VARCHAR,DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE())-1, 0),101)
And
OLH.DateStamp < CONVERT(VARCHAR,DATEADD(MONTH, DATEDIFF(MONTH, -1, GETDATE())-1, -1),101))
Thanks in advance
The simplest way to get data for the previous month is:
DATEDIFF(month, OLH.DateStamp, GETDATE()) = 1
However, that does not use indexes. So, a better method is:
OLH.DateStamp >= DATEADD(MONTH, -1, DATEADD(DAY, 1 - DAY(GETDATE()), CAST(GETDATE() as DATE))) AND
OLH.DateStamp < DATEADD(DAY, 1 - DAY(GETDATE()), CAST(GETDATE() as DATE))
The expression:
DATEADD(DAY, 1 - DAY(GETDATE), CAST(GETDATE() as DATE))
returns midnight on the first day of the current month.
You should try last day of Month Should be <= OLH.Datestamp
To get Desired Output you are Looking for:
--Use It In Where Clause
DECLARE #FirstDayOfLastMonth
DATETIME = CONVERT(DATE, DATEADD(d, -( DAY(DATEADD(m, -1, GETDATE() -
2)) ), DATEADD(m, -1, GETDATE() - 1)))
,#LastDayOfLastMonth DATETIME = CONVERT(DATE, DATEADD(d, -( DAY(GETDATE()) ), GETDATE()))
OLH.DateStamp >= CONVERT(VARCHAR, #FirstDayOfLastMonth , 101) AND
OLH.DateStamp <= CONVERT(VARCHAR, #LastDayOfLastMonth , 101)
In my SQL Server procedure, I have one input parameter like created_date.
Based on that created date I have to find out first and last date of the month.
For example,
If created_date is 12-08-2016,
start_date should be '01-08-2016'
end_date should be '31-08-2016'
If Created_date is 15-06-2016
start_date should be '01-06-2016 00:00:00'
end_date should be '30-06-2016 23:59:59'
Since your input is in DD-MM-YYYY format, so it is need to covert first as MM-DD-YYYY then only it is easy to find the first and last day of the month.
The below query will accept the input and return the result in your expected format:
DECLARE #datevalue AS VARCHAR(20) = '15-06-2016'; --12-08-2016';
SELECT CONVERT(VARCHAR(10), DATEADD(M, DATEDIFF(M, 0, CONVERT(DATETIME, #datevalue, 105)), 0), 105) [start_date],
CONVERT(VARCHAR(10), DATEADD(D, -1, DATEADD(M, DATEDIFF(m, 0, CONVERT(DATETIME, #datevalue, 105)) + 1, 0)), 105) [end_date]
Output:
start_date end_date
----------------------
01-06-2016 30-06-2016
select
DATEADD(Month, DATEDIFF(Month, 0, GETDATE()), 0) as monthfirstdate,
CAST(EOMONTH(GETDATE()) as DATETIME) as monthlastdate
EOMONTH works from SQL server 2012
How do I compare an SQL Server date column against the current week?
For instance:
WHERE [Order].SubmittedDate = *THIS WEEK*
You could convert your date to a week number and compare this to the week number from the current date. Likewise, you'll need to compare the year as well, so that you don't get last year's weeks.
WHERE DATEPART(wk, [Order].SubmittedDate) = DATEPART(wk, GETDATE())
AND DATEPART(yy, [Order].SubmittedDate) = DATEPART(yy, GETDATE())
Assuming you are meaning always "this week" and there are no records with Submitted dates in the future, which I imagine could be the case you can do:
WHERE [Order].SubmittedDate >= DATEADD(dd, -(DATEPART(dw, GETDATE()) -1), GETDATE())
If dates do go into the future, the full restriction to this week is:
WHERE [Order].SubmittedDate >= DATEADD(dd, -(DATEPART(dw, GETDATE()) -1), GETDATE())
AND [Order].SubmittedDate < CAST(CONVERT(VARCHAR(10), DATEADD(dd, (8 - DATEPART(dw, GETDATE())), GETDATE()), 120) AS DATETIME)
I'd strongly recommend using a clause based on a start and end date like this, as it will allow efficient index use so should perform better.
Try this:
WHERE [Order].SubmittedDate BETWEEN
DATEADD(d, - DATEPART(dw, GETDATE()) + 1, GETDATE()) AND
DATEADD(d, 7 - DATEPART(dw, GETDATE()) , GETDATE())
Maybe this can run faster, as doesn't needs to be evaluated everytime:
DECLARE #StartDate DATETIME,
#EndDate DATETIME
SELECT #StartDate = DATEADD(d, - DATEPART(dw, GETDATE()) + 1, GETDATE()),
#EndDate = DATEADD(d, 8 - DATEPART(dw, GETDATE()) , GETDATE())
-- // Strip time part, so week starts on Sunday 00:00
SELECT #StartDate = CAST(FLOOR(CAST(#StartDate AS FLOAT)) AS DATETIME),
#EndDate = CAST(FLOOR(CAST(#EndDate AS FLOAT)) AS DATETIME)
...
WHERE [Order].SubmittedDate >= #StartDate AND [Order].SubmittedDate < #EndDate