将 getdate 转换为格式 22nd APR, 2022
Convert getdate to format 22nd APR, 2022
谁能帮我解决这个问题,
如何将 getdate() 转换为格式 22nd APR, 2022,
21st 2022 年 4 月,
23rd 2022 年 4 月,
2022 年 4 月 24,
nd 或 th 或 rd 在这里很重要。
到目前为止我已经做到了,
SELECT 格式 (getdate(), 'dd MMM, yyyy') 作为日期
提前致谢!
对于 SQLServer:
SELECT FORMAT(getdate(),'d'
+IIF(DAY(getdate()) IN (1,21,31),'''st'''
,IIF(DAY(getdate()) IN (2,22),'''nd'''
,IIF(DAY(getdate()) IN (3,23),'''rd''','''th''')))
+' MMM' +', '+ 'yyyy') As [Formatted Date]
也许像下面这样的查询
SELECT FORMAT (getdate(), 'd'''+v.suff+''' MMM, yyyy') as date
FROM
(values (1,'st'), (2,'nd'),(3,'rd'), (4,'th')
, (5,'th'), (6,'th'), (7,'th'), (8,'th')
, (9,'th'), (0,'th'))v(num,suff)
WHERE DAY(getdate())%10=v.num
这个涵盖第11、12和13号。
select
cast(day(getdate()) as nvarchar)+
case when day(getdate()) in (11,12,13)
then 'th'
else
case right(day(getdate()),1)
when 1 then
'st'
when 2 then
'nd'
when 3 then
'rd'
else 'th'
end
end + format(getdate(),' MMM yyyy') as suffixed
我会在表示层执行此格式设置 - 报告工具、C# 和 PHP 等客户端语言等都具有比 T-SQL.[=19 更详尽和更高效的日期格式设置方法=]
如果必须在 T-SQL 完成,我会避免 FORMAT()
因为它是 slow as molasses and since, presumably, you're going to call this more than once or for sets, I would put it in an inline table-valued function which will be more efficient than a scalar UDF and much more maintainable than putting this conditional logic inline. Using the CASE
expression from :
CREATE FUNCTION dbo.SpecificFormat(@d date)
RETURNS TABLE
WITH SCHEMABINDING
AS
RETURN
(
WITH s(d) AS
(
SELECT REPLACE(UPPER(CONVERT(varchar(13), @d, 106)),' ','.')
),
p(d,m,y) AS
(
SELECT PARSENAME(d,3), PARSENAME(d,2), PARSENAME(d,1)
FROM s
)
SELECT OutputFormat = CONCAT_WS(' ', d + CASE
WHEN d % 100 IN (11,12,13) THEN 'th'
WHEN d % 10 = 1 THEN 'st'
WHEN d % 10 = 2 THEN 'nd'
WHEN d % 10 = 3 THEN 'rd'
ELSE 'th' END, m + ',', y)
FROM p
);
示例:
DECLARE @d date = getdate();
SELECT OutputFormat FROM dbo.SpecificFormat(@d);
输出:
OutputFormat
22nd APR, 2022
SELECT o.name, f.OutputFormat
FROM sys.objects AS o
CROSS APPLY dbo.SpecificFormat(o.modify_date) AS f;
输出:
name
OutputFormat
SpecificFormat
22nd APR, 2022
QueryNotificationErrorsQueue
13th APR, 2009
queue_messages_1977058079
24th SEP, 2019
...
...
谁能帮我解决这个问题, 如何将 getdate() 转换为格式 22nd APR, 2022, 21st 2022 年 4 月, 23rd 2022 年 4 月, 2022 年 4 月 24, nd 或 th 或 rd 在这里很重要。
到目前为止我已经做到了,
SELECT 格式 (getdate(), 'dd MMM, yyyy') 作为日期
提前致谢!
对于 SQLServer:
SELECT FORMAT(getdate(),'d'
+IIF(DAY(getdate()) IN (1,21,31),'''st'''
,IIF(DAY(getdate()) IN (2,22),'''nd'''
,IIF(DAY(getdate()) IN (3,23),'''rd''','''th''')))
+' MMM' +', '+ 'yyyy') As [Formatted Date]
也许像下面这样的查询
SELECT FORMAT (getdate(), 'd'''+v.suff+''' MMM, yyyy') as date
FROM
(values (1,'st'), (2,'nd'),(3,'rd'), (4,'th')
, (5,'th'), (6,'th'), (7,'th'), (8,'th')
, (9,'th'), (0,'th'))v(num,suff)
WHERE DAY(getdate())%10=v.num
这个涵盖第11、12和13号。
select
cast(day(getdate()) as nvarchar)+
case when day(getdate()) in (11,12,13)
then 'th'
else
case right(day(getdate()),1)
when 1 then
'st'
when 2 then
'nd'
when 3 then
'rd'
else 'th'
end
end + format(getdate(),' MMM yyyy') as suffixed
我会在表示层执行此格式设置 - 报告工具、C# 和 PHP 等客户端语言等都具有比 T-SQL.[=19 更详尽和更高效的日期格式设置方法=]
如果必须在 T-SQL 完成,我会避免 FORMAT()
因为它是 slow as molasses and since, presumably, you're going to call this more than once or for sets, I would put it in an inline table-valued function which will be more efficient than a scalar UDF and much more maintainable than putting this conditional logic inline. Using the CASE
expression from
CREATE FUNCTION dbo.SpecificFormat(@d date)
RETURNS TABLE
WITH SCHEMABINDING
AS
RETURN
(
WITH s(d) AS
(
SELECT REPLACE(UPPER(CONVERT(varchar(13), @d, 106)),' ','.')
),
p(d,m,y) AS
(
SELECT PARSENAME(d,3), PARSENAME(d,2), PARSENAME(d,1)
FROM s
)
SELECT OutputFormat = CONCAT_WS(' ', d + CASE
WHEN d % 100 IN (11,12,13) THEN 'th'
WHEN d % 10 = 1 THEN 'st'
WHEN d % 10 = 2 THEN 'nd'
WHEN d % 10 = 3 THEN 'rd'
ELSE 'th' END, m + ',', y)
FROM p
);
示例:
DECLARE @d date = getdate();
SELECT OutputFormat FROM dbo.SpecificFormat(@d);
输出:
OutputFormat 22nd APR, 2022
SELECT o.name, f.OutputFormat
FROM sys.objects AS o
CROSS APPLY dbo.SpecificFormat(o.modify_date) AS f;
输出:
name OutputFormat SpecificFormat 22nd APR, 2022 QueryNotificationErrorsQueue 13th APR, 2009 queue_messages_1977058079 24th SEP, 2019 ... ...