I wish, from a given date, to be able to have the last hour "round". For example, if it's 8:49 am, I would like a variable to take the value 8:00 am.
Any idea on how to do it ?
You may try offsetting the input by the difference to the start of the hour:
SELECT DATEADD(hour, DATEDIFF(hour, 0, '2020-07-10 08:49:00'), 0);
This returns:
2020-07-10 08:00:00.000
Related
I want to add 1 day to date with time component.
My date is say for eg.
2020-09-10 18:30:00.000'
and I want to add 24 hours i.e expected output is
2020-09-11 18:30:00.000
I wrote SELECT DATEADD(day, DATEDIFF(day, 0, '2020-09-10 18:30:00.000'), 1) but this does not show the time component.
How to add date with time component?
SQL Server is really flexible about recognizing date formats, and will happily understand your string as a date if you put in within a date function. So, no need for explicit conversion from string to date, you can just do:
dateadd(day, 1, '2020-09-10 18:30:00.000')
Drop the datediff() in your attempt. And according to the documentation, the date is the third argument of the dateadd() function.
select dateadd(day, 1, convert(datetime, '2020-09-10 18:30:00.000') );
Output:
2020-09-11 18:30:00.000
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
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.
Using date range to select values, but also need to use an hour range to determine if a record should be selected. The date ranges and time ranges are not necessarily associated.
game_time (between 6 am and 6 pm)
have tried straight between statement and datepart, but cannot get anything to capture what we need.
create table gametime(name varchar, start_time datetime, end_time datetime)
insert assorted name, start_times and end_times
Desired results
name start_time end_time
name1 8:00 AM 10:00 AM
name2 8:00 AM 11:30 AM
name3 4:00 PM 5:30 PM
name4 6:00 PM 9:00 PM
datetime is used is storage, but not needed in presentation.. only times are needed in presentation.
Selected games should only start between the hours of 6:00 AM and 6:00 PM.
Any and all suggestions and insight appreciated......
Using
LTRIM(RIGHT(CONVERT(VARCHAR(20), start_time, 100), 7))
to get the correct format for presentation,
but when I try to use
LTRIM(RIGHT(CONVERT(VARCHAR(20), start_time, 100), 7)) > 6
I get conversion errors.
I would use DATEPART rather than relying on converting to/comparing strings:
WHERE DATEPART(hour,start_time) BETWEEN 6 AND 18
Try CONVERT(VARCHAR(5),start_time,108) BETWEEN '06:00' AND '18:00'. Right now you're trying to compare a string to an integer.
Here's another way, provided you're on SQL Server 2008 or higher and have the TIME type available:
SELECT *
FROM gametime
WHERE CAST(start_time AS TIME) BETWEEN '06:00:00' and '18:00:00'
This can be a bit more flexible when your time range is not anchored to exact hours. It also is sarg-able -- i.e. it will use an index, where calling DATEPART will prevent that.
I am attempting to insert tomorrow's date at 10:00:00 into SQL Server 2005/2008.
I currently have
DATEADD(dd, 1, GETDATE())
which gives me tomorrow's date fine, however I have tried a number of methods to also concat / set the time to 10:00:00.
Example:
Current Time: 2013-01-07 15:37:05
Required Insert Date: 2013-01-08 10:00:00
You can use a combination of DATEADD and DATEDIFF.
SELECT DATEADD(hh,10, DATEDIFF(dd,0, DATEADD(dd, 1, GetDate())))
DATEDIFF(dd,0...) truncates the time part of a date, hence "rounds" to midnight and DATEADD(hh,10...) adds 10 hours.
DEMO
One less DATEADD than the other answers:
SELECT DATEADD(day,DATEDIFF(day,'20010101',GETDATE()),'2001-01-02T10:00:00')
This adds the (integral) number of days since 1st January 2001 onto 10am on the 2nd January 2001.
Try this out:
DATEADD(hh,34,DATEDIFF(dd,0,GETDATE()))
This will work in Oracle - returns 10 a.m. of next day:
Select to_char((trunc(Sysdate)+1)+10/24, 'yyyy-mm-dd hh24:ss:mi') insert_date
From dual
/
SQL> 2013-01-08 10:00:00