﻿<?xml version='1.0' encoding='UTF-8'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>SQLServerCentral / Article Discussions / Article Discussions by Author / Discuss content posted by brewmanz  / Extracting a Numeric Reference from a VARCHAR field / Latest Posts</title><generator>InstantForum.NET v2.9.0</generator><description>SQLServerCentral</description><link>http://www.sqlservercentral.com/Forums/</link><webMaster>notifications@sqlservercentral.com</webMaster><lastBuildDate>Thu, 20 Jun 2013 04:34:02 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: Extracting a Numeric Reference from a VARCHAR field</title><link>http://www.sqlservercentral.com/Forums/Topic605532-1401-1.aspx</link><description>[quote][b]brewmanz (11/24/2008)[/b][hr]I'm glad that this QOTD caused some deep thinking. It was prompted by a real-life problem, although it wasn't as constrained as I made it. The original input field was VARCHAR255) and failing on convert to INT when it had about 30 digits. The code was so bad that most would have seen the potential flaws. On playing with a way to fix it, I realised how (just about) useless ISNUMERIC is. What's the proper solution? I haven't actually worked that out! LOLI like writing QOTD (okay, I've only done 2 so far!) where you can't easily cheat by just paste-and-go (like, umm, the author has been know to do).I'm working on another one using date formats. Sadly, I have to work for a living so progress isn't as quick as I would like. Still, watch this space!RegardsBrewmanz[/quote]Definitely one of the better QOTD.  The type of teh case statement was obviously wrong but easily fixable by changing the line marked (A), so it would be easy to go for the wrong answer without thinking about the real meaning of isNumeric.  And thd explanation of teh anser is clear and correct.   I liked this one very much.</description><pubDate>Thu, 25 Jun 2009 12:24:31 GMT</pubDate><dc:creator>L' Eomot Inversé</dc:creator></item><item><title>RE: Extracting a Numeric Reference from a VARCHAR field</title><link>http://www.sqlservercentral.com/Forums/Topic605532-1401-1.aspx</link><description>Nice one...:)</description><pubDate>Mon, 24 Nov 2008 03:21:59 GMT</pubDate><dc:creator>Anipaul</dc:creator></item><item><title>RE: Extracting a Numeric Reference from a VARCHAR field</title><link>http://www.sqlservercentral.com/Forums/Topic605532-1401-1.aspx</link><description>I'm glad that this QOTD caused some deep thinking. It was prompted by a real-life problem, although it wasn't as constrained as I made it. The original input field was VARCHAR255) and failing on convert to INT when it had about 30 digits. The code was so bad that most would have seen the potential flaws. On playing with a way to fix it, I realised how (just about) useless ISNUMERIC is. What's the proper solution? I haven't actually worked that out! LOLI like writing QOTD (okay, I've only done 2 so far!) where you can't easily cheat by just paste-and-go (like, umm, the author has been know to do).I'm working on another one using date formats. Sadly, I have to work for a living so progress isn't as quick as I would like. Still, watch this space!RegardsBrewmanz</description><pubDate>Mon, 24 Nov 2008 03:09:46 GMT</pubDate><dc:creator>brewmanz</dc:creator></item><item><title>RE: Extracting a Numeric Reference from a VARCHAR field</title><link>http://www.sqlservercentral.com/Forums/Topic605532-1401-1.aspx</link><description>[quote][b]JohnG (11/20/2008)[/b][hr]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) [u]when it is part of a CASE statement[/u].  Error: [i]Conversion failed when converting the varchar value '123.45' to data type int.[/i]  Yet a direct assignment (IntRef = ExtRef) works fine!  (SQL Server 2005 SP2)[/quote]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. :)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!</description><pubDate>Mon, 24 Nov 2008 01:33:52 GMT</pubDate><dc:creator>Hugo Kornelis</dc:creator></item><item><title>RE: Extracting a Numeric Reference from a VARCHAR field</title><link>http://www.sqlservercentral.com/Forums/Topic605532-1401-1.aspx</link><description>Good one!</description><pubDate>Thu, 20 Nov 2008 12:50:02 GMT</pubDate><dc:creator>Jason Shadonix</dc:creator></item><item><title>RE: Extracting a Numeric Reference from a VARCHAR field</title><link>http://www.sqlservercentral.com/Forums/Topic605532-1401-1.aspx</link><description>I liked the question; I didn't think about it when I answered it much (so I got it wrong :().  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.</description><pubDate>Thu, 20 Nov 2008 10:09:49 GMT</pubDate><dc:creator>Mohit K. Gupta</dc:creator></item><item><title>RE: Extracting a Numeric Reference from a VARCHAR field</title><link>http://www.sqlservercentral.com/Forums/Topic605532-1401-1.aspx</link><description>JohnG -If you cast the -1 to Decimal(38,19), you won't get the error on 123.45Great question.</description><pubDate>Thu, 20 Nov 2008 07:40:38 GMT</pubDate><dc:creator>jarney</dc:creator></item><item><title>RE: Extracting a Numeric Reference from a VARCHAR field</title><link>http://www.sqlservercentral.com/Forums/Topic605532-1401-1.aspx</link><description>I see the exact same behavior with 2000.</description><pubDate>Thu, 20 Nov 2008 07:35:55 GMT</pubDate><dc:creator>stan rydzewski</dc:creator></item><item><title>RE: Extracting a Numeric Reference from a VARCHAR field</title><link>http://www.sqlservercentral.com/Forums/Topic605532-1401-1.aspx</link><description>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) [u]when it is part of a CASE statement[/u].  Error: [i]Conversion failed when converting the varchar value '123.45' to data type int.[/i]  Yet a direct assignment (IntRef = ExtRef) works fine!  (SQL Server 2005 SP2)</description><pubDate>Thu, 20 Nov 2008 07:19:11 GMT</pubDate><dc:creator>Mauve</dc:creator></item><item><title>RE: Extracting a Numeric Reference from a VARCHAR field</title><link>http://www.sqlservercentral.com/Forums/Topic605532-1401-1.aspx</link><description>Jim,Out of the box SQL 2005declare @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 @myTabset IntRef =  case when isnumeric(ExtRef) = 1 then cast(ExtRef as decimal(38,19))else cast(-1 as decimal(38,19)) --(A) endGood catch on the dollar sign!</description><pubDate>Thu, 20 Nov 2008 06:54:34 GMT</pubDate><dc:creator>Jamie Longstreet-481950</dc:creator></item><item><title>RE: Extracting a Numeric Reference from a VARCHAR field</title><link>http://www.sqlservercentral.com/Forums/Topic605532-1401-1.aspx</link><description>I'm using SQL Server 2008.To get the script to work for decimal numbers like 35.12, I had to changewhen isnumeric(ExtRef) = 1 then ExtRefto  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</description><pubDate>Thu, 20 Nov 2008 06:51:03 GMT</pubDate><dc:creator>jim.hamer</dc:creator></item><item><title>RE: Extracting a Numeric Reference from a VARCHAR field</title><link>http://www.sqlservercentral.com/Forums/Topic605532-1401-1.aspx</link><description>[quote]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 @myTabset IntRef =  case when isnumeric(ExtRef) = 1 then ExtRef else -1 --(A) endupdate @myTabset IntRef =  case when isnumeric(ExtRef) = 1 then ExtRef else -1 --(A) end[/quote]Confused here --(A) is a comment, yes?The cast to decimal would be required otherwise a failure converting varchar to decimal.</description><pubDate>Thu, 20 Nov 2008 06:46:18 GMT</pubDate><dc:creator>Jamie Longstreet-481950</dc:creator></item><item><title>Extracting a Numeric Reference from a VARCHAR field</title><link>http://www.sqlservercentral.com/Forums/Topic605532-1401-1.aspx</link><description>Comments posted to this topic are about the item [B]&lt;A HREF="/questions/T-SQL/64759/"&gt;Extracting a Numeric Reference from a VARCHAR field&lt;/A&gt;[/B]</description><pubDate>Wed, 19 Nov 2008 20:16:05 GMT</pubDate><dc:creator>brewmanz</dc:creator></item></channel></rss>