SQL Server 2008 - Alter column to Varchar from Flo

2019-09-02 09:52发布

问题:

I am having to change one of the columns in table which currently is Float to Varchar, but when I use alter command, it stores some of the longer numbers in Scientific Notation.

Can I avoid this?

If not, is there a way to easily update the table later to store the scientific notation as normal integer?

Thanks

回答1:

Please check the link

convert float into varchar in SQL server without scientific notation

You can cast the float to varchar(max)

How to convert float to varchar in SQL Server



回答2:

I have a workaround for this I have used in the past. Dan isn't far off, but just casting it won't work. You can alter the table by adding a new varchar column then using str and ltrim to update the new column from the old float column. Say your varchar column was varchar(50), use something like:

update YourTable set NewColumn = ltrim(str(OldColumn, 50))

str() converts to character data, ltrim() gets rid of any extra blanks on the left. You can then always get rid of the old column. Feels janky but should work for you.