I want to select all the records from [Orders] that have a [Submissiondate] less than 7 days.
I'm completely stumped. This is the query I'm executing:
SELECT * FROM [Orders] WHERE ([SubmissionDate] < #SubmissionDate)
Doesn't work.
If you mean you want rows with SubmissionDate between #SubmissionDate and #SubmissionDate - 7 days, then this is how I would implement that in Transact-SQL:
WHERE [SubmissionDate] BETWEEN DATEADD(DAY, -7, #SubmissionDate)
AND #SubmissionDate
Note that BETWEEN implies >= and <=. If you need strict inequalities, make it something like this:
WHERE [SubmissionDate] > DATEADD(DAY, -7, #SubmissionDate)
AND [SubmissionDate] < #SubmissionDate
Assuming that the sql parameter #SubmissionDate is the date (and time) now. You could use the following query that will return those [Orders] submitted within the last 7 days:
SELECT * FROM [Orders] WHERE ([SubmissionDate] >= DATEADD(DD, -7, DATEADD(dd, 0, DATEDIFF(dd, 0, #SubmissionDate))))
Two important remarks to this solution:
Time 'part' is being removed from #SubmissionDate.
As there is no 'Date To' restriction, do includes the [Orders] submitted
'today' (until the time the query is being executed).
The following code is just to get the date 'part' only of a date-time (extracted from this other SO thread).
DATEADD(dd, 0, DATEDIFF(dd, 0, #SubmissionDate))
Try this
SELECT * FROM [Orders] WHERE [submission_date] < NOW() - INTERVAL 7
DAY;
You can also try this DATEDIFF
SELECT * FROM [Orders] WHERE datediff(d,SubmissionDate,GETDATE()) > 7
where GetDate() is today's date and the d is difference in days
select datediff(d,'2012/06/23',GETDATE())
should give you 7 because it's 7 days ago
Related
this is my first question here. Hopefully I´m clear enough what I´m searching for.
My problem is following:
On this analysis I want to get from the last 7 weeks, the summarized prices of each week. Its working with out any problems, but now I would like to add the weeks number of each week as alias.
In my tests I was using for example something like this:
DECLARE #week7 varchar(10)
SET #week7 = DATEPART(wk, GetDate())
One of my problems is, that I´m not allowed to work with "EXEC".
This is just an example of my analysis:
SELECT DISTINCT(
SELECT SUM(Price)
FROM tblBookingdata
WHERE(Datum BETWEEN DATEADD(wk, -7, DATEADD(DAY, 1 - DATEPART(WEEKDAY, GETDATE()), DATEDIFF(dd, 0, GETDATE()))) AND DATEADD(wk, -6, DATEADD(DAY, 1 - DATEPART(WEEKDAY, GETDATE()), DATEDIFF(dd, 0, GETDATE()))))) AS '7 weeks ago', (
SELECT SUM(Price)
FROM tblBookingdata
WHERE(Datum BETWEEN DATEADD(wk, -6, DATEADD(DAY, 1 - DATEPART(WEEKDAY, GETDATE()), DATEDIFF(dd, 0, GETDATE()))) AND DATEADD(wk, -5, DATEADD(DAY, 1 - DATEPART(WEEKDAY, GETDATE()), DATEDIFF(dd, 0, GETDATE()))))) AS '6 weeks ago'
I would like the column name to show the week number from each sub select. That the output would be for example for this week: 40 (as column name) and 900 as price summary.
So I tried to work here with DECLARE and assign #week7 for example with the current week number. But here I got stuck, due it seems like I need to work here with EXEC.
Is this only possible with "EXEC" or are there any other solutions to solve this? I was looking in the www, but currently I´m stucking a bit. Thankful for every help! :)
I think the DateDiff function is your friend here. Are you using SQL Server? This won't display a row for the week if there are zero records in that week, but this should be close to what you want.
select WeeksAgo, sum(Price) as Price from (
select
Price
,Datediff(wk, Datum, getDate()) as WeeksAgo
,Datum --not used
from
tblBookingdata
)DataByWeek
where WeeksAgo between 0 and 7 --should this be 0-6?
group by WeeksAgo
I think you're looking for something like this. The prior 7 weeks are calculated from GETDATE based on a numbers table with 1, 2, 3, ... 7. Then the booking Prices are summarized by week where the Datum is within the prior 7 weeks. This will display NULL in price_sum if there were no sales that week.
drop table if exists #tblBookingdata;
go
create table #tblBookingdata(
Datum date not null,
Price int not null);
go
;with
weeks_cte(wk) as (
select datepart(wk, dateadd(wk, w*-1, getdate()))
from (values (1),(2),(3),(4),(5),(6),(7)) v(w)),
bookings_cte(wk, price_sum) as (
select datepart(wk, Datum), sum(Price)
from #tblBookingdata
where Datum>dateadd(wk, -7, getdate())
group by datepart(wk, Datum))
select *
from weeks_cte wc
left join bookings_cte b on wc.wk=b.wk;
I am trying the below query, but it's output comes last 2 weeks data, but I need only last before week data only.
select * from tablename where createddate>=DATEADD(WEEK,-2, GETDATE()) ;
Just add another condition to your WHERE clause to restrict to earlier than the week before last:
SELECT * FROM tablename
WHERE createddate >= DATEADD(WEEK,-2, GETDATE()) AND
createddate < DATEADD(WEEK,-1, GETDATE())
From one your comments I found that the week you are talking about starts from Friday, so you need to add up those gap days into your condition
SELECT * FROM tablename
WHERE createddate >= DATEADD(ww, DATEDIFF(ww, 4 ,DATEADD(WEEK, -1, GETDATE())), 4)
AND createddate < DATEADD(ww, DATEDIFF(ww, 4 ,DATEADD(WEEK, 0, GETDATE())), 4)
this code is currect answer for my quation,
select * from dbo_OrdersCompleteView1 where S2_DateTimeOrederLines between DATEADD(WEEK,-2, GETDATE()) and DATEADD(WEEK,-1,GETDATE());
I have table containing one datetime column. I need to return rows for only last 6 months. This can be done by
where datetime_column > DATEADD(m, -6, current_timestamp)
But how to extend this option if I want to return latest month beginning with first day of the month? E.g. I run this condition in the middle of month (14/6/2000), the latest row is set to 14/1/2000, but i would like to return it as 1/1/2000. Any advice?
I tried some subqueries (max function of datetime including month function) but with no success.
For MS SQL Server, you can use:
where datetime_column >= Dateadd(Month, Datediff(Month, 0, DATEADD(m, -6,
current_timestamp)), 0)
In SQL Server
where datetime_column > dateadd(m, -6, getdate() - datepart(d, getdate()) + 1)
SQLFiddle demo
In MySQL
where datetime_column > curdate() - interval (dayofmonth(curdate()) - 1) day - interval 6 month
SQLFiddle demo
Try this one
where datediff(month, datetime_column, getdate()) <= 6
To exclude or filter out future dates
where datediff(month, datetime_column, getdate()) between 0 and 6
This part datediff(month, datetime_column, getdate()) will get the month difference in number of current date and Datetime_Column and will return Rows like:
Result 1 2 3 4 5 6 7 8 9
10
This is Our final condition to get last 6 months data
where result <= 6
.... where yourdate_column > DATE_SUB(now(), INTERVAL 6 MONTH)
select *
from tbl1
where
datetime_column >=
DATEADD(m, -6, convert(date, convert(varchar(6), getdate(),112) + '01'))
I have a SQL data calculation which is used as part of where clause to get bookings from calculated date at midnight.
My solution:
bookDate >= (SELECT DATEADD(dd, -7, DATEADD(dd, DATEDIFF(dd, 0, GETDATE()), 0)))
The original was:
bookDate >= DATEADD(dd, -7, GETDATE())
However it returns at calculated date + current time
Is there a alternative and far simpler approach to this?
This is a bit simpler.
bookDate >= DATEADD(dd, DATEDIFF(dd, 0, GETDATE()) - 7, 0)
In SQL Server 2008 and SQL Server 2012 you can use the date data type.
bookDate >= DATEADD(dd, -7, CAST(GETDATE() as DATE))
The following will also work for 2005:
SELECT DATEADD(dd, -7, FLOOR(CAST(GETDATE() AS FLOAT)))
This works because SQL Server (and windows, for that matter) stores a date as a floating point, with the whole number representing the number of days is 01/01/1900, and the fraction part representing the time. The following is shorter, and more consistent with what I usually use in this situation:
SELECT FLOOR(CAST(GETDATE() AS FLOAT) -7)
DATEADD is useful if you're calculating on something other than days (i.e. months, years), because of the varying number of days is each given month or year. When working with days, it's often easier to add or subtract directly. Similarly, if you wanted to subtract, for example, two hours from a date, you can use:
SELECT CAST(GETDATE() AS FLOAT) * 2.0/24.0
You could also do it like this:
bookDate >= CAST(CONVERT(char(8), GETDATE() ,112) as datetime)
In this type of code,
AND Orders.ShipDate >= DATEADD(Day, Datediff(Day,0, GetDate() -6), 0)
It supposed to pull records with the date 6 days ago, until today. How can I make it pull records from 7 days ago until yesterday?
I know changing -6 to -7 will pull records from 7 days ago, but which variable is the end of the date span so I can change it to -1?
It's not a date span.
The condition you have there is really only one condition: greater than. The right side of the greater than is 6 days ago, so your condition matches any date that is later than the date six days ago. In other words, it doesn't stop at Today; it includes tomorrow, next week, and next year, too.
AND ( Orders.ShipDate >= DATEADD(Day, Datediff(Day,0, GetDate() -7), 0)
AND Orders.ShipDate < DATEADD(Day, Datediff(Day,0, GetDate()), 0) )
That's what you really want. It matches dates which are later than midnight of the day 7 days ago, and dates which are before midnight today (which is any time yesterday).
The "end of the date span" isn't in your query.
Change your code to:
AND (Orders.ShipDate BETWEEN DATEADD(Day, -1, GetDate()) AND DATEADD(Day, -7, GetDate()))
This should work too and it eliminates the needless addition of 0 days:
select *,DATEDIFF(Day, orders.ShipDate, GETDATE()) AS DAYS_SINCE_TODAY
from Orders
where DATEDIFF(Day, orders.ShipDate, GETDATE()) >= 1 AND --This many days since today
DATEDIFF(Day, orders.ShipDate, GETDATE()) <= 7 --Going back this many days
I like the BETWEEN function. This may be a different flavor of SQL than you are using, but this is what I use.
BETWEEN dateadd(second, 0, dateadd(dd,datediff(dd,7,getdate()),0)) AND dateadd(second,-1, dateadd(dd,datediff(dd,0,getdate()),0))