I have a column called Odo that contains the number of meters in a trip. I would normally divide that by 1000 to display the number of Km's.
The line of code in question:
convert(varchar(10), startPos.Distance / 1000)
causes the following error
Msg 8115, Level 16, State 8, Procedure sp_report_select_trip_start_and_stop, Line 7 [Batch Start Line 2]
Arithmetic overflow error converting numeric to data type numeric.Msg 232, Level 16, State 2, Procedure sp_report_select_trip_start_and_stop, Line 9 [Batch Start Line 2]
Arithmetic overflow error for type varchar, value = 931.785156.
That number is clearly longer than my varchar. How do I divide, truncate to 1 decimal place and then convert?
Edit: SQL Server 2008 R2, so format() is not available
round(931.785156,1)or useformat()sp_prefix for your stored procedures. Microsoft has reserved that prefix for its own use (see Naming Stored Procedures), and you do run the risk of a name clash sometime in the future. It's also bad for your stored procedure performance. It's best to just simply avoidsp_and use something else as a prefix - or no prefix at all!