Add time stamp to date in SQLserver - sql

Below fetches the last monday from the current date.
SELECT cast (DATEADD(wk, DATEDIFF(wk, 6, GETDATE()), 0) as datetime)
but it displays date as timestamp all zeros like this
2014-05-26 00:00:00.000
I want the time stamp to be at 6 am like this
05-26-2014 06:00:00
or else its ok even if I get the the exact last monday date with the current GETDATE() timestamp.
Thank you for the help!

Adding 6 hours
SELECT cast (DATEADD(HH,6,DATEADD(wk, DATEDIFF(wk, 6, GETDATE()), 0)) as datetime)

You can convert it using one of the many built-in formats.
This will display it in the format:
MM-DD-YYYY 06:00:00
and still fetch the last Monday date you want.
SELECT CONVERT(VARCHAR, DATEADD(wk, DATEDIFF(wk, 6, GETDATE()), 0), 110) + ' 06:00:00'

Related

Retrieve data between yesterday at 0700 and today at 0700

I am trying to come up with a SQL query that retrieves data starting from 0700 yesterday to 0700 today. I tried below, but I am not getting the correct values.
where datediff(hour, Incident_Call_Date_Time,getutcdate()) between 6 and 30
the data in the field is in the following format:
2020-10-28 22:16:30.000
Try this:
WHERE Incident_Call_Date_Time >= CONVERT(DATETIME, FORMAT(DATEADD(DAY, -1, GETDATE()), 'yyyy-MM-dd') + ' 07:00')
AND Incident_Call_Date_Time < CONVERT(DATETIME, FORMAT(GETDATE(), 'yyyy-MM-dd') + ' 07:00')
It gets yesterday and today's date as string (no time part), adds 07:00 to the dates as the new time part, and checks to see if your value is between those to new DATETIME values.
where Incident_Call_Date_Time
between dateadd(hh, 7, cast(cast(getutcdate() - 1 as date) as datetime))
and dateadd(hh, 7, cast(cast(getutcdate() as date) as datetime))

Get first day of previous month in YYYY-MM-DD format

How do I get the first day of previous month in yyyy-mm-dd format.
I have tried this
SELECT CONVERT(VARCHAR(10), DATEADD(s, -1, DATEADD(mm, DATEDIFF(m, 0, GETDATE()), 0)), 120)
You can use the EOMONTH function for this. This function is supposed to return the end of month so add one day to result:
SELECT DATEADD(DAY, 1, EOMONTH(GETDATE(), -2)) -- returns 2019-07-01 on 2019-08-28 01:02:03.004
Wrap the above expression inside FORMAT(..., 'yyyy-MM-dd') to format the result.
This will work:
SELECT CONVERT(VARCHAR(10),DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,GETDATE()),0)),23)
Output:
2019-07-31

How to create a time of 9pm today in SQL

I am trying to get a smalldatetime value of "9pm today" in a query. I thought I could use
DATEADD(HOUR, 21, CONVERT(date, GETDATE()))
but SQL Server doesn't like that - I get the error
The datepart hour is not supported by date function dateadd for data
type date.
Suggestions for a workaround?
Pretty simple, just cast date back to datetime after casting to date.
Thus you'll get current_date 00:00:00 and then add 21 hours:
select dateadd(hh, 21, cast(cast(getdate() as date) as datetime))
it is because dateadd's 3rd parameter should be datetime type, not date.
SELECT DATEADD(HOUR, 21, CONVERT(datetime,CONVERT(date, GETDATE())))
just add 21 / 24.0 to todays date
Select dateadd(day, datediff(day, 1, getDate()), 1) + (21 / 24.0)
First part, dateadd(day, datediff(day, 1, getDate()), 1), strips time from getdate(),
second part, + (21 / 24.0), adds fractional part of day equal to 9 am
This works because internally, SQL Server represents datetimes as two integers, one for the date, (number of days since 1 Jan 1900), and a second integer for the time, (number of ticks since midnight), which it combines into a decimal value where the integer part is the date integer, and the decimal part is the fraction of one day, so if you add 0.5 to a date, you get noon on that day, etc.
or, for comparison, using dateadd for hours,
Select dateadd(hour, 21, dateadd(day, datediff(day, 1, getDate()), 1))

