开发者

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

开发者 https://www.devze.com 2023-02-04 11:41 出处:网络
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.

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开发者_如何学JAVA 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).


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消