June 17, 2015 at 9:17 am
Team
As per my requirment using below script to convert from varchar to numeric.
declare @stg varchar(50)
set @stg='12,000,00'
select CONVERT(NUMERIC(17,2),@stg)
getting error : Error converting data type varchar to numeric
Please help..
June 17, 2015 at 9:21 am
The problem is the commas in the string. Try this select statement:
select CONVERT(NUMERIC(17,2),replace(@stg,',',''))
June 17, 2015 at 12:56 pm
I believe this was introduced in SQL 2012 feature but I thought it was interesting.
However, as it is somewhat related I thought I'd mention it. I recently discovered this little nifty function.
declare @stg varchar(50)
set @stg='12,000,00'
SELECT TRY_PARSE(@stg AS NUMERIC)
Also returns a NULL if the conversion fails which could be a good or bad thing depending on the situation.
June 18, 2015 at 8:23 am
Lynn Pettis (6/17/2015)
The problem is the commas in the string. Try this select statement:select CONVERT(NUMERIC(17,2),replace(@stg,',',''))
Yes, it will get rid of the commas, but it seems odd that a comma would appear just 2 places to the left of where the decimal point is usually located, as European numbering often uses a comma instead of a decimal point, so your CONVERT will deliver a value of 1,200,000, where a value of 12,000 might be expected. There's no way for us to know without the original poster chiming in on this...
If it is indeed a decimal point type of comma, we might need something a little more nuanced:
DECLARE @stg AS varchar(50) = '12,000,00';
SELECT CONVERT(numeric(17,2), REPLACE(LEFT(@stg, LEN(@stg) - 3), ',', '') + REPLACE(RIGHT(@stg, 3), ',', '.'))
Minnu, what say you?
Steve (aka sgmunson) 🙂 🙂 🙂
Rent Servers for Income (picks and shovels strategy)
June 18, 2015 at 3:01 pm
sgmunson (6/18/2015)
Lynn Pettis (6/17/2015)
The problem is the commas in the string. Try this select statement:select CONVERT(NUMERIC(17,2),replace(@stg,',',''))
Yes, it will get rid of the commas, but it seems odd that a comma would appear just 2 places to the left of where the decimal point is usually located, as European numbering often uses a comma instead of a decimal point, so your CONVERT will deliver a value of 1,200,000, where a value of 12,000 might be expected. There's no way for us to know without the original poster chiming in on this...
If it is indeed a decimal point type of comma, we might need something a little more nuanced:
DECLARE @stg AS varchar(50) = '12,000,00';
SELECT CONVERT(numeric(17,2), REPLACE(LEFT(@stg, LEN(@stg) - 3), ',', '') + REPLACE(RIGHT(@stg, 3), ',', '.'))
Minnu, what say you?
If the comma is a decimal point, shouldn't the other comma then be a '.'?
June 18, 2015 at 8:09 pm
Lynn Pettis (6/18/2015)
sgmunson (6/18/2015)
Lynn Pettis (6/17/2015)
The problem is the commas in the string. Try this select statement:select CONVERT(NUMERIC(17,2),replace(@stg,',',''))
Yes, it will get rid of the commas, but it seems odd that a comma would appear just 2 places to the left of where the decimal point is usually located, as European numbering often uses a comma instead of a decimal point, so your CONVERT will deliver a value of 1,200,000, where a value of 12,000 might be expected. There's no way for us to know without the original poster chiming in on this...
If it is indeed a decimal point type of comma, we might need something a little more nuanced:
DECLARE @stg AS varchar(50) = '12,000,00';
SELECT CONVERT(numeric(17,2), REPLACE(LEFT(@stg, LEN(@stg) - 3), ',', '') + REPLACE(RIGHT(@stg, 3), ',', '.'))
Minnu, what say you?
If the comma is a decimal point, shouldn't the other comma then be a '.'?
Not necessarily.. but your point is valid... That's why I asked the original poster to respond.
Steve (aka sgmunson) 🙂 🙂 🙂
Rent Servers for Income (picks and shovels strategy)
June 18, 2015 at 11:52 pm
Minnu (6/17/2015)
TeamAs per my requirment using below script to convert from varchar to numeric.
declare @stg varchar(50)
set @stg='12,000,00'
select CONVERT(NUMERIC(17,2),@stg)
getting error : Error converting data type varchar to numeric
Please help..
If the answer is supposed to be 12000.00, then the following works.
DECLARE @stg MONEY;
SET @stg = '12,000,00';
SELECT CONVERT(NUMERIC(17,2),@stg/100.0);
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 7 posts - 1 through 6 (of 6 total)
You must be logged in to reply to this topic. Login to reply