SQL SELECT HH:MM - HH:MM - sql

I am trying to write an SQL query that will return the current time as follows:
HH:MM - HH:MM (+1)
E.g. if the time is 14:00 it would return 14:00 - 15:00
I have managed to get as far as follows:
SELECT TOP (30)
DATEADD(HOUR, DATEDIFF(HOUR, 0, QUEUE_TIME.QueueDate), 0) AS 'DateAdded',
CAST(CONVERT(VARCHAR(2), DATEPART(HH, QUEUE_TIME.QueueDate), 108) AS VARCHAR(2)) + ' - ' +
CAST(CONVERT(VARCHAR(2), DATEPART(HH, QUEUE_TIME.QueueDate), 108) + 1 AS VARCHAR(2)) AS Interval,
QUEUE_TYPE.Name, QUEUE_TIME.QueueTypeId,
MAX(QUEUE_TIME.QueuedTimeInSec / 60) AS 'WaitingTimeInSec',
CAST(ROUND(AVG(QUEUE_TIME.FlowRateWhenJoinedPerMin), 1) AS numeric(36, 2)) AS AvgFlowRate
FROM
QUEUE_TIME
INNER JOIN
QUEUE_TYPE ON QUEUE_TIME.QueueTypeId = QUEUE_TYPE.Id
WHERE
(QUEUE_TIME.QueueDate >= '11/07/2014 00:00')
AND (QUEUE_TIME.IsFreeFlowing = '0')
AND (QUEUE_TIME.QueueTypeId = '3')
GROUP BY
DATEADD(HOUR, DATEDIFF(HOUR, 0, QUEUE_TIME.QueueDate), 0),
DATEPART(HOUR, QUEUE_TIME.QueueDate), QUEUE_TYPE.Name, QUEUE_TIME.QueueTypeId
ORDER BY
'DateAdded'
This will return as follows, e.g. if it is 8am it will return 8-9. But i need it to be in the format 08:00 - 09:00.
Any assistance would be appreciated.
I am using SQL Server 2008.
Thanks

