Insert Tomorrows Date at Specific Time - sql

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

Related

SQL Server 2019 - add day in date with time component

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

SQL get last hour from given hour

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

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

T-SQL : convert(datetime) to include/exclude certain hours

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.

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