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

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')

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 convert date time in varchar to date in oracle to get one month data [duplicate]

This question already has answers here:
Get the records of last month in SQL
(2 answers)
Closed 12 months ago.
I have data in oracle table where date field Created_Date is in format 01/01/2022 7:00:00 PM which is of type varchar2 ,i want to get past one month data, and i did below query which is not working
select *from Mn_Fdd_tbl where to_date(to_char(Created_Date,'DD/MM/YYYY'), 'DD/MM/YYYY' ) > trunc(sysdate)-30;
Should be
select *
from mn_fdd_tbl
where to_date(created_date, 'dd/mm/yyyy hh:mi:ss pm') > add_months(trunc(sysdate), -1);
because
no point in TO_CHAR-ing something that's already a string ...
... with a wrong format model
"last month": not all months have 30 days, so - your "calculation" is wrong for approx. 50% of months. Use add_months instead

Format Time as 24 Hours Military Format [duplicate]

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_)

How to get first hour of today in SQL Server? [duplicate]

This question already has answers here:
How to return only the Date from a SQL Server DateTime datatype
(46 answers)
Closed 5 years ago.
I need to obtain today's date but instead of current time I need the first time of the date.
WRONG WAY:
'4/7/2017 4:50:13 PM'
as result of getdate() function
RIGHT WAY:
'4/7/2017 00:00:00 AM'
Thanks for your help
How about this
select cast(cast(getdate() as date) as datetime)

SQL Database column is a DateTime but I want to search by just time [duplicate]

This question already has answers here:
How to get Time from DateTime format in SQL?
(19 answers)
Closed 6 years ago.
Column format is 'YYYY-MM-DD HH:MM:SS'. But I want to search for all rows where that time is between 19:00:00 and 24:00:00
You can use datepart():
where datepart(hour, col) between 19 and 23