This is an example, you can replace #now variable with column name:
declare #now datetime = getdate()
select convert(varchar(5), #now, 114) + ' - ' +
convert(varchar(5), dateadd(hour, 1, #now), 114) yourColumn
SQL Fiddle demo

Try with FORMAT for SQL Server 2012+:
SELECT FORMAT(GETDATE(), 'HH:mm') + '-' + FORMAT(DATEADD(HH, 1, GETDATE()), 'HH:mm')

Related

Query Datediff to output HH:MM in SQL Server

I'm working with SQL Server 2012, and I have two columns start and end with varchar(5) values in HH:MM format.
The data looks like this
ID Start End
------------------------
1 00:00 06:00
2 06:00 16:00
3 16:00 18:00
4 18:00 24:00
My query is like this:
SELECT
a.start,
a.[end],
RIGHT('0' + CONVERT(VARCHAR(3), DATEDIFF(MINUTE, a.Start, a.[end]) / 60), 2) + ':' +
RIGHT('0' + CONVERT(VARCHAR(2), DATEDIFF(MINUTE, a.Start, a.[end]) % 60), 2) AS TotalHours
FROM
TransactionActivity a
I exec the query with where clause based on ID number, it gives me the correct result, until in ID 4: i got error like this
Conversion failed when converting date and/or time from character string.
I think it because the End time value is 24:00, how can I make it to get the time difference?
I think you'd be better off converting them into full datetime's (using an arbitrary date) because then the date functions will work correctly.
select
a.[start]
, a.[end]
, RIGHT('0' + CONVERT(varchar(3),DATEDIFF(minute,a.[Start], a.[end])/60),2) + ':' +
RIGHT('0' + CONVERT(varchar(2),DATEDIFF(minute,a.[Start],a.[end])%60),2)
as TotalHours
from (
select id
, case when [Start] = '24:00' then dateadd(minute, 1, convert(datetime, '23:59')) else convert(datetime, [Start]) end [Start]
, case when [End] = '24:00' then dateadd(minute, 1, convert(datetime, '23:59')) else convert(datetime, [End]) end [End]
from TransactionActivity
) a

Need hour min and sec in a query

I am calculating the time difference between 2 times, I want to print the hour min and sec. Can anyone please tell me how to do it.
My query
SELECT
CONVERT(VARCHAR(8), DATEADD(ms, DATEDIFF(ms, CONVERT(VARCHAR(8), GETDATE(), 114), CONVERT(VARCHAR(8), VCTime, 114)), 0), 114) AS TImeDifference
FROM
Test
Output:
TimeDifference
---------------
10:51:37
20:51:37
21:51:37
22:21:37
08:51:37
00:51:37
Expected Output
TimeDifference
---------------
10h:51m:37s
20h:51m:37s
21h:51m:37s
22h:21m:37s
08h:51m:37s
00h:51m:37s
One way is to use sub query and concatenation operator + for 2008 with DATEPART function as below:
SELECT (
CAST(DATEPART(HOUR,(TImeDifference)) AS VARCHAR) + 'h:' +
CAST(DATEPART(MINUTE,(TImeDifference)) AS VARCHAR) + 'm:' +
CAST(DATEPART(SECOND,(TImeDifference)) AS VARCHAR) + 's')
FROM(
SELECT
CONVERT(varchar(8), DATEADD(ms, DATEDIFF(ms, convert(varchar(8),getdate(),114),
convert(varchar(8),VCTime,114)), 0), 114) as TImeDifference
FROM test
) t
Yes I realized concat is introduced in 2012 so we can use + instead
you can follow below way
DECLARE #x int,
#dt1 smalldatetime = '2018-08-17 03:24:16',
#dt2 smalldatetime = getdate()
SET #x = datediff (s, #dt1, #dt2)
SELECT convert(varchar, #x / (60 * 60 * 24)) + ':'
+ convert(varchar, dateadd(s, #x, convert(datetime2, '0001-01-01')), 108)
this will return 1:05:57:00
Try this:
select cast(date_diff / 3600 as varchar(4)) + 'h:' +
cast((date_diff % 3600) / 60 as varchar(4)) + 'm:' +
cast(date_diff % 60 as varchar(4)) + 's'
from (
select datediff(second, getdate(), VCTime) date_diff from my_table
) a
First, you should not be converting to strings to get the difference. I think this should be fine:
SELECT CONVERT(VARCHAR(8),
DATEDIFF(ms, CAST(GETDATE() as TIME), CAST(VCTime as TIME)),
114
) as TImeDifference
FROM Test;
Then you want to add "h", "m", and "s". You can use the STUFF() function. But let me do this using APPLY so the code doesn't look quite so messy:
SELECT ( STUFF(STUFF(TimeDifference_str, 6, 0, 'm'), 3, 0, 'h') + 's' ) as TimeDifference_hms
FROM test t CROSS APPLY
(VALUES (CONVERT(VARCHAR(8),
DATEDIFF(ms, CAST(GETDATE() as TIME), CAST(VCTime as TIME)),
114
)
)
) v(TimeDifference_str)

Sql Server select datetime without seconds

I have datetime column value below
2015-01-04 20:37:00.000
I tried below
cast(cast(MyDateColumn as date) as datetime)+cast(datepart(hour,MyDateColumn ) as float)/24
as MyDateColumn
and
CAST(CONVERT(CHAR(16),MyDateColumn,113) AS datetime) as MyDateColumn
These are did not work for me
How can i get above datetime as 01-04.2015 20:37 ?
Since MS SQL 2012, you can use FORMAT,
SELECT FORMAT([MyDateColumn], 'dd-MM.yyyy HH:mm')
In MYSQL it will work
SELECT DATE_FORMAT(date, '%Y-%m-%d %H:%i') AS formated_date FROM table;
In MS SQL It will work
SELECT FORMAT(getdate(), 'dd-mm-yyyy HH:mm')
In SQL Server this will work:
DECLARE #now [datetime];
SET #now = GETDATE();
SELECT
CONVERT([varchar](10), #now, 105) + ' ' +
RIGHT('0' + CONVERT([varchar](2), DATEPART(HOUR, #now)), 2) + ':' +
RIGHT('0' + CONVERT([varchar](2), DATEPART(MINUTE, #now)), 2);
In SQL Server this should do the trick:
declare #dt datetime = '2015-01-04 20:37:00.000'
select right('0' + cast(DATEPART(MM, #dt) as varchar), 2) + '-'
+ right('0' +cast(DATEPART(DAY, #dt) as varchar), 2) + '.'
+ cast(DATEPART(YEAR, #dt) as varchar) + ' '
+ right('0' +cast(DATEPART(HOUR, #dt) as varchar), 2) + ':'
+ right('0' +cast(DATEPART(MINUTE, #dt) as varchar), 2)
Simply,
SELECT CAST(CONVERT(varchar, GETDATE(), 100) as datetime)
Here's another way and you get a datetime in return.
SELECT DATEADD(
MILLISECOND,
DATEPART(MILLISECOND, '2016-02-16 13:45:24.573') * -1,
DATEADD(SECOND, DATEPART(SECOND,'2016-02-16 13:45:24.573') * -1,
'2016-02-16 13:45:24.573')
)
this is the way i do it. I needed to get -2 minutes
select CONVERT(datetime, CONVERT(CHAR(18), DATEADD(minute, -2, getdate()) , 113) + '00')
Format(Cast(Convert(varchar(15),Cast(timeval as Time),100) as DateTime),'hh:mm tt') As newtime
This will remove seconds from time as well as add AM,PM with time.

Sql returning date time without seconds and milliseconds

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.

Convert SQL datetime to specific format

I have one table with one datetime colomn as below:
tbTest
ID int
SetDate datetime
and one entry like:
ID SetDate
1 04/10/2014 2:38:16 PM
I want to convert SetDate to dd-mm-yyyy hh:mm:ss PM or dd-mm-yyyy hh:mm PM
Desired Output:
ID SetDate
1 10-04-2014 2:38:16 PM
or
1 10-04-2014 2:38 PM
You can use sql convert method for getting Desired output :-23-04-2014 10:16 AM
SELECT CONVERT(VARCHAR(10), getdate(), 105) + ' ' + REPLACE(REPLACE(RIGHT('0'+LTRIM(RIGHT(CONVERT(varchar,getDate(),100),7)),7),'AM',' AM'),'PM',' PM')
You can use convert
select convert(varchar(10), getdate(), 105) + ' ' +
SUBSTRING(CONVERT(varchar, getdate(), 100), 14, 4)+ ' ' +
RIGHT(convert(varchar, getdate(), 100), 2)
SELECT replace(convert(NVARCHAR(100), getdate(), 106), ' ', '-')
+' ' + CONVERT(VARCHAR(30), CAST(GETDATE()AS Time), 100)
Use CONVERT with the appropriate styles:
SELECT CONVERT(CHAR(10), #SetDate, 110)
+ ' ' + SUBSTRING(CONVERT(CHAR(19), #SetDate, 100), 14, 4)
+ ' ' + SUBSTRING(CONVERT(CHAR(19), #SetDate, 100), 18, 2)
The first is for the date part, the second for the 12h time part and the last for the am/pm designator.
Demo
The shortest and best looking query you can use is:
SELECT CONVERT(VARCHAR(10), SetDate, 105) + ' ' +
CONVERT(VARCHAR(8), CAST(SetDate AS TIME), 100)
Output:
31-07-2001 12:00AM
No string manipulation required.
Another example:
DECLARE #Date AS DATETIME
SET #Date = '2014-31-12 18:00:00'
SELECT CONVERT(VARCHAR(10), #Date, 105) + ' ' +
CONVERT(VARCHAR(8), CAST(#Date AS TIME), 100) AS FormatDate
Outputs:
31-12-2014 6:00PM