Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
duro
Starting Member
5 Posts |
Posted - 2012-10-13 : 13:56:06
|
hellowhen i run the following sql query:"update table set price = price * 1.1 "i get the following error :"Msg 8115, Level 16, State 8, Line 1Arithmetic overflow error converting nvarchar to data type numeric.The statement has been terminated."the table is set to nvarchar, and i am just trying to make the prices go up 10%any ideas?thanks! |
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
duro
Starting Member
5 Posts |
Posted - 2012-10-13 : 14:13:40
|
thank you for your reply almight sql goddess,when i go into the design view and try and change it to money, then i try and save it, it says "saving changes is not permitted" any ideas? |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
duro
Starting Member
5 Posts |
Posted - 2012-10-14 : 04:51:39
|
when i runselect * from table order by price asci get...115.5036.004.954.954.9547.3049.5049.5053.90when i should get4.954.954.9536.0047.3049.5049.5053.90115.50what do i need to change in the sql query ? |
|
|
sunitabeck
Master Smack Fu Yak Hacker
5155 Posts |
Posted - 2012-10-14 : 15:14:47
|
Couple of things:a) As Tara suggested, the problem (in ordering and in updating) is happening because you have a nvarchar column for the price. That is not a good choice because prices, if I am not mistaken, should always contain numeric values. If you are allowed to change the data type, change it as Tara suggested. If you are on a production server, be very careful - there may be other code that depends on the column being of type nvarcharWhen you try to change it in the design view, it is not letting you change it because of a setting. To change this setting, in SSMS, go to Tools-> Options -> Designers -> Table and Database Designers and uncheck the "Prevent saving changes that require table-recreation". HOWEVER, that means, when you make that change, SQL Server is really recreating the table for you. It will do that very gracefully - but still if your table has a lot of records, that can be a scary thing to do, especially on a production server.b) As to your question about why the prices are not being ordered as you expected - again, this is because the column is of character type. In character ordering, each character starting from the left is looked at. So comparing 115.50 and 36.00, it compares the 1 and 3 and decides that 1 is less than 3, so 115.50 will come before 36.00. Change your ordering to this:select * from table order by CAST(price AS FLOAT) asc |
|
|
|
|
|
|
|