我如何使用自定义分隔符货币值?

How can i use custom separator characters money values?

我想显示我的价格 table 的结果,具体格式如下:

36360.23 ==> 2"536"360/23

我写了这段代码, 请帮我完成:

Declare     @Input           Money = 2536360.23
,           @Seprator        Char(1) = '"'
,           @DecimalPointer  Char(1) = '/'

我同意 Larnu 的观点,这实际上不应该在数据库层完成,但这是可能的。

因此,如果您只是对学习 "how" 感兴趣:

Declare     @Input           Money = 2536360.23
,           @Seprator        Char(1) = '"'
,           @DecimalPointer  Char(1) = '/'

SELECT 
    REPLACE(
        REPLACE(
            REPLACE(                
                FORMAT(@Input, 'C', 'en-us'), -- this puts it in the expected starting format ,536,360.23
                                              -- if you skip the last parameter, it would be based on whatever your local culture is set to
                                              -- e.g. I am British so for me it would say £2,536,360.23
            ',', @Seprator), 
        '.', @DecimalPointer), 
    '$', '');

考虑永远不要使用 FORMAT 函数。至少在 SQL 服务器中没有。在我测试性能之前,我真的很高兴看到它问世。

这是对此 post 的测试以及更快的可能性...

--===== Create some test data. =============================================================
     -- This is NOT a part of the solution. We're just building test data here.
 SELECT TOP (1000000)
        SomeMoneyValue = CONVERT(MONEY,CHECKSUM(NEWID())/100.0)
   INTO #TestTable
   FROM      sys.all_columns ac1
  CROSS JOIN sys.all_columns ac2
;
GO
/*******************************************************************************************
 In the following code, the output is dumped to a variable to take disk and display times
 out of the picture.
*******************************************************************************************/
GO
PRINT'
--===== Run the BASELINE code with no formatting ==========================================';
DECLARE @BitBucket MONEY
;
    SET STATISTICS TIME,IO ON;
 SELECT @BitBucket =SomeMoneyValue
   FROM #TestTable;
    SET STATISTICS TIME,IO OFF;
GO

PRINT'
--===== Run the CONVERT code ===============================================================';
DECLARE @BitBucket VARCHAR(30)
;
    SET STATISTICS TIME,IO ON;
 SELECT @BitBucket = REPLACE(REPLACE(CONVERT(VARCHAR(30),SomeMoneyValue,1),',','"'),'.','/')
   FROM #TestTable;
    SET STATISTICS TIME,IO OFF;
GO
PRINT'
--===== Run the FORMAT code, take a nap ====================================================';
DECLARE @BitBucket VARCHAR(30)
;
    SET STATISTICS TIME,IO ON;
 SELECT @BitBucket = REPLACE(REPLACE(FORMAT(SomeMoneyValue, 'C', 'en-us'),',','"'),'.','/')
   FROM #TestTable;
    SET STATISTICS TIME,IO OFF;
GO
--===== Housekeeping =======================================================================
   DROP TABLE #TestTable
;
GO

结果如下:

(1000000 rows affected)

--===== Run the BASELINE code with no formatting ==========================================
Table '#TestTable______________________________________________________________________________
0000006218B0'. Scan count 1, logical reads 2101, physical reads 0, read-ahead reads 0, lob logi

 SQL Server Execution Times:
   CPU time = 156 ms,  elapsed time = 153 ms.

--===== Run the CONVERT code ===============================================================
Table '#TestTable______________________________________________________________________________
0000006218B0'. Scan count 1, logical reads 2101, physical reads 0, read-ahead reads 0, lob logi

 SQL Server Execution Times:
   CPU time = 1032 ms,  elapsed time = 1038 ms.

--===== Run the FORMAT code, take a nap ====================================================
Table '#TestTable______________________________________________________________________________
0000006218B0'. Scan count 1, logical reads 2101, physical reads 0, read-ahead reads 0, lob logi

 SQL Server Execution Times:
   CPU time = 32875 ms,  elapsed time = 36664 ms.

至于不做这种事情 SQL 服务器,我坚信表示层和数据层通常应该保持分离。在某些情况下,例如制作一个具有一些奇怪格式的文件,在 SQL 服务器中完成所有操作将使您的网络流量减少一半,而不会增加数据库服务器的压力。