Add days to Datetime in MSSQL leaving the Time intact [duplicate] - sql-server

This question already has answers here:
How to add days to the current date?
(10 answers)
DATEADD(day, -7, GETDATE()) - Does it take time into account?
(1 answer)
Closed 5 months ago.
I am sure this has to be a dup post, however i have search with no results.
I have a Table with column Datetime, i want to add for example 5 days to the Date but leaving the Time as it currently is
I have tried
SET date = DATEADD(DAY, 5, CAST(CAST(date AS DATE) AS DATE))
however this removes the time on the datetime
For example:
2022-09-28 10:59:45.467
2022-09-27 12:16:53.273
Would become:
2022-10-03 10:59:45.467
2022-10-02 12:16:53.273
This is an existing Database i have no control over, however i just want to manipulate some data for examples.
If someone wants to be super clever, then if possible the best solutions would be having the most recent Date to be todays date then any other date to be off set by 1 day prior (-1).
example:
1- 2022-09-28 10:59:45.467
2- 2022-09-27 12:16:53.273
3- 2022-09-24 09:46:24.124
4- 2022-09-22 10:22:51.248
5- 2022-09-20 07:31:12.232
6- 2022-09-20 08:41:00.147
7- 2022-09-20 10:12:13.245
8- 2022-09-15 12:16:53.145
Outcome:
1- 2022-10-03 10:59:45.467
2- 2022-10-02 12:16:53.273
3- 2022-10-01 09:46:24.124
4- 2022-09-30 10:22:51.248
5- 2022-09-29 07:31:12.232
6- 2022-09-28 08:41:00.147
7- 2022-09-27 10:12:13.245
8- 2022-09-26 12:16:53.145
But i guess this one is a big ask, the other solution is simple enough, the super clever solution would be a awsome thing i could add in powershell to automate the process in a single click.
Update
Oversight on my side, as pointed out in the comments, i do not know why i was using Cast.
SET date = DATEADD(DAY, 5, date)
Is working.

Related

How to set SSRS subscription time as an expression

I have a report and the report StartDate and EndDate parameters are using the expression as a default value below.
=DateAdd(DateInterval.Minute,0,DateAdd("h",7,DateAdd("h",-24,Today())))
=DateAdd(DateInterval.Minute,0,DateAdd("h",7,Today()))
When I execute the report, the report is starting from the day before at 7 AM to today 7 AM.
I would like to keep the report Start time and End time like this(07:00).
I also want to send the report to customer every day 7:30 AM but the report needs to be executed according to start date and end date paramaters.
Example: today 12.12.2019
Subscription time will be 07:30 AM
report needs to be running this time:
StartDate : 11/12/2019 07:00:00
EndDate : 12/12/2019 07:00:00
But when I schedule subscription every day and 7:30 AM, I received report from one day before 7:30 AM and today 7:30 AM.
I just want to see report from 7:00am to 7 am. Even if I change schedule time.
Could you please help me about this problem. How can I edit my subscription?
Is it possible to write an expression in "date/time from - date/time to" fields in subscription?
Btw, When I unclick “use Default” part, it always takes 11-12-2019 even 2 days after ☹
Time from needs to be one day before at 07:00 AM
Time to should be on that day at 07:00 AM
Do you have any suggestion for it?
Thanks
I resolved my issue. There are 2 solutions for it.
Option 1 :
In the report design If it is must to have those date parameters must be DATTEIME and to allow TIME factor as well then and if you want to run the report which is subscribed always for Yesterday 7:00 to today 7:00 am then I would not rely on sending any parameter values based on expressions …I would set up Date/Time Parameter in report design to allow null values and send null values as default from the subscription settings.
Then In report SP you can always add a clause at the TOP like
if #startDateTime is null AND #endDateTime is null
begin
set #startDateTime =CONVERT(VARCHAR(10), getdate(), 111);
set #startDateTime =dateadd(hh,7,( dateadd(d,-1,#startDateTime)))
set #endDateTime =dateadd(d,1,#startDateTime)
end
and let the rest SP be same
Option 2 :
If you can change the report parameters to be a type only DATE then its easy always send =Today() in your subscription parameter for both Start & End
Then In report SP you can always add a clause at the TOP like
if #startDateTime = #endDateTime
begin
set #endDateTime =CONVERT(VARCHAR(10), #endDateTime, 111);
set #endDateTime =dateadd(hh,7,#endDateTime)
set #startDateTime =dateadd(d,-1,#startDateTime)
end
and let the rest SP be same
Option 2 is better if they are ok to have Start & End date parameter as just DATE instead of DATETIME.
Any way Using any of these options do handle this in SP… you can always have control in future if they want to change time form 7:00 am to any other time …no need to change report design just update SP…2 minutes
You can schedule this report for any time of the day and it will always send them a report for Yesterday 7:00 to Today 7:00

