cast or convert a float to nvarchar?

前端 未结 7 1388
醉话见心
醉话见心 2021-02-03 22:02

I need to select from one column of datatype float and insert it in another column as nvarchar.

I tried to cast it: cast([Column_Name] as nvarchar(50))

7条回答
  •  暗喜
    暗喜 (楼主)
    2021-02-03 22:44

    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.

    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.

    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...

提交回复
热议问题