Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

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

The result was 9.07235e+009 instead of a 10 digit number (phone number).

Does any one know how to cast or convert this data properly?

like image 849
Eugene Avatar asked Feb 03 '11 00:02

Eugene


People also ask

Is it better to use CAST () or convert ()?

CAST is also less powerful and less flexible than CONVERT. On the other hand, CONVERT allows more flexibility and is the preferred function to use for data, time values, traditional numbers, and money signifiers. CONVERT is also useful in formatting the data's format. What is this?

Which is faster convert or cast?

Summary. Casting and converting are ways in which we can change a value from one type to another; casting is faster but more prone to errors, while conversion is more computationally expensive but also more forgiving.

Can you convert varchar to Nvarchar?

NVARCHAR has higher precedence than the VARCHAR data type. Therefore, during the data type conversion, SQL Server converts the existing VARCHAR values into NVARCHAR.

How do I convert and cast in SQL?

Cast() Function in SQL ServerThe Cast() function is used to convert a data type variable or data from one data type to another data type. The Cast() function provides a data type to a dynamic parameter (?) or a NULL value. The data type to which you are casting an expression is the target type.

How to convert float data to NVARCHAR data in MS SQL Server?

Here we will see, how to convert FLOAT data to NVARCHAR data in an MS SQL Server’s database table using the CAST (), CONVERT (), and FORMAT () functions. We will be creating a person table in a database called “geeks”. We have the following Employee table in our geeks database :

How do you cast a float to a nvarchar?

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.

How do I convert a phone number to a nvarchar?

Must use LTRIM or CONVERT to decimal first. 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. In these examples the innermost cast (1234567890 as float) is used in place of selecting a value from the appropriate column.

How to convert float field to varchar field?

I needed to convert the float field to varchar as I need to display NA when NULL and 0 as it is. I achieved this by adding CASE statement in the query as below; CASE WHEN float_field IS NULL THEN 'NA' WHEN float_field = 0 THEN '0' ELSE CONVERT (VARCHAR, float_field, 128) END AS float_As_VChar


2 Answers

Check STR. You need something like SELECT STR([Column_Name],10,0) ** This is SQL Server solution, for other servers check their docs.

like image 158
a1ex07 Avatar answered Sep 29 '22 16:09

a1ex07


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

like image 20
Coxy Avatar answered Sep 29 '22 15:09

Coxy