Share via

Datetime Convert

David Chase 681 Reputation points
2020-09-19T14:57:09.89+00:00

I am using the CONVERT function in TSQL to get a date, time and AMPM as follows

CONVERT(CHAR(20), CURRENT_TIMESTAMP, 22)

but it gives me 08/07/20 4:41:48 PM

Is there a setting that would give only 4:41 PM without the seconds?

SQL Server | Other
SQL Server | Other

Additional SQL Server features and topics not covered by specific categories

0 comments No comments

Answer accepted by question author

Viorel 127K Reputation points
2020-09-19T16:09:28.217+00:00

If no such feature, then consider two alternatives:

-- Method 1  
  
declare @s as varchar(max)  
set @s = CONVERT(CHAR(20), CURRENT_TIMESTAMP, 22)  
set @s = STUFF( @s, PATINDEX('%:[0-9][0-9] [AP]M', @s), 3, '')  
select @s  
  
-- Method 2  
  
select FORMAT( CURRENT_TIMESTAMP, 'dd''/''MM''/''yy h'':''mm tt')  

The date part can be removed if not needed.

Was this answer helpful?

0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Ademir Passos 1 Reputation point
    2020-09-20T01:38:38.497+00:00

    Was this answer helpful?

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.