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


CONVERT nvarchar to numeric


CONVERT nvarchar to numeric

Author
Message
jsheldon
jsheldon
Right there with Babe
Right there with Babe (716 reputation)Right there with Babe (716 reputation)Right there with Babe (716 reputation)Right there with Babe (716 reputation)Right there with Babe (716 reputation)Right there with Babe (716 reputation)Right there with Babe (716 reputation)Right there with Babe (716 reputation)

Group: General Forum Members
Points: 716 Visits: 425
I need to sum up numeric data from an analysis cube however #miss is shown on the fields with zero data.

[Column 13] is the column on table #temp It was from its original table nvarchar.

I am trying to do a convert to 0.00 but am failing with each syntax I am doing.

CONVERT (decimal(2,2),[Column 13])

or

CONVERT (decimal(2,2),[Column 13],2)

Need some assistance..thanks
Rajib Bahar
Rajib Bahar
SSC-Enthusiastic
SSC-Enthusiastic (173 reputation)SSC-Enthusiastic (173 reputation)SSC-Enthusiastic (173 reputation)SSC-Enthusiastic (173 reputation)SSC-Enthusiastic (173 reputation)SSC-Enthusiastic (173 reputation)SSC-Enthusiastic (173 reputation)SSC-Enthusiastic (173 reputation)

Group: General Forum Members
Points: 173 Visits: 245
Your convert function looks fine to me...

I tried this and it worked:
DECLARE @tmp TABLE ([Column 13] nvarchar(20))
INSERT INTO @tmp SELECT '0.00'
select CONVERT (decimal(2,2),[Column 13]) from @tmp
select CONVERT (decimal(2,2),[Column 13],2) from @tmp

Have you considered using the IsNumeric() function to check whether the data is numerically convertible or not?

Rajib Bahar
http://www.rajib-bahar.com
http://www.twitter.com/rajib2k5
http://www.youtube.com/icsql
jsheldon
jsheldon
Right there with Babe
Right there with Babe (716 reputation)Right there with Babe (716 reputation)Right there with Babe (716 reputation)Right there with Babe (716 reputation)Right there with Babe (716 reputation)Right there with Babe (716 reputation)Right there with Babe (716 reputation)Right there with Babe (716 reputation)

Group: General Forum Members
Points: 716 Visits: 425
Hi Rajib,

I did the following query:

SELECT ISNUMERIC(column_13) from #temp

The column brought back 0 and 1s

Again this column that has budget data. So values like 100, 105, 105.67, 9845.43, #MISS appear

I tried to do a SELECT CONVERT(numeric 18,2),column_13) AS convert_col13 INTO #temp2 from #temp but I still get error about unable to convert nvarchar to numeric
fhanlon
fhanlon
SSCrazy
SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)

Group: General Forum Members
Points: 2673 Visits: 2303
to convert to 0.00 format you need CONVERT(decimal(3,2),[column 13]) oytherwise you will get a Arithmetic overflow error converting varchar to data type numeric. error. 0,00 is 3 digits long 2 of which are after the decimal place. decimal(2,2) means your answer is 2 digits long of which 2 digits are to the right of the decimal; example .00



Francis
jsheldon
jsheldon
Right there with Babe
Right there with Babe (716 reputation)Right there with Babe (716 reputation)Right there with Babe (716 reputation)Right there with Babe (716 reputation)Right there with Babe (716 reputation)Right there with Babe (716 reputation)Right there with Babe (716 reputation)Right there with Babe (716 reputation)

Group: General Forum Members
Points: 716 Visits: 425
Tried that and no dice

SELECT *, CONVERT(decimal(3,2),[Column 13]) AS CONVERT13
INTO #TEMP2 FROM #TEMP
fhanlon
fhanlon
SSCrazy
SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)

Group: General Forum Members
Points: 2673 Visits: 2303
If you are getting an error please post it



Francis
Rajib Bahar
Rajib Bahar
SSC-Enthusiastic
SSC-Enthusiastic (173 reputation)SSC-Enthusiastic (173 reputation)SSC-Enthusiastic (173 reputation)SSC-Enthusiastic (173 reputation)SSC-Enthusiastic (173 reputation)SSC-Enthusiastic (173 reputation)SSC-Enthusiastic (173 reputation)SSC-Enthusiastic (173 reputation)

Group: General Forum Members
Points: 173 Visits: 245
add the error, your temp table script, insert script, and anything else that gives everyone a clear picture

Rajib Bahar
http://www.rajib-bahar.com
http://www.twitter.com/rajib2k5
http://www.youtube.com/icsql
Jack Corbett
  Jack Corbett
SSCoach
SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)

Group: General Forum Members
Points: 18748 Visits: 14899
jsheldon (4/16/2009)
Hi Rajib,

I did the following query:

SELECT ISNUMERIC(column_13) from #temp

The column brought back 0 and 1s

Again this column that has budget data. So values like 100, 105, 105.67, 9845.43, #MISS appear

I tried to do a SELECT CONVERT(numeric 18,2),column_13) AS convert_col13 INTO #temp2 from #temp but I still get error about unable to convert nvarchar to numeric


The bolded item in your list is at least one of the things that is causing the error. You can't convert '#MISS' to a numeric. You could try doing:


SELECT
CONVERT(numeric(18, 2), column_13) AS convert_col13
INTO
#temp2
from
#temp
WHERE -- only do ones that could be numeric.
ISNUMERIC(column_13) = 1



You could still get some errors as currency characters, '-', and '.' are among characters that cause isnumeric to return 1.



Jack Corbett

Applications Developer

Don't let the good be the enemy of the best. -- Paul Fleming
At best you can say that one job may be more secure than another, but total job security is an illusion. -- Rod at work

Check out these links on how to get faster and more accurate answers:
Forum Etiquette: How to post data/code on a forum to get the best help
Need an Answer? Actually, No ... You Need a Question
How to Post Performance Problems
Crosstabs and Pivots or How to turn rows into columns Part 1
Crosstabs and Pivots or How to turn rows into columns Part 2
fhanlon
fhanlon
SSCrazy
SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)

Group: General Forum Members
Points: 2673 Visits: 2303
try using a CASE statement like


SELECT column_13
, CASE ISNUMERIC(column_13)
WHEN 1 THEN CONVERT(numeric(18,2),column_13)
ELSE 0.00
END
FROM #temp





Francis
jsheldon
jsheldon
Right there with Babe
Right there with Babe (716 reputation)Right there with Babe (716 reputation)Right there with Babe (716 reputation)Right there with Babe (716 reputation)Right there with Babe (716 reputation)Right there with Babe (716 reputation)Right there with Babe (716 reputation)Right there with Babe (716 reputation)

Group: General Forum Members
Points: 716 Visits: 425
Francis,

Your code was the best fit...thanks to all for the help...
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