Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Extracting a Numeric Reference from a VARCHAR field


Extracting a Numeric Reference from a VARCHAR field

Author
Message
brewmanz
brewmanz
SSC-Addicted
SSC-Addicted (489 reputation)SSC-Addicted (489 reputation)SSC-Addicted (489 reputation)SSC-Addicted (489 reputation)SSC-Addicted (489 reputation)SSC-Addicted (489 reputation)SSC-Addicted (489 reputation)SSC-Addicted (489 reputation)

Group: General Forum Members
Points: 489 Visits: 406
Comments posted to this topic are about the item Extracting a Numeric Reference from a VARCHAR field
Jamie-2229
Jamie-2229
SSCrazy
SSCrazy (3K reputation)SSCrazy (3K reputation)SSCrazy (3K reputation)SSCrazy (3K reputation)SSCrazy (3K reputation)SSCrazy (3K reputation)SSCrazy (3K reputation)SSCrazy (3K reputation)

Group: General Forum Members
Points: 2955 Visits: 831
declare @myTab table( ExtRef varchar(9), IntRef decimal(38,19))
insert into @myTab(ExtRef,IntRef) values ('1',0)
insert into @myTab(ExtRef,IntRef) values ('2.1',0)
insert into @myTab(ExtRef,IntRef) values ('A',0)
update @myTab
set IntRef = case when isnumeric(ExtRef) = 1 then ExtRef else -1
--(A)
end

update @myTab
set IntRef = case when isnumeric(ExtRef) = 1 then ExtRef else -1
--(A)
end


Confused here --(A) is a comment, yes?

The cast to decimal would be required otherwise a failure converting varchar to decimal.

Jamie
jim.hamer
jim.hamer
SSCommitted
SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)

Group: General Forum Members
Points: 1861 Visits: 414
I'm using SQL Server 2008.

To get the script to work for decimal numbers like 35.12, I had to change
when isnumeric(ExtRef) = 1 then ExtRef
to
when isnumeric(ExtRef) = 1 then cast(ExtRef as decimal)

After I made that change, I was able find the problem that decimal data with dollar signs are considered numeric but can't be cast as decimals.

Jim
Jamie-2229
Jamie-2229
SSCrazy
SSCrazy (3K reputation)SSCrazy (3K reputation)SSCrazy (3K reputation)SSCrazy (3K reputation)SSCrazy (3K reputation)SSCrazy (3K reputation)SSCrazy (3K reputation)SSCrazy (3K reputation)

Group: General Forum Members
Points: 2955 Visits: 831
Jim,

Out of the box SQL 2005

declare @myTab table( ExtRef varchar(9), IntRef decimal(38,19))
insert into @myTab(ExtRef,IntRef) values ('$1.00',0)
insert into @myTab(ExtRef,IntRef) values ('2.1',0)
insert into @myTab(ExtRef,IntRef) values ('A',0)
update @myTab
set IntRef = case when isnumeric(ExtRef) = 1 then cast(ExtRef as decimal(38,19))else cast(-1 as decimal(38,19))
--(A)
end

Good catch on the dollar sign!

Jamie
Mauve
Mauve
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: 1244 Visits: 2049
Additionally,

1. It doesn't properly support a legitimate value of -1 as -1 is being used to indicate that the value is not numeric.

2. An explicit CAST is needed in the assignment part ("THEN") of the CASE statement as for some quirky reason, a value of '123.45' cannot be implicitly converted to DECIMAL(38,19) when it is part of a CASE statement. Error: Conversion failed when converting the varchar value '123.45' to data type int. Yet a direct assignment (IntRef = ExtRef) works fine! (SQL Server 2005 SP2)


(PHB) I think we should build an SQL database. (Dilbert) What color do you want that database? (PHB) I think mauve has the most RAM.
stan rydzewski
stan rydzewski
Grasshopper
Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)

Group: General Forum Members
Points: 21 Visits: 224
I see the exact same behavior with 2000.
jarney
jarney
Hall of Fame
Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)

Group: General Forum Members
Points: 3359 Visits: 675
JohnG -
If you cast the -1 to Decimal(38,19), you won't get the error on 123.45

Great question.



Mohit K. Gupta
Mohit K. Gupta
SSC Eights!
SSC Eights! (970 reputation)SSC Eights! (970 reputation)SSC Eights! (970 reputation)SSC Eights! (970 reputation)SSC Eights! (970 reputation)SSC Eights! (970 reputation)SSC Eights! (970 reputation)SSC Eights! (970 reputation)

Group: General Forum Members
Points: 970 Visits: 1089
I liked the question; I didn't think about it when I answered it much (so I got it wrong Sad). The SQL syntax looked right, but needed to read it a bit more carefully I guess.

I just got confused by -- (A); I wasn't sure what was being implied by that when I read the options available to me.

---

Mohit K. Gupta, MCITP: Database Administrator (2005), My Blog, Twitter: @SQLCAN.
Microsoft FTE - SQL Server PFE

* Some time its the search that counts, not the finding...
* I didn't think so, but if I was wrong, I was wrong. I'd rather do something, and make a mistake than be frightened and be doing nothing. Smooooth


How to ask for help .. Read Best Practices here.
TheRedneckDBA
TheRedneckDBA
SSCommitted
SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)

Group: General Forum Members
Points: 1864 Visits: 2592
Good one!

The Redneck DBA
Hugo Kornelis
Hugo Kornelis
SSCrazy Eights
SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)

Group: General Forum Members
Points: 8307 Visits: 11538
JohnG (11/20/2008)
Additionally,

1. It doesn't properly support a legitimate value of -1 as -1 is being used to indicate that the value is not numeric.

2. An explicit CAST is needed in the assignment part ("THEN") of the CASE statement as for some quirky reason, a value of '123.45' cannot be implicitly converted to DECIMAL(38,19) when it is part of a CASE statement. Error: Conversion failed when converting the varchar value '123.45' to data type int. Yet a direct assignment (IntRef = ExtRef) works fine! (SQL Server 2005 SP2)


Hi John,

Well, your first point is by design. If the design calls for -1 being used to represent incorrect format, you can't blame the code for using -1 for two purposes. Apparently, the assumption here is that -1 will never be a real value in the data. Smile

WRT the second point - the reason is not "quirky" at all. The data type of the CASE is always equal to a data type used in one of the WHEN clauses or the ELSE clause, with the choice being determined by the rules of precedence. In the posted code, data types used were varchar(9) (from the ExtRef column) and int (from the constant -1, as a constant with no decimal point and within the range of integers is always considered to be int). Of these, int has the highest precedence, so the varchar is converted. And after that, the result of the CASE is converted to decimal(38,19) for the purpose of assiging it to the IntRef column.
I'll gladly admit that I missed this double conversion myself. Fortunately I did not lose a point over it as I am well aware of the limitations of ISNUMERIC(), but it wasn't until I read the comments here that I noticed the code would choke on "really" valid decimal numbers as well due to the hidden conversion to integer.

Good question!


Hugo Kornelis, SQL Server MVP
Visit my SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
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