Skip to content
Advertisement

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 ?

Advertisement

Answer

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);
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement