Format Time as 24 Hours Military Format [duplicate] - sql

This question already has answers here:
Format time as "24-hour Military Time"?
(2 answers)
Closed 3 years ago.
I need to convert a date/time format that currently stores the Start Time of an event (Start_) to military time format without the colon ':'. So if the time is stored as 5:30pm it should come through as 1730
Code I have tried is below but it brings through the seconds and the colon.
SELECT CONVERT(varchar, E.Start_, 108)
FROM Events E

If I understand correctly, you can just use format():
select format(E.Start_, 'hhmm')
If you want a number:
select datepart(hour, E.Start_) * 100 + datepart(minute, E.Start_)

Related

How do i get the full hour instead of the minutes? [duplicate]

This question already has answers here:
Truncate date to only hour / minute
(4 answers)
T-SQL datetime rounded to nearest minute and nearest hours with using functions
(4 answers)
Group DateTime into 5,15,30 and 60 minute intervals
(6 answers)
Closed 6 months ago.
I'm tring to extract hour values from a datetime:
I want all the datetimes like '2021-10-27 04:55:00.000' to show the hour data, so: '2021-10-27 04:00:00.000'
What query do i run to get this?
Thanks in advance!
Use date maths and a "magic" date:
DATEADD(HOUR,DATEDIFF(HOUR,0,YourColumn),0);
This gets the number of hours between the "date" 0 (1900-01-01) and your date value, and then adds that many hours to the "date" 0.
On SQL Server 2022 (currently in preview), however, you have access to DATETRUNC and DATE_BUCKET that make this much easier:
DATETRUNC(HOUR,YourColumn),
DATE_BUCKET(HOUR,0,YourColumn)
The current time (UK) is 11:06 (am) and:
select format(getdate(), 'yyyy-MM-dd HH')
..returns 2022-08-12 11
You can add whatever you want on the end of it, e.g.:
select format(getdate(), 'yyyy-MM-dd HH') + ':00:00'
..which gives 2022-08-12 11:00:00
NB Format is a SQL-Server function. I'm not sure how many other databases have it.
For the string representation of a date and time value you can do something like this
SELECT CONCAT(FORMAT(dt, 'yyyy-MM-dd HH'),'00:00.000')
see Custom formatting and Custom Date and Time Formatting
use convert
declare #a datetime='2021-10-27 04:55:00.000'
select convert(varchar(10),#a,120) + ' '+convert(varchar(2), datepart(hour,#a))+':00:00'

How to get 12 hour time from 24 hour string [duplicate]

This question already has answers here:
Converting a time into 12 hour format in SQL
(6 answers)
Closed 6 months ago.
I have a column of varchar type which stores time value as 24hr format like 08:20, 14:30 etc
I need the output as 12hr format like 8:20 AM, 2:30 PM respectively
What should be the query in SQL Server?
declare #tm VARCHAR(20)= '13:40'
select FORMAT(CAST(#tm AS DATETIME), 't')

Convert an INT Column to H:MM Format in SQL Server [duplicate]

This question already has answers here:
How to convert number of minutes to hh:mm format in TSQL?
(13 answers)
Closed 7 months ago.
I am trying to convert a column which contains integer value of a quantity of time for example 60 mins, 90mins, 200mins etc to HH:MM Format. I have tried various formulas related to CAST() AND CONVERT() SUCH AS:
1. CONVERT(VARCHAR(2),C.Quantity/60) + ':' + CONVERT(VARCHAR(2),C.Quantity%60)
2.REPLACE(CONVERT(NUMERIC(18, 2),
(SUM(COALESCE(DATEDIFF(Minute, 0,C.Quantity), 0))/60) +
((SUM(COALESCE(DATEDIFF(Minute, 0,C.Quantity), 0)) % 60))),'.',':')
3.CONCAT(CONVERT(VARCHAR(3),C.Quantity/60,0),108)+':'+CONVERT(VARCHAR(3),C.Quantity%60,0),108)
4.CONVERT(VARCHAR(2),C.Quantity/60) + ':' + CONVERT(VARCHAR(2),C.Quantity%60)
5.CONVERT(varchar(5),DATEADD(minute, DATEDIFF(minute, #FirstDate, #LastDate), 0), 114)
You can use DATEADD onto a starting time of 00:00:00
SELECT DATEADD(minute, t.Quantity, CAST('00:00:00' AS time))
FROM YourTable t
db<>fiddle

How to Select only Date Hour and Minutes from 2017-09-27 15:39:36.000 [duplicate]

This question already has answers here:
A way to extract from a DateTime value data without seconds
(9 answers)
Closed 4 years ago.
I have a column in my table (having value- 2017-09-27 15:39:36.000).
I want to select only Date, Hour, Minutes (i.e:- 2017-09-27 15:39) from column.
Can anyone please tell me how to select.?
Use the FORMAT function to format the date:
SELECT FORMAT(CAST('2017-09-27 15:39:36.000' AS DATETIME), 'yyyy-MM-dd HH:mm')
-- 2017-09-27 15:39
Here is a list of available format specifiers.
One option would be to use CONVERT with a mask matching the format you want:
SELECT CONVERT(varchar(16), GETDATE(), 120)
2018-09-28 07:58
Demo
We convert to varchar(16) explicitly so that only the hour and minute components are retained.
Use the datepart function which can extract specific parts of the date value.
https://learn.microsoft.com/en-us/sql/t-sql/functions/datepart-transact-sql?view=sql-server-2017

Convert date to MM//DD HH/MM [duplicate]

This question already has answers here:
Sql Server string to date conversion
(16 answers)
Closed 5 years ago.
How do you convert date to MM/DD HH/MM
example: 04-05 12:42
Since you are on SQL Server 2012, use FORMAT:
SELECT FORMAT(GETDATE(), 'MM-dd HH:mm')
Try using the MONTH, DAY, HOUR, MINUTE functions in T-SQL and concatenate your results into a string form like:
CONCAT(STRING(HOUR(created_at)), ':', RIGHT(STRING(MINUTE(created_at)), 3)) AS hour_min
Where the created_at is your timestamp column. Hope this helps!