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());
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;
SQL query to get the 15 of the month for the following year.
Today
select getdate() = 2017-08-23 17:05:24.143
Looking for: 2018-8-15 00:00:00
I know how to get a year from today:
select dateadd(year,1,datediff(day,0,getdate()))
I know how to get the beginning of the month:
SELECT DATEADD(month, DATEDIFF(month, 0, GETDATE()), 0)
However I am having trouble combining the two.
You can use datefromparts for SQL Server versions 2012 and above.
select datefromparts(year(getdate())+1,month(getdate()),15)
Truncate the current day to the start of the month with the code you have, but add 12 months (so, a year), and add 14 days.
select dateadd(day,14,dateadd(month, 12+datediff(month, 0, getdate()), 0))
SELECT DATEADD(month, DATEDIFF(month, 0, DATEADD(year, 1, DATEDIFF(day, 0, GETDATE())), 0)
Vamsi Prabhala's answer deserves to be accepted.
But, you should consider creating a Calendar Table because it greatly simplifies working with dates in general.
Here is a pretty simply query that yields the results that you want:
select * from Calendar C where C.year = datepart(year, getdate()) + 1
and C.day_of_month = 15 and C.month = datepart(m, getdate())
Rextester Demo
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)
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 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