How to add date and time in SQL Server - sql

I have two variables #date of type datetime and #time of type time. I want to add both to get another datetime variable. And I want to perform further calculations on it.
Ex:
Declare #date datetime
Declare #time time
I want something like this
#date = #date + #time (but not concatenation)
SELECT #Startdate = DATEADD(DAY, -1, #date )
Is there any way?

You can tranform your time to seconds and add them to your datetime value:
DECLARE #datetime DATETIME = GETDATE(),
#time TIME = '01:16:24',
#timeinseconds INT
PRINT 'we add ' + CAST(#time AS VARCHAR(8)) + ' to ' + CONVERT(VARCHAR,#datetime,120)+ ':'
SELECT #timeinseconds = DATEPART(SECOND, #time)
+ DATEPART(MINUTE, #time) * 60
+ DATEPART(HOUR, #time) * 3600
SET #datetime = DATEADD(SECOND,#timeinseconds,#datetime)
PRINT 'The result is: ' + CONVERT(VARCHAR,#datetime,120)
Output:
we add 01:16:24 to 2015-07-17 09:58:45:
The result is: 2015-07-17 11:15:09

The only thing you are missing is that #time needs to be cast back to a datetime before adding to #date.
declare #date datetime = '2022-05-26'
declare #time time = '09:52:14'
declare #Startdate datetime
set #date = #date + convert(datetime,#time)
SELECT #Startdate = DATEADD(DAY, -1, #date)
Produces:

If you need to take only date part from #date and time part from #time - can convert your #date and #time to strings, concatenate the values and convert back to datetime:
select cast(convert(nvarchar(20), #date, 104) + ' ' +
convert(nvarchar(20), #time, 108) as datetime2)
Or, alternatively, if you need to add time to datetime value, you can do something like:
select dateadd(ms,
datepart(ms, #time),
dateadd(ss,
datepart(ss, #time),
dateadd(mi,
datepart(mi, #time),
dateadd(hh, datepart(hh, #time), #date))))

First of all convert #date and #time variables to NVARCHAR(), then concat them and after It convert It to DATETIME datatype. After It you can use DATEADD function on It. Try in following:
DECLARE #date DATETIME
DECLARE #time TIME
SET #date = GETDATE()
SET #time = '10:12:13'
SELECT DATEADD(DAY, -1, CAST(CONVERT(NVARCHAR(20), #date, 110) + ' ' +
CONVERT(NVARCHAR(20), #time, 108) AS DATETIME))
OUTPUT (Today day -1 + time '10:12:13'):
2015-07-16 10:12:13.000

I'm not sure what's going on here, but if your variables are datetime and time types, this should work just fine:
declare #date datetime
declare #time time
set #date = '20150717'
set #time = '12:34:56'
set #date = #date + #time
select #date, DATEADD(DAY,-1,#date)
See SQL Fiddle
If the problem is that #date contains also time part, you can use:
set #date = convert(datetime, convert(date, #date)) + #time

Your code is correct.
DECLARE #date DATETIME = '1/1/2020'
DECLARE #time TIME = '1:00 pm'
DECLARE #Startdate DATETIME
SET #date = #date + #time
SELECT #Startdate = DATEADD(DAY, -1, #date)
#date = 2020-01-01 13:00:00.000
#Startdate = 2019-12-31 13:00:00.000
It isn't concatenating, it is adding them together. The time on #date is 0:00:00.000, so it might appear to be concatenating them. But change #date to '1/1/2020 1:00 am' and then:
#date = 2020-01-01 14:00:00.000
#Startdate = 2019-12-31 14:00:00.000

Related

Getting records between two dates changing at 04:00AM instead of 00:00

I am trying to get the orders records between today date at 11 am until tomorrow date until 4 am.
I have tried the below code and start_date results 2017-04-20 11:00:00.000 and end date 2017-04-21 04:00:00.000
but the problem is when the clock reach 12 am at (Midnight) the start_date results 2017-04-21 11:00:00.000 and end date 2017-04-22 04:00:00.000 start date and end date should have the same above values.
How can I solve this issue
Query
DECLARE #start_date datetime = CONVERT(datetime, CONVERT(char(9), CURRENT_TIMESTAMP, 112) + '11:00');
DECLARE #end_date datetime = DATEADD(HOUR, 17, #start_date)
SELECT * from [dbo].[Orders] where [OrderDate] between #start_date and #end_date
Just check if your current date is before 4AM and substract a day in this case.
DECLARE #from datetime
set #from = getdate();
if (DATEPART(HOUR, #from) < 4) set #from = dateadd(day, -1, #from);
DECLARE #start_date datetime = CONVERT(datetime, CONVERT(char(9), #from, 112) + '11:00');
DECLARE #end_date datetime =
SELECT * from [dbo].[Orders] where [OrderDate] between #start_date and #end_date
Test :
DECLARE #from datetime
--set #from = getdate();
set #from = '2017-04-21 02:00' -- We test a value instead of using the current datetime
if (DATEPART(HOUR, #from) < 4) set #from = dateadd(day, -1, #from);
DECLARE #start_date datetime = CONVERT(datetime, CONVERT(char(9), #from, 112) + '11:00');
DECLARE #end_date datetime = DATEADD(HOUR, 17, #start_date)
SELECT #start_date, #end_date -- Test result
SELECT * from [dbo].[Orders] where [OrderDate] between #start_date and #end_date
Testing it with 2017-04-21 02:00, it returns : 2017-04-20 11:00 and 2017-04-21 04:00

How to print a week in date in SQL

I use sql server.
what I've been doing is this:
LEFT(yearmonth,4) + RIGHT( '0' + CONVERT(VARCHAR, DATEPART(WK, yearmonthdate)), 2)
yearmonth = '201601'
and
yearmonthdate = '20160101' through '20160131'
which prints out like this:
201601
201602
but I want to print out like the following:
20160101-20160102
20160103-20160109
respectively.
How do I accomplish that?
I've on google but I couldn't get to print out like that.
Thank you in advance.
DECLARE #Table TABLE (Col1 INT, Col2 DATETIME)
DECLARE #StartDT DATETIME
DECLARE #tempDT DATETIME
DECLARE #EndDT DATETIME
SET #StartDT = DATEFROMPARTS(YEAR(getdate()),MONTH(getdate()),1)
SET #EndDT = EOMONTH (#StartDT)
set #tempDT=#StartDT
WHILE #StartDT < #EndDT
BEGIN
PRINT
CONVERT(VARCHAR,cast(#tempDT as date))
+ ' - ' +
convert(VARCHAR,cast(DATEADD(dd, 7-(DATEPART(dw, #StartDT)), #StartDT) as date))
SET #StartDT = DATEADD(dd, 7-(DATEPART(dw, #StartDT)), #StartDT)
SET #tempDT = DATEADD(dd,1,#StartDT)
SET #StartDT = DATEADD(WEEK,1,#StartDT)
END
PRINT
CONVERT(VARCHAR,cast(#tempDT as date))
+ ' - ' +
convert(VARCHAR,cast(#EndDT as date))

How to get the difference between two datetimes in hours and minutes

If I have two datetimes like this :
transtime_in, transtime_out
How to get the difference between those datetimes in the following format :
hh:mm
I use
DATEDIFF(hour, transtime_in, transtime_out)
but i get the hours only .
Try this one -
Query:
DECLARE
#transtime_in DATETIME
, #transtime_out DATETIME
SELECT
#transtime_in = '2013-05-19 08:58:07.000'
, #transtime_out = '2013-05-19 16:40:53.000'
SELECT LEFT(CONVERT(VARCHAR(10), #transtime_out - #transtime_in, 108), 5)
Output:
-----
07:42
declare #D1 datetime
declare #D2 datetime
set #D1 = '2014-03-25 00:00:00.000'
set #D2 = '2014-03-24 17:14:05.000'
--select datediff(hour, cast(#D1 as time(0)), cast(#D2 as time(0)))
SELECT LEFT(CONVERT(VARCHAR(10), #D2 - #D1, 108), 8)
declare #D1 datetime
declare #D2 datetime
set #D1 = '2014-03-25 00:00:00.000'
set #D2 = '2014-03-24 17:14:05.000'
SELECT LEFT(CONVERT(VARCHAR(10), #D1 - #D2, 108), 8)

Difference between two dates in exact number of hours in SQL

I would like to calculate the exact hours difference between two datetime variables. The hours difference should be exact like this:
1.5
2
6.25
Anybody please help out..Thanks in advance...
You could use DATEDIFF to find the difference in minutes and convert that into hours:
select datediff(mi, startdate, enddate)
Assuming 1.5 means 1 hour and 30 minutes you could simply divide the result by 60:
select datediff(mi, startdate, enddate) / 60.0
Keep it simple:
declare #date1 datetime
declare #date2 datetime
select #date1 = GETDATE();
select #date2 = '2013-02-02 14:05'
select DATEDIFF(hh, #date2, #date1)
Results
-----------
71
(1 row(s) affected)
it will help you....
Declare #Date1 dateTime
Declare #Date2 dateTime
Set #Date1 = '22:30:00'
Set #Date2 = '00:00:00'
Select Cast((#Date1 - #Date2) as Float) * 24.0
To get Exact Time Difference in HH:MM try the below code in MS-SQL
Declare #InTime datetime='2017-11-27 10:00:00',
#OutTime datetime='2017-11-27 11:15:00'
SELECT CONVERT(varchar(5),DATEADD(minute,DATEDIFF(minute,#InTime,#OutTime),0), 114)
-----------
Result
01:15
Please try:
declare #dt1 datetime, #dt2 datetime, #Seconds int
select #dt1='2013-02-05 14:05:55.113', #dt2 =getdate()
set #Seconds=datediff(second, #dt1, #dt2)
declare #Hour nvarchar(50)
declare #Min nvarchar(50)
declare #MinTemp int
if #Seconds >0
begin
set #Hour=cast((#Seconds / 3600) as nvarchar(20)) +' Hrs '
set #MinTemp= (#Seconds % 3600) / 60
set #Min=cast(#MinTemp as nvarchar(20))
if #MinTemp<10
select #Hour+'0'+#Min +' Min'
else
select #Hour+#Min +' Min'
end
else
select '00 Hrs 00 Min'

SQL need to subtract certain time from date parameter passed into query

I have a date parameter so the date and time can always change.
For this example the datetime is '2010-07-06 14:46:37.577'
I need to see how much time is between this date paramter and the time of '17:00:00.000'
The time of 5PM will never change but as I said the date paramter can change.
declare #MyDate datetime
set #MyDate = '2010-07-06 14:46:37.577'
select DATEDIFF(MINUTE, #MyDate, CONVERT(varchar(10), #Mydate, 101)+' 17:00:00')
DECLARE #DateParameter datetime
DECLARE #DateTime5PM datetime
SET #DateParameter = '2010-07-06 14:46:37.577'
SET #DateTime5PM = CAST(CONVERT(varchar, #DateParameter, 101) + ' 17:00' AS datetime)
SELECT DATEDIFF (MI, #DateParameter, #DateTime5PM)