Solved

ms sql server 2005 error converting varchar to numeric

Posted on 2011-09-28
5
188 Views
Last Modified: 2012-05-12
not sure why are having issues converting here..

 declare @home_spread nvarchar(max)


declare @home_spread_numeric numeric(30,2)

-- the value will look like '+33.5' or '-4.5 or +7'
set @home_spread = (select home_team_spread from schedule_shoe where shoe_id = @shoe_id)


-- ==================================================
set @home_spread = (replace(@home_spread,'-',''))

set @home_spread = (replace(@home_spread,'+',''))



-- =================================
-- IF NO DECIMAL EXISTS PROVIDE IT...
-- ======================================
if @home_spread not like '%.%'
begin
     set @home_spread_numeric = cast(@away_spread as numeric(30,2)) + '.0'
end
0
Comment
Question by:GlobaLevel
5 Comments
 
LVL 10

Author Comment

by:GlobaLevel
Comment Utility
Any ideas?
0
 
LVL 45

Expert Comment

by:Vitor Montalvão
Comment Utility
Where it fails?
And in the last line  
set @home_spread_numeric = cast(@away_spread as numeric(30,2)) + '.0'
where is the @away_spread came from? You don't need the " +'0' " because you are converting already to 2 decimal places
0
 
LVL 22

Accepted Solution

by:
8080_Diver earned 500 total points
Comment Utility
I suspect thaqt the line in question shoud read:
set @home_spread_numeric = cast(@home_spread as numeric(30,2)) + '.0'

Open in new window


As previously stated, forget the '.0', not only because it is unneeded but also because you are trying to add an Alphanumeric (i.e. CHAR or VARCHAR) to a numeric.  If you feel compelled to add 0 tenths to the value, use:
set @home_spread_numeric = cast(@home_spread as numeric(30,2)) + 0.0

Open in new window

0
 
LVL 22

Expert Comment

by:8080_Diver
Comment Utility
Alternatively you could use:
SET @home_spread = @home_spread + '.0'
set @home_spread_numeric = cast(@home_spread as numeric(30,2))

Open in new window

0
 
LVL 19

Expert Comment

by:Bhavesh Shah
Comment Utility
Hi,

I made bit change in SQL.

check out.
declare @home_spread nvarchar(max),@shoe_id int


declare @home_spread_numeric numeric(30,2)

-- the value will look like '+33.5' or '-4.5 or +7'
set @home_spread = (select Replace(Replace(home_team_spread,'+',''),'-','') from schedule_shoe where shoe_id = @shoe_id)

IF IsNumeric(@home_spread)=1
begin
     set @home_spread_numeric = cast(@home_spread as numeric(30,1))
end

Open in new window

0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Join & Write a Comment

Introduction: When running hybrid database environments, you often need to query some data from a remote db of any type, while being connected to your MS SQL Server database. Problems start when you try to combine that with some "user input" pass…
Data architecture is an important aspect in Software as a Service (SaaS) delivery model. This article is a study on the database of a single-tenant application that could be extended to support multiple tenants. The application is web-based develope…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now