Search code examples
sqlsql-servert-sqlsql-server-2008alter-column

TSQL: How are values converted when altering a numeric column in sql server?


This is on SQL Server 2008.

I have several columns I want to convert from money and decimal to varchar, for example a column called item_amount.

  • How will these values be converted?

    Will it be the same as convert(varchar, item_amount)? Running a query like select item_amount, convert(varchar, item_amount) from <table> renders the columns identically, which is what I would expect and want.

  • I should be safe from possible truncation, correct?

    Assuming there are enough characters in the varchar column (which would be 39, since the max precision for a decimal column is 38 + 1 character for the decimal point). None of the numeric values are even close to 38 digits, most in the 3-5 range.

  • I've run this command successfully on a test table and want to make sure I'm not overlooking or forgetting something that's going to screw me: alter table <mytable> alter column item_amount varchar(39) default '0' (this is after droping the existing default ((0)) constraint).


Solution

  • With regard to the way conversion is done, yes you are correct as long as the VARCHAR column you are placing it in has the right number of characters available you will be set to go.

    With regards to your change of amount to varchar, you should be fine here as it will do the conversion.

    I just have to note that it doesn't sound like a good idea to do this as you are no longer interacting with numbers for sorting, filtering, etc....but just a note.