0

I need a SQL server query to convert date to Mon DD, YYYY HH:MM:SS AM/PM

for e.g. the date is :- 2016-11-21 16:58:57.797

I need the SQL output as Nov 21 2016 4:58:57 PM

I tried --> CONVERT(VARCHAR(19),GETDATE(),109)
but it is displaying results like --> Nov 21 2016 4:58:5

Viki888
  • 2,686
  • 2
  • 13
  • 16
Amar Kumar
  • 11
  • 6

4 Answers4

1

Increase the length of VARCHAR in your statement like below

select CONVERT(VARCHAR(30),GETDATE(),109)

This is giving me result as Nov 21 2016 12:55:31:390PM

Hope this should work out for you.

Viki888
  • 2,686
  • 2
  • 13
  • 16
0

For mysql you can do this:

SELECT DATE_FORMAT(NOW(),'%b %d %Y %h:%i %p');

For ms sql you can do this:

SELECT CONVERT(VARCHAR(19),GETDATE())

Reference:

Arion
  • 31,011
  • 10
  • 70
  • 88
0

Use SUBSTRING and CHARINDEX to get the left part of the string excluding the milliseconds part. Then concatenate the last AM/PM with that.

Query

declare @date as varchar(30) = convert(varchar(30),getdate(),109);
select substring(@date, 1, len(@date) - charindex(':', reverse(@date), 1)) 
       + ' ' + right(@date, 2) as [datetime];
Ullas
  • 11,450
  • 4
  • 33
  • 50
0

you can use this

select FORMAT(CAST(GETDATE() AS DATETIME),'MMM dd yyyy hh:mm:ss tt') AS DateTimeAMPM
Kentgi
  • 308
  • 1
  • 3
  • 15