SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Convert varchar to numeric


Convert varchar to numeric

Author
Message
arun_anand09
arun_anand09
SSC Veteran
SSC Veteran (275 reputation)SSC Veteran (275 reputation)SSC Veteran (275 reputation)SSC Veteran (275 reputation)SSC Veteran (275 reputation)SSC Veteran (275 reputation)SSC Veteran (275 reputation)SSC Veteran (275 reputation)

Group: General Forum Members
Points: 275 Visits: 58
Hi Guys,

This should be easy for some of you - I am looking at SQL Query which will convert succesfully varchar to numeric.

I am using the query below which should do the trick:
Select *
from Table1 a INNER JOIN Table2 b ON Convert(numeric(17,2), b.Col1) = a.Col2

The column b.Col1 is a varchar and a.Col2 is numeric.

When I run this query it gives me an error
[color=Red]Server: Msg 8114, Level 16, State 5, Line 1
Error converting data type varchar to numeric.[/color]


Please correct what I might be doign wrong.

Cheers
Arun
Christopher Stobbs
Christopher Stobbs
SSCrazy Eights
SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)

Group: General Forum Members
Points: 8520 Visits: 2233
are you sure that all your varchar values are numerically compatebly?

----------------------------------------------
Try to learn something about everything and everything about something. - Thomas Henry Huxley

w00t
Posting Best Practices
Numbers / Tally Tables

SQL-4-Life
Christopher Stobbs
Christopher Stobbs
SSCrazy Eights
SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)

Group: General Forum Members
Points: 8520 Visits: 2233
this might work, by converting the numeric to varchar instead.


select *
from thangela.NpowerNorthern_v1 a INNER JOIN EPPS.dbo.Customers b ON b.MPANCORE = CAST(a.MPAN1 as VARCHAR)



----------------------------------------------
Try to learn something about everything and everything about something. - Thomas Henry Huxley

w00t
Posting Best Practices
Numbers / Tally Tables

SQL-4-Life
Lamprey13
Lamprey13
SSCrazy
SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)

Group: General Forum Members
Points: 2180 Visits: 1657
It looks like you have some non numeric data in your column. Maybe this would help:
select 
*
from
thangela.NpowerNorthern_v1 a
INNER JOIN
(
SELECT
--Columns needed,
CAST( b.MPANCORE AS numeric(17,2) AS MPANCORE
FROM
EPPS.dbo.Customers b
WHERE
b.MPANCORE NOT LIKE '%[^0-9.-]%'
) AS T
ON T.MPANCORE = a.MPAN1

EDIT - I didn't read your post correctly. I though you were doing a calculation. So I changed the code.
arun_anand09
arun_anand09
SSC Veteran
SSC Veteran (275 reputation)SSC Veteran (275 reputation)SSC Veteran (275 reputation)SSC Veteran (275 reputation)SSC Veteran (275 reputation)SSC Veteran (275 reputation)SSC Veteran (275 reputation)SSC Veteran (275 reputation)

Group: General Forum Members
Points: 275 Visits: 58
Brilliant both the queries works.

thanks guys!

Keep the good work of sharing knowledge.
Atif-ullah Sheikh
Atif-ullah Sheikh
SSCertifiable
SSCertifiable (6.4K reputation)SSCertifiable (6.4K reputation)SSCertifiable (6.4K reputation)SSCertifiable (6.4K reputation)SSCertifiable (6.4K reputation)SSCertifiable (6.4K reputation)SSCertifiable (6.4K reputation)SSCertifiable (6.4K reputation)

Group: General Forum Members
Points: 6390 Visits: 5209
if you are willing to ignore records with non-numeric value in MPANCORE,
try this


select *
from thangela.NpowerNorthern_v1 a
INNER JOIN EPPS.dbo.Customers b ON case when IsNumeric(b.MPANCORE) = 1 then b.MPANCORE else -1 end = a.MPAN1
-- Asuming -1 will not be the valid value for a.MPAN1



----------------------------------------------------------------------------------------------------------------------------------------------------------------------

Sometimes, winning is not an issue but trying.

You can check my BLOG here


Lamprey13
Lamprey13
SSCrazy
SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)

Group: General Forum Members
Points: 2180 Visits: 1657
Atif Sheikh (4/29/2009)
if you are willing to ignore records with non-numeric value in MPANCORE,
try this


select *
from thangela.NpowerNorthern_v1 a
INNER JOIN EPPS.dbo.Customers b ON case when IsNumeric(b.MPANCORE) = 1 then b.MPANCORE else -1 end = a.MPAN1
-- Asuming -1 will not be the valid value for a.MPAN1

That might work, but you need to be carfull using SQL's "IS" functions..
DECLARE @Foo VARCHAR(50)

SET @Foo = '1e1'

-- Is numeric
SELECT ISNUMERIC(@Foo)

-- Conversion fails
SELECT CAST(@Foo AS NUMERIC(17,2))


Atif-ullah Sheikh
Atif-ullah Sheikh
SSCertifiable
SSCertifiable (6.4K reputation)SSCertifiable (6.4K reputation)SSCertifiable (6.4K reputation)SSCertifiable (6.4K reputation)SSCertifiable (6.4K reputation)SSCertifiable (6.4K reputation)SSCertifiable (6.4K reputation)SSCertifiable (6.4K reputation)

Group: General Forum Members
Points: 6390 Visits: 5209
Thats right. And thanks for pointing.

Also, '1d1' is a Numeric Value but generate error on conversion. The values are valid from '1d1' - '1d308' (if I am not wrong).

But it will be a highly exceptional case to have one occurence of d or e within the range. But, still, the point is valid.

----------------------------------------------------------------------------------------------------------------------------------------------------------------------

Sometimes, winning is not an issue but trying.

You can check my BLOG here


Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search