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)
Related
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
my this code returns date time but with seconds milliseconds , i don't want milliseconds and seconds. I tried but not working, help. In this format.
Output:
Code:
Set #DateFrom = Convert(date,(Select min(ReceivedMessages.ReceivedDateTime) from ReceivedMessages))
Set #DateTo = Convert(date,(Select max(ReceivedMessages.ReceivedDateTime) from ReceivedMessages))
SELECT [ID]
,LEFT(REPLACE(convert(varchar, ReceivedMessages.ReceivedDateTime, 113), ' ','/'), 11) + ' ' +
RIGHT(REPLACE(convert(varchar, ReceivedMessages.ReceivedDateTime, 113), ' ','/'), 12)
as RecievingDate
,[FromMobileNo]
,[Message],
[IsComplaint]
FROM [CmsSMSDb].[dbo].[ReceivedMessages]
where Convert(date,ReceivedDateTime)>= #DateFrom AND Convert(date,ReceivedDateTime)<= #DateTo
AND IsComplaint = 2
Try this, but replace GETDATE() with your time
SELECT CONVERT(VARCHAR(16), GETDATE(), 121)
declare #dtime datetime;
set #dtime = getdate();
select REPLACE(CONVERT(varchar(11), #dtime, 113), ' ', '/')
+ RIGHT(CONVERT(varchar(17), #dtime, 113), 6)
You can round datetime to the nearest minute like this:
DECLARE #CaptureDate datetime
SELECT #CaptureDate = DATEADD(minute, DATEDIFF(minute, 0, DATEADD(second, 30 - DATEPART(second, GETDATE() + '00:00:30.000'), GETDATE())), 0)
SELECT CONVERT(VARCHAR(16), #CaptureDate, 120)
Change GETDATE() to your date fields as applicable.
I have a table with datekey column values(20120728,20120728...) in format of yyyymmdd as int type, I need to make them into date format of mm/dd/yyyy while writing select statement.
DECLARE #date int;
SET #date = 20131107
SELECT CONVERT(date, CONVERT(varchar(8), #date), 112)
Please Try This
DECLARE #date int;
SET #date = 20120728
SELECT CONVERT(varchar(20), CONVERT(date, CONVERT(varchar(8), #date), 112),110)as datetime
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'
I am working on a query something require DATE!!
DECLARE #YesterDay DATETIME, #Today DATETIME
SET #YesterDay = DateAdd(DD, DateDiff(DD, 0, GETDATE())-1, 0)
SET #Today = DateAdd(DD, DateDiff(DD, 0, GETDATE()), 0)
select #YesterDay = convert(varchar, getdate()-1 , 110)
select #Today = convert(varchar, getdate() , 110)
EXEC #return_value = [dbo].[post_sec_admin_list_user_log]
#pDateFr = #YesterDay ,
#pDateTo = #Today,
#pName = '',
#pSec = NULL
#DateFr is varchar(50)
#DateT0 is varchar(50)
the #dateFr and #dateTo are both varchar..
And I try to execute it, it print the time format as this 2011-06-09 16:15:38.927
error statement
The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.
Additionally, the varchar format I need is MM-DD-YYYY
Anyone know where is my error at?
thanks
Your code is confusing:
DECLARE #YesterDay DATETIME, #Today DATETIME
SET #YesterDay = DateAdd(DD, DateDiff(DD, 0, GETDATE())-1, 0)
SET #Today = DateAdd(DD, DateDiff(DD, 0, GETDATE()), 0)
select #YesterDay = convert(varchar, getdate()-1 , 110)
select #Today = convert(varchar, getdate() , 110)
So you declare it as DATETIME, set value with DateDiff then overwrite that value with an varchar representation of a date that is recalculated using different method.
At line 4 and 5 #Yesterday and #Today variables are still DATETIME, because it's declared that way.
EDIT: As mentioned in the comment to my answer you need a variable to pass to the procedure.
So the correct fix would be to declare the variables as VARCHAR(50) at the beginning and do the conversion directly.
DECLARE #YesterDay VARCHAR(50), #Today VARCHAR(50)
SELECT #YesterDay = convert(varchar(50), dateadd(dd, -1, getdate()) , 110)
SELECT #Today = convert(varchar(50), getdate() , 110)
And then call the procedure the way you did originally.
My guess is that getdate()-1 part is wrong.
Also, you were making dateadd and datediff why? Try it like this:
SET #YesterDay = dateadd(dd, -1, GETDATE())
SET #Today = GETDATE()
select #YesterDay = convert(varchar(50), dateadd(dd, -1, getdate()) , 110)
select #Today = convert(varchar(50), getdate() , 110)
EXEC #return_value = [dbo].[post_sec_admin_list_user_log]
#pDateFr = #YesterDay ,
#pDateTo = #Today,
#pName = '',
#pSec = NULL