November 29, 2022 at 1:41 am
Hi,
I have a field showing select field from table ;
2022-11-28 16:19:38
I would like to convert to 11-28-2022 4:19 PM
November 29, 2022 at 3:07 am
Replace GETDATE() in the following with your datetime valued.
SELECT CONVERT(CHAR(10),GETDATE(),110)+STUFF(RIGHT(CONVERT(CHAR(19),GETDATE(),100),8),7,0,' ')
Some will suggest using the FORMAT function... I strongly recommend against that. See the article at the following link... I can vouch for the author. 😀
https://www.sqlservercentral.com/articles/how-to-format-dates-in-sql-server-hint-dont-use-format
I also recommend that you bone up on the CONVERT function. Here's the link for that.
https://learn.microsoft.com/en-us/sql/t-sql/functions/cast-and-convert-transact-sql
--Jeff Moden
Change is inevitable... Change for the better is not.
November 29, 2022 at 9:59 pm
Your solution did not convert to 12 hour or include AM PM designation.
Ha, I know you suggested against using FORMAT but that is what I ended up doing .
FORMAT(CAST(field_here AS datetime), 'MM/dd/yyyy hh:mm tt') 'field_name_here'
(I am not an expert by any means).
November 29, 2022 at 10:09 pm
Your solution did not convert to 12 hour or include AM PM designation.
Ha, I know you suggested against using FORMAT but that is what I ended up doing .
FORMAT(CAST(field_here AS datetime), 'MM/dd/yyyy hh:mm tt') 'field_name_here'
(I am not an expert by any means).
I don't post untested code. Please post the exact code that you say didn't work and identify the datatype you used to replace GETDATE() with.
Here's the code I posted and the results it returned.
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 4 posts - 1 through 3 (of 3 total)
You must be logged in to reply to this topic. Login to reply