How to you display a date in the following format with lower case am or pm? MM/DD/YYYY h:mm a
I've searched in Stack Overflow, but no one seems to ask for this exact format. I am close, but I can't get the seconds to hide:
declare @X datetime
set @X = '2012-01-25 05:24:05 pm' --this needs to show as 01/25/2012 5:24 pm
select LOWER(CONVERT(varchar(10), @X, 101) + ' ' +
SUBSTRING(CONVERT(varchar(26), @X, 109), 13, 8) + ' ' +
SUBSTRING(CONVERT(varchar(26), @X, 109), 25, 2))
答案 0 :(得分:1)
Try this:
declare @X datetime
set @X = '2012-01-25 05:24:05 pm' --this needs to show as 01/25/2012 5:24 pm
SELECT CONVERT(VARCHAR(10), @X, 101) + RIGHT(CONVERT(VARCHAR(100), @X, 100),CHARINDEX (' ',REVERSE(CONVERT(VARCHAR(100), @X, 100))))
Result:
01/25/2012 5:24PM
Remark: There is missing space between AM/PM and minutes. If it is improtant use this:
declare @X datetime
declare @stringX varchar(100)
set @X = '2012-01-25 05:24:05 pm' --this needs to show as 01/25/2012 5:24 pm
SELECT @stringX = CONVERT(VARCHAR(10), @X, 101) + RIGHT(CONVERT(VARCHAR(100), @X, 100),CHARINDEX (' ',REVERSE(CONVERT(VARCHAR(100), @X, 100))))
SELECT LEFT (@stringX, LEN(@stringX)-2) + ' ' + LOWER(RIGHT(@stringX, 2))
Result:
01/25/2012 5:24 pm
答案 1 :(得分:1)
DECLARE @d datetime, @t time
SET @d = GETDATE()
SET @t = CAST(@d AS time)
SELECT
(
CONVERT(nvarchar(10), @d, 101) +
' ' +
SUBSTRING(CONVERT(nvarchar(15), @t, 100), 1, CHARINDEX('M', CONVERT(nvarchar(15), @t, 100), 1) - 2) +
' ' +
SUBSTRING(CONVERT(nvarchar(15), @t, 100), CHARINDEX('M', CONVERT(nvarchar(15), @t, 100), 1) - 1, 2)
)
答案 2 :(得分:-1)
Try the MySQL manual page on the date formatting :
https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-format