SQL timestamp between hours that fall across midnight

I'm trying to find all records that would fall with in a current time period based on the current time. For example the current time is 12:30am and I need all the records from the previous day starting at 6:00pm till current time. Very new to SQL and any help would be appreciated.
Below is a screenshot of the table and the column of interest is the timestamp (datetime). I have not tried really anything concrete, am still struggling with just trying to figure out how I would go about it.
Table
The following query returns "yesterday at 6 PM":
select DATEADD(hh, -6, DATEADD(dd, DATEDIFF(dd, 0, getdate()), 0))
The inner DATEADD gets "today at midnight" (basically just truncates the time part from "now". Try running the following by itself to see this:
select DATEADD(dd, DATEDIFF(dd, 0, getdate()), 0)
The outer DATEADD then subtracts 6 hours from "today at midnight" to get "yesterday at 6 PM".
So, to get all records from a table with a time greater than yesterday at 6 PM you would put this expression in a WHERE clause, like this:
select * from MyTable
where MyDateField > DATEADD(hh, -6, DATEADD(dd, DATEDIFF(dd, 0, getdate()), 0))
The options to manipulate dates in SQL are limited only by your imagination :)
For example, in your comment below, you want to filter on "current shift", which could be 6AM-6PM or 6PM-6AM, depending on current time. This would be more complicated, but still doable, like this:
select * from MyTable
where MyDateField >=
case
when datepart(hh, getdate()) < 6 then -- if before 6AM
--yesterday at 6 PM
dateadd(hh, -6, dateadd(dd, datediff(dd, 0, getdate()), 0))
when datepart(hh, getdate()) between 6 and 18 then -- if between 6AM and 6PM
--today at 6 AM
dateadd(hh, 6, dateadd(dd, datediff(dd, 0, getdate()), 0))
else -- if after 6 PM
--today at 6 PM
dateadd(hh, 18, dateadd(dd, datediff(dd, 0, getdate()), 0))
end
and MyDateField <
case
when datepart(hh, getdate()) < 6 then -- if before 6AM
--today at 6 AM
dateadd(hh, 6, dateadd(dd, datediff(dd, 0, getdate()), 0))
when datepart(hh, getdate()) between 6 and 18 then -- if between 6AM and 6PM
--today at 6 PM
dateadd(hh, 18, dateadd(dd, datediff(dd, 0, getdate()), 0))
else -- if after 6 PM
--tomorrow at 6 AM
dateadd(hh, 30, dateadd(dd, datediff(dd, 0, getdate()), 0))
end
Notice how this reuses the same calculation for "today at midnight" and simply adds a variable number of hours to that, depending on the current hour of the day, which is where datepart comes in handy.
Good luck!
select dateadd(hour,-6,cast(cast(getdate() as date) as datetime))
This may be a bit convoluted, but the above is getting the current date and going back 6 hours. This will return 6PM from "yesterday".
You could take records where TIMESTAMP > the above.

Get Last Day of the Month with the time set to 00:00:00 on MSSQL

I had a query and I am trying to get the yesterday date at 00:00:00 and the previous month date at 00:00:00.
This is my query:
SELECT DATEADD( DD,-2, CONVERT( CHAR(8) , getdate() , 112 )) 'Yesterday',
CONVERT( CHAR(8) ,DATEADD(ms,-3, DATEADD(mm,0,DATEADD(mm,DATEDIFF(mm,0,GETDATE()),0))) , 112 ) 'Last Day of Last Month'
The result is as follows:
Yesterday:2015-05-24 00:00:00.000
Last Day of Last Month:20150430
What is wrong here? Even if I move the convert I still getting this format. Is my approach correct?
Try this:
select
dateadd(dd, datediff(dd, 0, getdate()) - 1, 0) as yesterday,
dateadd(dd, -1, dateadd(mm, datediff(mm, 0, getdate()), 0)) as lastDayOfLastMonth
For more common date routines, visit this article by Lynn Pettis
If you're using SQL Server 2012 or above, you can simply use EOMONTH() built-in function.
SELECT CAST(EOMONTH (CURRENT_TIMESTAMP, -1) AS DATETIME) will return you 2015-04-30 00:00:00.000