Home > front end >  Convert float to varchar in SQL and remove the value after decimal
Convert float to varchar in SQL and remove the value after decimal

Time:10-06

I have table contains brand ID looks like the below: BrandID is stored as float.

| BrandID | 
| -------- | 
| 1000962.000000   | 
| 1000565.000000   | 

How can I convert this brand ID into varchar(11) and remove the value after .(dot) What I want is:

| BrandID  | 
| -------- | 
| 1000962  | 
| 1000565  | 

I tried SELECT CAST([brand ID] AS Varchar) but failed and get another error String or binary data would be truncated. How to remove the value after . and then convert it to varchar? Thank you very much.

CodePudding user response:

You should convert float to int first, then convert it again to nvarchar.

try this:

SELECT CAST(CAST([BrandID] AS INT) AS NVARCHAR)
  • Related