How to trim seconds in TSQL?

I have time, select cast (SYSDATETIME () AS time) 14: 59: 09,2834595

What is the way to truncate seconds? 14:59

+7
source share
4 answers

Description

You can use the T-SQL convert function.

Example

 PRINT convert(varchar(5), SYSDATETIME(), 108) 

will give you hh:mm

Additional Information

+10
source

If you need to completely drop seconds, you can use the DATEPART () function (SQL Server) to cut the hour and minute, and then add them back together. (I like dknaack's solution more if it works.)

 SELECT CAST(DATEPART(hour, SYSDATETIME()) + ':' + DATEPART(minute, SYSDATETIME()) AS DATETIME) 
+1
source
 select cast(left(cast(SYSDATETIME() AS time), 5) as time) 
+1
source

If you want to truncate seconds and still have the T-SQL Date data type, first convert the date to minutes from the date โ€œ0โ€, and then add the minutes to โ€œ0โ€. This answer does not require additional analysis / conversion. This method works to truncate other parts that only replace MINUTE. Example: SELECT DATEADD(MINUTE, DATEDIFF(MINUTE, 0, '2016-01-01 23:22:56.997'), 0)

+1
source

All Articles