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

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)

Related

Concatenate date and string to create datetime in SQL

I need to concatenate a datetime and a time field in SQL to a single date time.
e.g I have a datetime of 2017-09-05 00:00:00.000 and a string time of 11:00. What I want is a single field in a view of 2017-09-05 11:00:00.000
I have tried casting the datetime to a date and then concatenate the new date and string date field together but this doesn't work.
To cast the datetime I am using: CAST(dtDate AS DATE) AS dtNewDate which works fine. When I then use: CAST(dtNewDate + szTime AS datetime) AS dtNewDateTime the creation of the view works fine but selecting the top 1000 returns a "conversion failed when converting date and/or time from character string."
Is there an easier way to do this or can anyone offer some advise (other than storing the date and time in a single datetime field in the first place as it is populated by a third party application which I do not have access to change)
You can add two datetime values together, so try:
CAST(dtDate AS DATETIME) + CAST(CAST(szTime AS TIME) as DATETIME)
Assuming 11:00 stands for 11:00:00, you can do something like this:
SELECT dtDate + CONVERT(DateTime, szTime, 108)
FROM...
See a live demo on rextester
You can try following.
DECLARE #YourDate AS DATETIME
SET #YourDate = CONVERT(VARCHAR(10), '2017-09-05 00:00:00.000', 111) + ' 11:00'
PRINT #YourDate
another way is to get the hour from your stringfield, convert it to int, and add that as hours to the datetime
declare #date datetime = '20170905'
declare #stringtime varchar(5) = '11:00'
select left(#stringtime, 2),
dateadd(hour, convert(int, left(#stringtime, 2)), #date)
If you also need the minutes you can do it like this :
declare #date datetime = '20170905'
declare #stringtime varchar(5) = '11:05'
select left(#stringtime, 2),
right(#stringtime, 2),
dateadd(minute, convert(int, right(#stringtime, 2)), dateadd(hour, convert(int, left(#stringtime, 2)), #date))
This will only work if the stringfield is always in format hh:mm
If you care about precision with DATETIME2,
DECLARE #D DATETIME = '2017-01-01';
DECLARE #T varchar(7) = '11:00';
SELECT DATEADD(day, DATEDIFF(day, '19000101', #D), CAST(CAST(#T AS TIME) AS DATETIME2(7)));
Running example here

How to add date and time in SQL Server

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

How to check if DateTime lies within String times?

DECLARE #CurDate datetime,
#Begintime nvarchar(10),
#Endtime nvarchar(10)
SELECT #CurDate = GETDATE()
SELECT #Begintime = '9:00 AM'
SELECT #Endtime = '5:00 PM'
What is the best way to check if the CurDate lies within the given times? Thanks!
With SQL Server 2008 and higher, you can convert your current time, and the begin and end times (as nvarchar) into TIME variables and then do the check:
DECLARE #CurrTime TIME = CAST(#CurDate AS TIME)
DECLARE #TimeFrom TIME = CAST(#Begintime AS TIME)
DECLARE #TimeTo TIME = CAST(#Endtime AS TIME)
SELECT
#CurrTime, #TimeFrom, #TimeTo,
CASE
WHEN #CurrTime BETWEEN #TimeFrom AND #TimeTo THEN 'Yes!'
ELSE 'No, sorry'
END
Use the function DATEDIFF to calculate the hours and minutes difference.
Here -> https://msdn.microsoft.com/en-us/library/ms189794.aspx
I wouldn't use use nvarchar, but instead of it use something like this:
#BeginTime = DATEADD(hour, 9, CONVERT(datetime,CONVERT(date,getdate())))
#EndTime = DATEADD(hour, 17, CONVERT(datetime,CONVERT(date,getdate())))
The condition to use can be:
WHERE #CurDate BETWEEN #BeginTime AND #EndTime
Best regards,

Convert datetime and varchar to single date

I have a following database columns and the values
MyDate datetime and My_Time varchar(5)
the values stored are
2006-09-05 00:00:00.000 and 16:47
Now I want to add this two columns and get a single datetime value 2006-09-05 16:47:00.000
How can I do this in SQL ?
UPDATE:
Some rows have NULL values for DocDate and DocTime.
So i am getting error like Conversion failed when converting date and/or time from character string
Simply...
DECLARE #date DATETIME
DECLARE #time VARCHAR(5)
SET #date = '2006-09-05 00:00:00.000'
SET #time = '16:47'
SELECT CAST(#date + #time AS DATETIME) -- 2006-09-05 16:47:00.000
Try this:
select dateadd(ss, datediff(ss, 0, #My_Time), #MyDate)
Key point is to understand that it's the same as...
select dateadd(ss, datediff(ss, 0, cast(#My_Time as time)), #MyDate)
...but the conversion is done explicitly.
EDIT
For default time and/or date, use ISNULL or COALESCE as appropriate.
Example:
SELECT
CAST(
isnull(#date, '2000-1-1') +
isnull(#time, '0:0')
AS DATETIME)
Try
CAST(MyDate AS DATETIME) + CAST(MyTime AS DATETIME) as CombinedDate

Add Date parameter and time parameter to single date time

I'm modifying a store procedure in SQL Server 2008. The original procedure took in two dates
#StartDate DATETIME
#EndDate DATETIME
And would convert the time portion to the Earliest and latest possible times respectively.
I have added two additional parameters to accept Time Portions.
#StartTime DATETIME
#EndTime DATETIME
The Time portions are optional.
An RDL report file generates the report online for the users. The logic needs to occur in the Stored Proc.
What I have so far is not much, as I'm a C# programmer leaving my element.
IF (#StartTime IS NULL)
SET #StartDate = fn_SetBeginningTime(#StartDate) -- Sets time portion to earliest value
ELSE
-- This is where I don't know how to add the time from #StartTime to the time portion of the datetime value #StartDate
IF (#EndTime IS NULL)
-- This will do the same as the start time/start date
Assuming:
By earliest you mean 00:00:00 on the start date
By latest you mean 00:00:00 on the day after enddate (for use with <=)
Should there be any, this will ignore the time from a #*Date param and the date from a#*Time param.
declare #StartDate DATETIME = '15 jul 2010'
declare #EndDate DATETIME = '15 jul 2010'
declare #StartTime DATETIME = '06:06:06'
declare #EndTime DATETIME = null
--always make #StartDate/#EndDate's time 00:00:00
SET #StartDate = CAST(#StartDate AS DATE)
SET #EndDate = CAST(#EndDate AS DATE)
IF (#StartTime IS NOT NULL) -- set #StartDate's time to #StartTime
SET #StartDate += CAST(#StartTime AS TIME)
IF (#EndTime IS NULL)
SET #EndDate += 1 --set it to midnight
ELSE
SET #EndDate += CAST(#EndTime AS TIME) --set #EndDate's time to #EndTime
select #StartDate, #EndDate
>>> 2010-07-15 06:06:06.000,2010-07-16 00:00:00.000
For DATE / TIME;
declare #StartDate DATE = '15 jul 2010'
declare #EndDate DATE = '15 jul 2010'
declare #StartTime TIME = null
declare #EndTime TIME = '22:22:22'
declare #start datetime = cast(#StartDate as datetime) + coalesce(#StartTime, cast('00:00:00' as time))
declare #end datetime = cast(#EndDate as datetime) + coalesce(#EndTime, cast('23:59:59' as time))
select #start, #end