Correct week number from datepart [duplicate]

This question already has answers here:
Isoweek in SQL Server 2005
(4 answers)
Closed 7 years ago.
The code returns:
set datefirst 1
select datepart(wk,'2016-01-01') - 1
but
set datefirst 1
select datepart(wk,'2015-12-31')
returns..53 :/
But in fact - this is the same week. There is more days belonging to 2015 in this week, so it should be "53" or "1" (the same value) for any dates in this particular week. Is this possible to avieve this without building dedicated procedure to analyse date and adjust returned value ?
I use SQL Server 2005
You probably want iso_week:
set datefirst 1
select datepart(iso_week,'2015-12-31') --53
select datepart(iso_week,'2016-01-01') --53
LiveDemo
EDIT:
Looks like that iso_week is supported from SQL Server 2008+.
Is this possible to avieve this without building dedicated procedure
to analyse date and adjust returned value ?
Probably you need to write custom code to calculate it.

Change the following to take in account for the new year

I'm very new to sql so im not sure how i would go about changing the line below to take in account for the new year. The sql server query i'm running will run on January 1st of 2014. Will this work in sql server 2008 or will it need to be changed for the new year to capture 2013?
DT.[DOS-DATE] between convert(date,GETDATE()-7) and convert(date,GETDATE())
This query takes records between the last 7 days and today. The year does not matter in that condition.
It's better to use explicit operators on date. Also, no reason to call GETDATE() twice. Try this instead:
DECLARE #Now DATETIME = GETDATE()
...
DT.[DOS-DATE] between convert(date,DATEADD(day, -7, #Now)) and convert(date,#Now)
However, technically your existing code will work.
Also, you may want to take a look at this for potential issues with using BETWEEN:
What do BETWEEN and the devil have in common?

Use a report parameter instead of GETDATE in stored procedure

I have a SQL query which is saved as a stored procedure. I am using three different stored procedures to go back 1, 2, or 3 days depending on today's date. This makes it possible for my dashboard users to go back and skip weekends when comparing stats. This actually works. It looks at todays date and if it is Sunday go back 2 days, if it is Monday go back 3 days otherwise go back 1 day. The dashboard is created in Report Builder. Below is shown the between dates if today is Sunday (i.e. skip 2 days and go back to Friday which is the last normal working day). So much for the background. My challenge is now the user of the dashboard would like a BeginDate and EndDate date picker parameter in the report so I can no longer use these hard coded date strings but will have to incorporate a parameter into them. How do I replace a parameter say called #BeginDate in the following line where it says "GETDATE()":
BETWEEN DATEADD(DAY, -2, DATEADD(DAY, DATEDIFF(DAY,0,GETDATE()), 0))
AND DATEADD(SECOND, -86401, DATEADD(DAY, DATEDIFF(DAY,0,GETDATE()),0))
And based upon the date the end user selects then go and use one of the three stored procedures. I think I can make this work somehow, but I cannot figure out how to replace GETDATE() above with a parameter entered in by the user. If I just replace it with a parameter it throws back an error. I'm sure there is a really smart way of doing this. I'm still looking!!!
If you are passing a new parameter into the stored prcoedure:
BETWEEN DATEADD(DAY, -2, #BEGINDATE) AND DATEADD(SECOND, -86401, #BEGINDATE)
Adding seconds seems unnecessarily complicated:
>= DATEADD(DAY, -2, #BEGINDATE) and XXX < DATEADD(DAY, -1, #BEGINDATE)

SQL query to search

i am new at sqlserver and i just started learning it , and i have a question .
**i want an SQL query that can bring up all rows that were added in the past 6 months. in table 'users' the date is in field 'joined' but its in UNIX time stamp format
**and i want then like another SQL query the same as above but it also with the results sets the field 'activate' in each row to 'yes'
i tried that code :
select * from users where time_of_post is like '07/**/2011'
** = anyday but i can`t implement it right .
Thank you a lot for your help in advance .
select *
from users
where datediff(mm, time_of_post, getdate()) <= 6
What you want to use is the DATEDIFF() function, and get the difference between today's day (GETDATE()) and the stored dates (time_of_post). If that is less than or equal to 6 months (as denoted by the first parameter, mm) then that should be what you're looking for.
EDIT:
If you're looking to use this logic for an UPDATE, you'd do something like this:
update users
set activation = 'yes'
where datediff(mm, time_of_post, getdate()) <= 6
and activation <> 'yes'

Resources