如何在SQL Server中向数字添加逗号?

时间:2017-08-10 16:38:35

标签: sql sql-server stored-procedures

我正在使用SQL Server 2012.我需要为数字添加逗号。因为前2000应该是2000。

我该怎么办?谢谢。

这是我的问题:

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

2 个答案:

答案 0 :(得分:4)

确实属于表示层,但在2012年以后,您可以使用Format()。这是一个很好的功能,但不是一个高性能。

示例

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

<强>返回

25,302

答案 1 :(得分:1)

查看类似问题here。你要做的是先CAST到MONEY,然后CONVERT到VARCHAR。