Thursday, March 8, 2012

DATE FORMAT/SYNTAX QUESTION

What getdate() syntax command can give me time in the following format:

10:41:55 AM

Regards,

Addi"addi" <addi_s@.hotmail.com> wrote in message news:llnGb.31580$zU5.19873@.news01.roc.ny...
> What getdate() syntax command can give me time in the following format:
> 10:41:55 AM
> Regards,
> Addi

SELECT CAST(((DATEPART(HOUR, CURRENT_TIMESTAMP) + 11) % 12) + 1
AS VARCHAR(2)) +
':' +
RIGHT('0' + DATENAME(MINUTE, CURRENT_TIMESTAMP), 2) +
':' +
RIGHT('0' + DATENAME(SECOND, CURRENT_TIMESTAMP), 2) +
CASE WHEN DATEPART(HOUR, CURRENT_TIMESTAMP) < 12
THEN ' AM'
ELSE ' PM'
END AS current_time

current_time
5:22:57 PM

Regards,
jag|||Note that John's solution may be ugly but remember that SQL is not a
report-writing language. It's usually better to format results in the
front-end. Many languages provide robust date formatting functions.

--
Hope this helps.

Dan Guzman
SQL Server MVP

"addi" <addi_s@.hotmail.com> wrote in message
news:llnGb.31580$zU5.19873@.news01.roc.ny...
> What getdate() syntax command can give me time in the following format:
> 10:41:55 AM
> Regards,
> Addi

No comments:

Post a Comment