March 4, 2014 at 8:05 am
How can I calculate and return the previous Date at 18:00 Hours?
Here is a miserable attempt:
DECLARE @RunDate SmallDateTime
DECLARE @CurrentDate SmallDateTime
DECLARE @RunDateWoTime SmallDateTime
SET @CurrentDate = GETDATE()
SET @RunDate = DATEADD(day,-1,@CurrentDate)-- AS CurrentDate
SELECT @RunDate AS RunDate
-- Desired Result is the following:
-- 2014-03-03 18:00
For better, quicker answers on T-SQL questions, click on the following...
http://www.sqlservercentral.com/articles/Best+Practices/61537/
For better answers on performance questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
March 4, 2014 at 8:17 am
SELECT DATEDIFF(dd,0,GETDATE())-1
SELECT DATEADD(dd,DATEDIFF(dd,0,GETDATE())-1,0)
SELECT DATEADD(hh,18,DATEADD(dd,DATEDIFF(dd,0,GETDATE())-1,0))
Use q3. q1 and q2 help explain the algorithm.
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
March 4, 2014 at 8:21 am
Welsh Corgi (3/4/2014)
How can I calculate and return the previous Date at 18:00 Hours?Here is a miserable attempt:
DECLARE @RunDate SmallDateTime
DECLARE @CurrentDate SmallDateTime
DECLARE @RunDateWoTime SmallDateTime
SET @CurrentDate = GETDATE()
SET @RunDate = DATEADD(day,-1,@CurrentDate)-- AS CurrentDate
SELECT @RunDate AS RunDate
-- Desired Result is the following:
-- 2014-03-03 18:00
Could do something like:
DECLARE @RunDate SmallDateTime
DECLARE @CurrentDate DATE
DECLARE @RunDateWoTime SmallDateTime
SET @CurrentDate = GETDATE()
SET @RunDate = DATEADD(day,-1,@CurrentDate) + ' 18:00' -- AS CurrentDate
SELECT @RunDate AS RunDate
March 4, 2014 at 8:28 am
ChrisM@Work (3/4/2014)
SELECT DATEDIFF(dd,0,GETDATE())-1
SELECT DATEADD(dd,DATEDIFF(dd,0,GETDATE())-1,0)
SELECT DATEADD(hh,18,DATEADD(dd,DATEDIFF(dd,0,GETDATE())-1,0))
Use q3. q1 and q2 help explain the algorithm.
Chris solution is obviously better than the below but if you do not want the seconds part then try below
SELECT CONVERT(VARCHAR(10),GETDATE()-1,120) + ' 18:00' AS RunDate
March 4, 2014 at 2:42 pm
Thank you very much everyone!
For better, quicker answers on T-SQL questions, click on the following...
http://www.sqlservercentral.com/articles/Best+Practices/61537/
For better answers on performance questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
March 4, 2014 at 6:17 pm
ChrisM@Work (3/4/2014)
SELECT DATEDIFF(dd,0,GETDATE())-1
SELECT DATEADD(dd,DATEDIFF(dd,0,GETDATE())-1,0)
SELECT DATEADD(hh,18,DATEADD(dd,DATEDIFF(dd,0,GETDATE())-1,0))
Use q3. q1 and q2 help explain the algorithm.
I can do it with just two date function calls:
SELECT DATEADD(dd,DATEDIFF(dd,0,GETDATE())-1,'18:00')
I can also name that tune in 3 notes. π
My thought question: Have you ever been told that your query runs too fast?
My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.
Need to UNPIVOT? Why not CROSS APPLY VALUES instead?[/url]
Since random numbers are too important to be left to chance, let's generate some![/url]
Learn to understand recursive CTEs by example.[/url]
[url url=http://www.sqlservercentral.com/articles/St
March 5, 2014 at 6:43 pm
Thank you very much for all of the help.
For better, quicker answers on T-SQL questions, click on the following...
http://www.sqlservercentral.com/articles/Best+Practices/61537/
For better answers on performance questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
March 6, 2014 at 2:11 am
dwain.c (3/4/2014)
ChrisM@Work (3/4/2014)
SELECT DATEDIFF(dd,0,GETDATE())-1
SELECT DATEADD(dd,DATEDIFF(dd,0,GETDATE())-1,0)
SELECT DATEADD(hh,18,DATEADD(dd,DATEDIFF(dd,0,GETDATE())-1,0))
Use q3. q1 and q2 help explain the algorithm.
I can do it with just two date function calls:
SELECT DATEADD(dd,DATEDIFF(dd,0,GETDATE())-1,'18:00')
I can also name that tune in 3 notes. π
- and an implicit type conversion, which hides a note!
DATEADD(dd,DATEDIFF(dd,0,GETDATE())-1,CAST('18:00' AS DATETIME))
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
March 6, 2014 at 4:09 am
ChrisM@Work (3/6/2014)
dwain.c (3/4/2014)
ChrisM@Work (3/4/2014)
SELECT DATEDIFF(dd,0,GETDATE())-1
SELECT DATEADD(dd,DATEDIFF(dd,0,GETDATE())-1,0)
SELECT DATEADD(hh,18,DATEADD(dd,DATEDIFF(dd,0,GETDATE())-1,0))
Use q3. q1 and q2 help explain the algorithm.
I can do it with just two date function calls:
SELECT DATEADD(dd,DATEDIFF(dd,0,GETDATE())-1,'18:00')
I can also name that tune in 3 notes. π
- and an implicit type conversion, which hides a note!
DATEADD(dd,DATEDIFF(dd,0,GETDATE())-1,CAST('18:00' AS DATETIME))
Where's the fun in that? π
My thought question: Have you ever been told that your query runs too fast?
My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.
Need to UNPIVOT? Why not CROSS APPLY VALUES instead?[/url]
Since random numbers are too important to be left to chance, let's generate some![/url]
Learn to understand recursive CTEs by example.[/url]
[url url=http://www.sqlservercentral.com/articles/St
March 6, 2014 at 9:25 am
awesome:cool:
For better, quicker answers on T-SQL questions, click on the following...
http://www.sqlservercentral.com/articles/Best+Practices/61537/
For better answers on performance questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
Viewing 10 posts - 1 through 9 (of 9 total)
You must be logged in to reply to this topic. Login to reply