Home > other >  How to remove AM/PM from DateTime Column in SQl?
How to remove AM/PM from DateTime Column in SQl?

Time:04-12

Please help me, How to remove AM/PM from varchar datetime.

For example 2/14/2017 2:21:18 PM , I want to remove PM from that.

I have DateTime as a string -

'2022-03-11 11:13:24 AM'

I want to remove AM/PM

I tried using this query but it is not working

REPLACE(REPLACE(src.[ApprovedDate],'AM',''),'PM','')

CodePudding user response:

DECLARE @myDatetime varchar(50);
SET @myDatetime = '2/14/2017 2:21:18 PM';
SELECT CONVERT(varchar(50), CONVERT(datetime, @myDatetime), 101)   ' '   
CONVERT(varchar(50), CONVERT(datetime, @myDatetime), 108);

CodePudding user response:

Maybe use FORMAT function of sql assuming your are in MS SQL world

SELECT FORMAT(CAST(src.[ApprovedDate] AS DATETIME),'yyyy-MM-dd HH:mm')

in MYSQL you could use DATE_FORMAT

see demo link

  • Related