I'm creating a query in SQL Server 2008, for an SSRS report that runs every Monday at 0330am, retrieving weekend sales details.
The report is ready, but I had to hard-code the date range, as I'm having trouble expressing that condition in the WHERE statement.
I need to retrieve data based on column [salestime] (of type datetime), between Friday at 1230pm and Monday at 330am.
I'd really appreciate your assistance with this.
How about something like:
WHERE SalesTime BETWEEN DATEADD(HH,-63,GETDATE()) AND GETDATE()
I believe the time values are 63 hours apart. This is if your report automatically runs at 3:30 AM, which is what it sounds like in your post.
If you want to run your report any time during the week to report on last weekend's sale
SET DATEFIRST 7
DECLARE #ThisMonday date = DATEADD(DAY, 2 - DATEPART(WEEKDAY, GETDATE()), GETDATE())
DECLARE #LastFriday date = DATEADD(DAY, -3, #ThisMonday)
DECLARE #StartTime datetime = CAST(#LastFriday AS datetime) + CAST('12:30' AS datetime)
DECLARE #EndTime datetime = CAST(#ThisMonday AS datetime) + CAST('03:30' AS datetime)
SELECT #ThisMonday, #LastFriday, #StartTime, #EndTime
Now you can filter your report with salestime BETWEEN #StartTime AND #EndTime.
I have a Tasks table that contains Date, Description and WorkerID columns. I want to write a stored procedure that selects all tasks from this week and the following week. Can someone help me get to a solution please? I am not so good at this.
If indexing (performance) isn't a concern then it's a simple expression if your week coincides with the SQL Server setup:
where datediff(week, getdate(), [Date]) between 0 and 1
The snippet below is another option that works with both dates and datetimes. It's possible to adapt it to work with any ##datefirst setting but I'm just going to assume that the setting matches with the start of week you're looking to report on.
where
[Date] >= dateadd(day, 1 - datepart(weekday, getdate()), cast(getdate() as date))
and [Date] < dateadd(day, 15 - datepart(weekday, getdate()), cast(getdate() as date))
The following should give you what you want (assuming Monday is the first day of the week, and that the DBMS is SQL Server):-
select *
from Tasks
where [Date]
between
dateadd(dd,-(datepart(dw,[Date])-2),[Date]) --Monday of this week
and
dateadd(dd,13,dateadd(dd,-(datepart(dw,[Date])-2),[Date])) --Sunday of next week
I have the following SQL Server code, that I need to translate to PL/SQL.
I'm trying to understand what this code does, can someone help me?
--STEP 0a:
Declare prior MONDAY and prior SUNDAY
DECLARE #ENDINGSUNDAY DATE
SET #ENDINGSUNDAY = --'2013-06-23' --PUT ENDING DATE FOR WEEK HERE(the sunday ending the week)
--/*
(SELECT MAX(DATE)
FROM DIM.DATE
WHERE DAY_OF_WEEK_DESC LIKE 'SUNDAY' AND DATE <=CAST(GETDATE()) AS DATE))
SELECT MAX(DATE) FROM DIM.DATE WHERE DAY_OV_WEEK_DESC LIKE 'SUNDAY' AND DATA <= SYSDATE
I´m writing a query where i get the last month, but with the time in zeros (if today is 2013-05-21 then i want to get 2013-04-21 00:00:00.000).
So I tried:
select (dateadd(month,datediff(month,(0),getdate())-1,(0)));
But I get the first day of the previous month.
Then I tried:
select dateadd(month, -1, GETDATE());
I get the right day, but I also get the current time (2013-04-21 11:41:31.090), and I want the time in zeros.
So how should my query be in order to get something like: 2013-04-21 00:00:00.000
Thanks in advance.
In SQL Server 2008 there is the date data type, which has no time attached. You can thus remove the time portion quite easily simply by converting, then performing the DateAdd.
SELECT DateAdd(month, -1, Convert(date, GetDate()));
This will return a date data type. To force it to be datetime again, you can simply add one more Convert:
SELECT Convert(datetime, DateAdd(month, -1, Convert(date, GetDate())));
You may not need the explicit conversion to datetime, though.
Note: "One month ago from today" could be defined in many different ways. The way it works in SQL server is to return the day from the previous month that is the closest to the same day number as the current month. This means that the result of this expression when run on March 31 will be February 28. So, you may not get expected results in certain scenarios if you don't think clearly about the ramifications of this, such as if you performed the one-month calculation multiple times, expecting to get the same day in a different month (such as doing March -> February -> January).
See a live demo at SQL Fiddle
The demo shows the values and resulting data types of each expression.
Try like this..
select Cast(Cast(dateadd(month, -1, GETDATE()) as Date) as Datetime);
You can use this , it's pretty simple and worked for me -
SELECT SUM( amount ) AS total FROM expenses WHERE MONTH( date ) = MONTH( curdate() ) -1
To get the previous month start date and end date
DECLARE #StartDate date;
DECLARE #EndDate date;
select #StartDate= DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE())-1, 0)
select #EndDate= DATEADD(MONTH, DATEDIFF(MONTH, -1, GETDATE())-1, -1)
Here are many common dates you may need to pull with logic
SELECT DATEADD(dd,DATEDIFF(dd,0,GETDATE()),0) -- Today Midnight
SELECT DATEADD(dd,DATEDIFF(dd,0,GETDATE()),-1) -- Yesterday Midnight
SELECT DATEADD(d,0,DATEADD(mm, DATEDIFF(m,0,GETDATE())-1,0)) -- First of Last Month
SELECT DATEADD(d,DATEPART(DD,GETDATE()-1),DATEADD(mm, DATEDIFF(m,0,GETDATE())-1,0)) -- Same Day Last Month
SELECT DATEADD(d,-1,DATEADD(mm, DATEDIFF(m,0,GETDATE()),0)) -- Last of Last Month
SELECT DATEADD(d,0,DATEADD(mm, DATEDIFF(m,0,GETDATE()),0)) -- First of this month
SELECT DATEADD(d,-1,DATEADD(mm, DATEDIFF(m,0,GETDATE())+1,0)) -- Last of this month
SELECT DATEADD(d,0,DATEADD(mm, DATEDIFF(m,0,GETDATE())+1,0)) -- First of next month
SELECT DATEADD(d,DATEPART(DD,GETDATE()-1),DATEADD(mm, DATEDIFF(m,0,GETDATE())+1,0)) -- Same Day Next Month
SELECT DATEADD(d,-1,DATEADD(mm, DATEDIFF(m,0,GETDATE())+2,0)) -- Last of next month
SELECT DATEADD(d,-1,DATEADD(mm, DATEDIFF(m,0,GETDATE())+12,0)) -- Last of prior month one year from now
SELECT DATEADD(dd,DATEDIFF(dd,0,DATEADD(DAY, 13-(##DATEFIRST + (DATEPART(WEEKDAY,GETDATE()) %7)), GETDATE())),0) -- Next Friday Midnight
I want to select all last 3 month dates from the table in SQL Server.
Data is like this:
Sunday 20-05-2012
Sunday 27-05-2012
Sunday 10-06-2012
Sunday 24-06-2012
Sunday 08-07-2012
Sunday 22-07-2012
Sunday 12-08-2012
Sunday 19-08-2012
Sunday 09-09-2012
Sunday 16-09-2012
Saving date column as varchar with day of week - bad idea. You can't write faster query, because you must convert all of time your field for using it in where clause. Also your query can't use indexes of date_column and each query will be use scan index.
With datetime column, query should be:
select date_column
from table_name
where date_column between dateadd(m, -3, getdate()) and getdate()
Try using this query:
select date_column from table_name
where datepart(m,date_column) > datepart(m,getdate())-3 and datepart(yy,date_column) >= datepart(yy,getdate())