SQL - Transform GETDATE() into the exactly hour - sql

I've this:
SELECT GETDATE()
Which gives me :
2017-06-12 16:51:50.410
How can I convert this in order to get:
2017-06-12 16:00:00.000
I've some processes and I'm registering the date and hour of the job but I only want the exactly hour.
How can I do this?
Thanks

To get getdate() truncated to hour:
select dateadd(hour, datediff(hour, 0, getdate() ), 0)
This adds the number of hours since 1900-01-01 to the date 1900-01-01.
You can also swap hour for other levels of truncation: year, quarter, month, day, minute, second, et cetera.

I think the most human-readable solution is:
SELECT convert(datetime,FORMAT(GETDATE(),'yyyy-MM-dd HH:00:00.000'))
FORMAT to get the GETDATE timestamp exactly how you want it.
CONVERT to ensure the data type is a datetime.

Related

<Resolved> Fetch last date of a month and last minute of that day

I need to get last day of the month with time like
2023-01-31 23:59:59:000000
I'm able to get only the last day of the month with time stamp as
2023-01-31 00:00:00:000000
As jarlh said your best method is to add a day to the end of the month, then subtract a second (although if you really want the absolute maximum time I think you'd want to subtract 3 milliseconds).
EOMONTH -> Add 1 day -> Cast as datetime -> remove 1 second / 3 milliseconds. You have to cast as datetime because the EOMONTH function implicitly casts to a date
The code will be something like this:
SELECT DATEADD(SECOND, -1, CAST(DATEADD(DAY, 1, EOMONTH(#currentDate)) AS DATETIME))
SELECT DATEADD(MILLISECOND, -3, CAST(DATEADD(DAY, 1, EOMONTH(#currentDate)) AS DATETIME))
There are already similar questions with a lot of answers. You should find your anwer for sure:
Get the last day of the month in SQL
SQL query to display end date of current month
DECLARE #currentDate DATE = GETDATE()
SELECT EOMONTH (#currentDate) AS CurrentMonthED
SQL query to display end date of Next month
DECLARE #currentDate DATE = GETDATE()
SELECT EOMONTH (#currentDate, 1 ) AS NextMonthED

Round a timestamp

I would like to differentiate between two dates and round the result to the next day.
For exemple, if I have date1='2020-03-10 11:59:00' and date2='2020-03-10 20:53:00', the difference between date1 and date2 with datediff() is equal to 8 hours. I would like to round this result to have 24 hours.
EDIT
I tried by using dateadd() like this :
select DATEADD(HOUR, DATEDIFF(HOUR, '2020-03-10 11:59:00', '2020-03-10 20:53:00'),0)
Return 1900-01-02 09:00:00.000 doesn't correspond to what I want.
The explanation of the output is not at all clear so I am just guessing here. I am taking the difference in days between two dates. Then adding 1 because if two dates are the same day the difference in days is 0. Then multiplying that result by 24. I changed up the date literal so it is ANSI compliant and will always get the correct date regardless of localization or language settings.
declare #date1 datetime = '20201003 11:59:00'
, #date2 datetime = '20201003 20:53:00'
select datediff(day, #date1, #date2) + 1 * 24

How to show Current Date as Current Date ending at 3:00:00AM in SQL

I have a task that I need to show the current date time as ending at 3:00:00 AM at current date. For example, GETDATE() returns the current date time when executes. I need to show it as 9/5/2019 3:00:00 AM instead. Below is my code:
DECLARE #END_SHIFT AS DATETIME
SET #END_SHIFT = '06:00:00 AM'
SELECT
NUMBER_ID,
GETDATE() AS CURRENT_DT,
GETDATE() - #END_SHIFT AS END_SHIFT_DATE
FROM table
My issue when running this is it does not return as ending at 3:00:00AM. Please let me know your direction.
Thanks,
H
A bit of an odd request for sure but you could simply use DATEADD.
SELECT dateadd(hour, 3, convert(datetime, convert(date, getdate())))
If you really need a "hard" time, one option is to use format()
Example
Select format(GetDate(),'yyyy-MM-dd 03:00')
Returns
2019-09-05 03:00

Extract Date And Hour Only From Date Time Field

I am running SQL Server 2008 and I have a datetime field, from this field I only want to extract the date and hour. I tried to parse out using datepart() but I think I did it incorrectly as when I attempted to string the two back together I got a number value. What I attempted was
Select Datepart(YEAR, '12/30/2015 10:57:00.000') +
Datepart(HOUR, '12/30/2015 10:57:00.000')
And what was returned was 2025 What I want to see returned is
12/30/2015 10:00:00.000
You could use DATEADD and DATEDIFF:
DECLARE #d DATETIME = '12/30/2015 10:57:00.000';
SELECT DATEADD(second,DATEDIFF(second,'1970-01-01',#d)/3600*3600 , '1970-01-01')
-- 2015-12-30 10:00:00
LiveDemo
How it works:
Get seconds difference from 1970-01-01
Divide by 3600 (integer division so the part after decimal point will be skipped)
Multiply by 3600 to get value back to full hours
Add calculated seconds number to 1970-01-01
With SQL Server 2012+ the neat way is to use DATETIMEFROMPARTS:
SELECT DATETIMEFROMPARTS(YEAR(#d), MONTH(#d), DAY(#d), DATEPART(HOUR, #d),0,0,0)
LiveDemo2

SQL datetime needs to read 00:00:00.000

I have the following piece of SQL:
select DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,GETDATE()),0))
which comes through as this format:
2012-02-29 23:59:59.000
I need the exact piece of code with the date the same, however the time part must read 00:00:00.000
Modify* I should have been clear here: I need to have the last day of previous month at any given time (with the time in 00:00:00.000 format of course)
select dateadd(d,datediff(d,0,dateadd(s,-1,dateadd(m,datediff(m,0,getdate()),0))),0)
SELECT DATEADD(MONTH, -1, DATEADD(DAY, 0, DATEDIFF(DAY, 0, GETDATE())))
This will give you the last second of the prior month
select dateadd(s,-1,dateadd(month,datediff(month,0,GETDATE()),0));
and this will give you the last day of the prior month
select dateadd(day,-1,dateadd(month,datediff(month,0,GETDATE()),0));
More details of how to do this:
select dateadd(day,datediff(day,0,#datetime),0);
or
select dateadd(day,datediff(day,0,GETDATE()),0);
In English: Take the number of days between this date and 0 and add those days to 0.
This works with any parameter for datediff. So
select dateadd(month,datediff(month,0,GETDATE()),0);
Will "remove" all day information in addition to time information.
An alternative method to strip out the time portion is to cast it to a float, apply the Floor function and cast back to a datetime.
select Cast(Floor(Cast(DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,GETDATE()),0)) as float)) as datetime)
SELECT DATEADD(DAY, -1, DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0))
In SQL Server 2012 you could use eomonth.
SELECT EOMONTH(DATEADD(MONTH, -1, GETDATE()))
Saw some similar posts
select cast(cast(dateadd(dd,-1,getdate()) as date) as datetime)
Cast your dateadd as a date and then enclose it in another cast back to datetime
So it goes from this
2012-02-29 23:59:59.000
To this
2012-02-29
and the finally this
2012-02-29 00:00:00.000