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


convert varchar to numeric


convert varchar to numeric

Author
Message
varshini
varshini
SSC Veteran
SSC Veteran (257 reputation)SSC Veteran (257 reputation)SSC Veteran (257 reputation)SSC Veteran (257 reputation)SSC Veteran (257 reputation)SSC Veteran (257 reputation)SSC Veteran (257 reputation)SSC Veteran (257 reputation)

Group: General Forum Members
Points: 257 Visits: 107
how to convert (10,000) a varchar data type value to numeric ?
GilaMonster
GilaMonster
SSC Guru
SSC Guru (411K reputation)SSC Guru (411K reputation)SSC Guru (411K reputation)SSC Guru (411K reputation)SSC Guru (411K reputation)SSC Guru (411K reputation)SSC Guru (411K reputation)SSC Guru (411K reputation)

Group: General Forum Members
Points: 411489 Visits: 47113
CAST or CONVERT. See Books Online (the SQL help file) for details.

Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass


varshini
varshini
SSC Veteran
SSC Veteran (257 reputation)SSC Veteran (257 reputation)SSC Veteran (257 reputation)SSC Veteran (257 reputation)SSC Veteran (257 reputation)SSC Veteran (257 reputation)SSC Veteran (257 reputation)SSC Veteran (257 reputation)

Group: General Forum Members
Points: 257 Visits: 107
how to convert comma seperated string to numeric ?
GilaMonster
GilaMonster
SSC Guru
SSC Guru (411K reputation)SSC Guru (411K reputation)SSC Guru (411K reputation)SSC Guru (411K reputation)SSC Guru (411K reputation)SSC Guru (411K reputation)SSC Guru (411K reputation)SSC Guru (411K reputation)

Group: General Forum Members
Points: 411489 Visits: 47113
Use REPLACE to remove the commas, then use CAST or CONVERT. Alternatively, you could cast first to the Money data type (providing it has sufficient precision) and then cast to numeric.

Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass


LutzM
LutzM
SSC-Forever
SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)

Group: General Forum Members
Points: 42299 Visits: 13559
First of all you need to make sure to know what that value actually represents:
It could be either 10000 or 10.
Based on that you'd have to replace the comma with an empty string or with a '.' (dot).



Lutz
A pessimist is an optimist with experience.

How to get fast answers to your question
How to post performance related questions
Links for Tally Table , Cross Tabs and Dynamic Cross Tabs , Delimited Split Function
lduvall
lduvall
SSCrazy
SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)

Group: General Forum Members
Points: 2066 Visits: 397
I am attempting to convert a varchar to numeric. However, I am trying to convert just a substring of the varchar.
I know with 100% certainty that this query only returns numeric info. However, it won't let me convert the substring. (I tried cast, too.) I've consulted books online. I am just not sure whether my using substring is possible.

SELECT SUBSTRING(Col4,1,CHARINDEX('%',Col4)-1)
FROM Executive.StagedimWeight
WHERE Col4 IS NOT NULL
AND Col4 LIKE '%\%%'ESCAPE '\'


The above query returns:
35
65

However, if I try this convert syntax:
SELECT CONVERT(NUMERIC(5,4),SUBSTRING(Col4,1,CHARINDEX('%',Col4)-1))
FROM Executive.StagedimWeight
WHERE Col4 IS NOT NULL
AND Col4 LIKE '%\%%'ESCAPE '\'

I get: Msg 8115, Level 16, State 8, Line 1
Arithmetic overflow error converting nvarchar to data type numeric.

Any help greatly appreciated!
Steve Jones
Steve Jones
SSC Guru
SSC Guru (247K reputation)SSC Guru (247K reputation)SSC Guru (247K reputation)SSC Guru (247K reputation)SSC Guru (247K reputation)SSC Guru (247K reputation)SSC Guru (247K reputation)SSC Guru (247K reputation)

Group: Administrators
Points: 247447 Visits: 19791
You have some data in that column that does not convert correctly. That's the overflow.

No matter what you think, this is a common error and a reason why you store numeric data in numeric data types. There is some value in your column which is not converting,

Follow me on Twitter: @way0utwest
Forum Etiquette: How to post data/code on a forum to get the best help
My Blog: www.voiceofthedba.com
lduvall
lduvall
SSCrazy
SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)

Group: General Forum Members
Points: 2066 Visits: 397
Finally got it! I'm not sure if this is the best way, but it worked for me:

select CONVERT(NUMERIC(3,0),(CONVERT(int,SUBSTRING(Col4,1,CHARINDEX('%',Col4)-1))))/100
FROM Executive.StagedimWeight
WHERE Col4 IS NOT NULL
AND Col4 LIKE '%\%%'ESCAPE '\'
fahey.jonathan
fahey.jonathan
Ten Centuries
Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)

Group: General Forum Members
Points: 1204 Visits: 402
The problem with the original query is that it was trying to convert to a NUMERIC(5,4), which will hold a maximum value of 9.9999. The value "35" has two digits left of the decimal, but NUMERIC(5,4) allows only one. Therefore, an overflow resulted. The query that worked (above post) changed the CONVERT to use NUMERIC(3,0), resulting in sufficient space to hold a two-digit number. I suggest you use a NUMERIC value large enough to hold all the expected values. For example, if you want to hold 10,000 as listed in the original post, you will need a NUMERIC(5,0), NUMERIC(6,1), or something where the first digit of the NUMERIC definition minus the second digit is 5 or greater (5-0=5; 6-1=5, etc).
sivaj2k
sivaj2k
SSC-Addicted
SSC-Addicted (442 reputation)SSC-Addicted (442 reputation)SSC-Addicted (442 reputation)SSC-Addicted (442 reputation)SSC-Addicted (442 reputation)SSC-Addicted (442 reputation)SSC-Addicted (442 reputation)SSC-Addicted (442 reputation)

Group: General Forum Members
Points: 442 Visits: 549
Hi

select convert(bigint,REPLACE ('10,00,000',',',''))

Siva Kumar J
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