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

时间:2019-07-16 08:57:27

标签: sql-server currency

我想以特定格式显示价格表中的结果:

$2536360.23 ==> 2"536"360/23

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

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

2 个答案:

答案 0 :(得分:0)

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

因此,如果您只想学习“方法”:

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 $2,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), 
    '$', '');

答案 1 :(得分:0)

请考虑永远不要使用FORMAT函数。至少在SQL Server中没有。在测试性能之前,我真的很高兴看到它的出现。

这里是对这篇文章的测试,并且有更快的可能性...

--===== 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 Server而言,只要不做这种事情,我坚信表示层和数据层通常应该保持分开。在某些情况下,例如制作具有某些奇怪格式的文件,在SQL Server中完成所有这些操作会将网络通信量减少一半,而又不会给数据库服务器带来太大的压力。