SQL 如何在sql命令中将数字转换为nvarchar

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8411415/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 13:33:50  来源:igfitidea点击:

how to convert numeric to nvarchar in sql command

sqlsql-server-2005

提问by Arun

I need to convert a numeric value to nvarchar in sql command.

我需要在 sql 命令中将数值转换为 nvarchar。

Can anyone please help me.

谁能帮帮我吗。

回答by Olivier S

select convert(nvarchar(255), 4343)

Should do the trick.

应该做的伎俩。

回答by Chris Fulstow

declare @MyNumber int
set @MyNumber = 123
select 'My number is ' + CAST(@MyNumber as nvarchar(20))

回答by Anbuselvan

declare @MyNumber float 
set @MyNumber = 123.45 
select 'My number is ' + CAST(@MyNumber as nvarchar(max))

回答by Spock

If the culture of the result doesn't matters or we're only talking of integer values, CONVERTor CASTwill be fine.

如果结果的文化无关紧要或者我们只是在谈论整数值,CONVERT或者CAST会很好。

However, if the result must match a specific culture, FORMATmight be the function to go:

但是,如果结果必须匹配特定的文化,则FORMAT可能是要执行的函数:

DECLARE @value DECIMAL(19,4) = 1505.5698
SELECT CONVERT(NVARCHAR, @value)        -->  1505.5698
SELECT FORMAT(@value, 'N2', 'en-us')    --> 1,505.57
SELECT FORMAT(@value, 'N2', 'de-de')    --> 1.505,57

For more information on FORMATsee here.

有关更多信息,FORMAT请参见此处

Of course, formatting the result should be a matter of the UI layer of the software.

当然,格式化结果应该是软件UI层的事情。