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

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)

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 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!

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

Convert Unix time from IPFix to datetime in SQL [duplicate]

This question already has answers here:
converting Epoch timestamp to sql server(human readable format)
(2 answers)
SQL: Using DATEADD with bigints
(6 answers)
Closed 8 years ago.
I have an IPFix timestamp which appears to be in Unix time. My SQL code is:
update
temp_ipfix
set FlowStartTimeDT = dateadd(S, FlowStartTime, '1970-01-01 00:00:00')
Where FlowStartTime is the column containing the Unix timestamp.
A sample timestamp is 1410435197783
The error I'm getting is:
Arithmetic overflow error for type int, value = 1410435197783.000000.
The statement has been terminated
I tried the suggestions linked to this page, but the results are incorrect. Month and Day are correct, but year is 2034.
select dateadd(s, (flowstoptime/1000), '1990-01-01 00:00:00') from temp_unix