简体   繁体   English

将浮点数转换或转换为 nvarchar?

[英]cast or convert a float to nvarchar?

I need to select from one column of datatype float and insert it in another column as nvarchar.我需要从一列数据类型 float 中提取 select 并将其作为 nvarchar 插入另一列。

I tried to cast it: cast([Column_Name] as nvarchar(50))我试图转换它: cast([Column_Name] as nvarchar(50))

The result was 9.07235e+009 instead of a 10 digit number (phone number).结果是9.07235e+009而不是 10 位数字(电话号码)。

Does any one know how to cast or convert this data properly?有谁知道如何正确转换或转换这些数据?

Check STR .检查STR You need something like SELECT STR([Column_Name],10,0) ** This is SQL Server solution, for other servers check their docs.你需要像SELECT STR([Column_Name],10,0) ** 这是 SQL Server 解决方案,其他服务器检查他们的文档。

If you're storing phone numbers in a float typed column (which is a bad idea) then they are presumably all integers and could be cast to int before casting to nvarchar.如果您将电话号码存储在浮点类型的列中(这是一个坏主意),那么它们可能都是整数,并且可以在转换为 nvarchar 之前转换为 int。

So instead of:所以而不是:

select cast(cast(1234567890 as float) as nvarchar(50))
1.23457e+009

You would use:你会使用:

select cast(cast(cast(1234567890 as float) as int) as nvarchar(50))
1234567890

In these examples the innermost cast(1234567890 as float) is used in place of selecting a value from the appropriate column.在这些示例中,最里面的类型转换cast(1234567890 as float)用于代替从适当的列中选择值。

I really recommend that you not store phone numbers in floats though!不过,我真的建议您不要将电话号码存储在浮点数中!
What if the phone number starts with a zero?如果电话号码以零开头怎么办?

select cast(0100884555 as float)
100884555

Whoops!哎呀! We just stored an incorrect phone number...我们刚刚存储了一个错误的电话号码...

Do not use floats to store fixed-point, accuracy-required data.不要使用浮点数来存储需要精度的定点数据。 This example shows how to convert a float to NVARCHAR(50) properly, while also showing why it is a bad idea to use floats for precision data.此示例说明如何将浮点数正确转换为 NVARCHAR(50),同时还说明了为什么将浮点数用于精度数据是一个坏主意。

create table #f ([Column_Name] float)
insert #f select 9072351234
insert #f select 907235123400000000000

select
    cast([Column_Name] as nvarchar(50)),
    --cast([Column_Name] as int), Arithmetic overflow
    --cast([Column_Name] as bigint), Arithmetic overflow
    CAST(LTRIM(STR([Column_Name],50)) AS NVARCHAR(50))
from #f

Output输出

9.07235e+009    9072351234
9.07235e+020    907235123400000010000

You may notice that the 2nd output ends with '10000' even though the data we tried to store in the table ends with '00000'.您可能会注意到,即使我们尝试存储在表中的数据以“00000”结尾,第二个输出也以“10000”结尾。 It is because float datatype has a fixed number of significant figures supported, which doesn't extend that far.这是因为float数据类型支持固定数量的有效数字,这并没有扩展那么远。

For anyone willing to try a different method, they can use this:对于任何愿意尝试不同方法的人,他们可以使用这个:

select FORMAT([Column_Name], '') from YourTable

This will easily change any float value to nvarchar.这将很容易将任何浮点值更改为 nvarchar。

Float won't convert into NVARCHAR directly, first we need to convert float into money datatype and then convert into NVARCHAR, see the examples below. Float不会直接转换成NVARCHAR,首先我们需要把float转换成money数据类型,然后再转换成NVARCHAR,看下面的例子。

Example1示例 1

SELECT CAST(CAST(1234567890.1234  AS FLOAT) AS NVARCHAR(100))

output输出

1.23457e+009

Example2例2

SELECT CAST(CAST(CAST(1234567890.1234  AS FLOAT) AS MONEY) AS NVARCHAR(100))

output输出

1234567890.12

In Example2 value is converted into float to NVARCHAR Example2 中的值被转换为 float 到 NVARCHAR

You can also do something:你还可以做一些事情:

SELECT CAST(CAST(34512367.392 AS decimal(30,9)) AS NVARCHAR(100))

Output: 34512367.392000000输出: 34512367.392000000

I had same problem and i saw your solution.我有同样的问题,我看到了你的解决方案。 Good solution, its worked, thank you... I created a function with your codes.好的解决方案,它的工作,谢谢...我用你的代码创建了一个函数。 Now i use it.现在我用它。 My function is here:我的功能在这里:

create function dbo.fnc_BigNumbertoNvarchar (@MyFloat float)

returns NVARCHAR(50)

AS

BEGIN
    RETURN REPLACE (RTRIM (REPLACE (REPLACE (RTRIM ((REPLACE (CAST (CAST (@MyFloat AS DECIMAL (38 ,18 )) AS VARCHAR( max)), '0' , ' '))), ' ' , '0'), '.', ' ')), ' ','.') 
END

Continuing a1ex07's answer - to use STR function (SQL SERVER),继续a1ex07 的回答- 使用 STR function (SQL SERVER),
and Ronen Festinger's comment - that he gets asterisks instead of digits,和 Ronen Festinger 的评论 - 他得到星号而不是数字,
I wanted to point out that the default length of STR is 10,我想指出STR的默认长度是10,
therefore, for large numbers, don't forget to use the length argument For example: STR(1234567890123, 14)因此,对于大数,不要忘记使用长度参数 例如:STR(1234567890123, 14)

DECLARE @MyFloat [float]

SET @MyFloat = 1000109360.050

SELECT REPLACE (RTRIM (REPLACE (REPLACE (RTRIM ((REPLACE (CAST (CAST (@MyFloat AS DECIMAL (38 ,18 )) AS VARCHAR( max)), '0' , ' '))), ' ' , '0'), '.', ' ')), ' ','.')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM