技术开发 频道

Money类型与nvarchar类型转换

  【IT168技术文档】

  下面介绍一个功能:把Money类型转换成nvarchar类型,保留三位小数,而且把数值后面多余的0去掉。如money类型的数值2.59100,转化成nvarchar(20)类型时,要求的结果是2.591,又或者如money类型的数值89.7800,转化成nvarchar(20)类型时,要求的结果是89.78

CREATE TABLE #Temp([ID] int ,F1 nvarchar(20),F2 nvarchar(20),SMoney money,Dmoney money) INSERT INTO #Temp SELECT 1,'F1','F2',45.895,23.89000 UNION SELECT 2,'F2','F3',45.895,234.67 UNION SELECT 3,'F3','F4',25.835,32.123 UNION SELECT 4,'F4','F5',13.7600,31.6754 UNION SELECT 5,'F5','F6',34.783,78.345

  要求的结果如下:

SMoney +'X'+ Dmoney ------------------ 45.895X23.89 45.895X234.67 25.835X32.123 13.76X31.675 34.783X78.345

  如下:

SELECT *,cast(SMoney as nvarchar(20)) FROM #Temp SELECT CAST(CAST(CAST(Smoney AS decimal(20,3)) AS float) as nvarchar)+'X'+ CAST(CAST(CAST(Dmoney AS decimal(20,3)) AS float)AS nvarchar) FROM #Temp DROP TABLE #Temp
0
相关文章