gpt4 book ai didi

sql - 如何在 SQL Server 中为数字添加逗号?

转载 作者:行者123 更新时间:2023-12-05 01:01:09 26 4
gpt4 key购买 nike

我使用的是 SQL Server 2012。我需要为数字添加逗号。对于 ex 2000 应该是 2,000。

我该怎么做?谢谢。

这是我的查询:

declare @Term int = 20172;

Select
'Applications' as 'NEW FIRST-TIME FRESHMEN',
Cast(Round(((Cast(y.Last_Term As FLOAT)) * 1), 2) as varchar(10)) as 'Last_Term',
Cast(Round(((Cast(y.Current_Term AS FLOAT))*1),2) as varchar(10)) as 'Current_Term',
Cast(Round(((Cast(y.Current_Term AS FLOAT) - Cast(y.Last_Term AS FLOAT)) * 1), 2) as varchar(10)) as '#Change',
Cast(Round((((Cast(y.Current_Term AS FLOAT) - Cast(y.Last_Term AS FLOAT)) / (Cast(y.Last_Term AS FLOAT)))* 100), 2) as varchar(10)) + '%' as '%Change'
from
(Select
Case
When [ACADEMIC_PERIOD_ALL_SF_COMBINED] = @Term
Then 'Current_Term'
When [ACADEMIC_PERIOD_ALL_SF_COMBINED] = @Term-10
Then 'Last_Term'
End ACADEMIC_PERIOD_ALL_SF_COMBINED,
[APPLICATION_COUNT] Applied
From
[IR_DW].[dbo].[vw_dw_Applied_Admitted_Enrolled_F]
Where
STUDENT_LEVEL ='UG'
and STUDENT_POPULATION ='N'
and ACADEMIC_PERIOD_ALL_SF_COMBINED between @Term-10 and @Term
and ACADEMIC_PERIOD_ALL_SF_COMBINED like '%2') x
Pivot
(sum(Applied)
for ACADEMIC_PERIOD_ALL_SF_COMBINED in ([Current_Term] , [Last_Term])
)y

output

最佳答案

确实属于表现层,但在2012+,可以使用Format()。这是一个很棒的功能,但不是一个高性能的。

示例

Select Format(25302.25,'#,##0')

返回

25,302

关于sql - 如何在 SQL Server 中为数字添加逗号?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45619173/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com