5
Select 
    CAST(de.ornum AS numeric) + 1 as ornum2 
from Cpaym as de
left outer join Cpaym as de1 on CAST(de.ornum AS numeric) = de1.ornum
where ISNUMERIC(de.ornum) = 1 

I'm trying to get the missing sequence number, but I am getting an error:

Msg 8114, Level 16, State 5, Line 1 Error converting data type varchar to numeric.

Dale K
  • 25,246
  • 15
  • 42
  • 71
Jesun Bicar
  • 103
  • 1
  • 4
  • 11
  • Please post your schema (table structure, field definitions). Have a look at the answers to this [SO post](http://stackoverflow.com/questions/338075/cast-and-isnumeric). – PM 77-1 Sep 26 '13 at 02:37
  • sorry but i cant..... the table is already filled... a thousand of records..and its datatype is in varchar... so changing the datatype is out of the options... :D – Jesun Bicar Sep 26 '13 at 02:49
  • @JesunBicar: changing data type is never out of the options, unless someone tells you you don't have time, or there is some 3rd party relying on the data being a certain type. Even if you have to keep it varchar, you can still clean up the data so it's all numeric. – siride Sep 26 '13 at 03:18
  • yeah.. i dont have the authority to change the datatype.... and it was a human error when someone..to put some characters on a sequence number... -_- – Jesun Bicar Sep 26 '13 at 03:24
  • @JesunBicar: you can still clean up the data to get rid of those characters. I would seriously look into that. A database won't be very effective if the data is junk (garbage-in, garbage-out). – siride Sep 26 '13 at 13:22

1 Answers1

4

You have a CAST() in your join predicate, which is evaluated BEFORE the WHERE clause. If de.ornum is not numeric, then this cast will fail.

Also, IsNumeric() doesn't catch all numeric types, but it's perhaps good enough in most cases. Here's the documentation: http://technet.microsoft.com/en-us/library/ms186272(v=sql.105).aspx and here is one (of many) articles describing the problems with IsNumeric(): http://classicasp.aspfaq.com/general/what-is-wrong-with-isnumeric.html.

There are a few things you can do:

  • Fix your schema so that a column named ornum actually contains a number in all tables that have it.
  • Use a CASE expression in your join predicate (slow, but it'll work): ... ON CASE WHEN ISNUMERIC(de.ornum) = 1 THEN CAST(de.ornum AS numeric) ELSE NULL END = de1.ornum
  • Use a derived table to pre-convert before the join (also slow, but perhaps less slow -- check the execution plan)

Code:

FROM (
  SELECT de.ornum
    FROM Cpaym AS de
   WHERE IsNumeric(de.ornum) = 1
) AS de
LEFT OUTER JOIN ...
siride
  • 200,666
  • 4
  • 41
  • 62
  • * cant change the column schema cause it got some remarks of characters which i dont know why.... - i've been trying to check each cast.. the only problem is the 2nd cast.. which i dont know how to solve it... 1st cast got no problem.. – Jesun Bicar Sep 26 '13 at 02:42
  • 2
    Did you actually read the answer? Two of the three offered solutions do not require schema changes. It also clearly explains why the second CAST (inside JOIN expression) gives you a problem. – PM 77-1 Sep 26 '13 at 02:52
  • @JesunBicar: (1) consider filtering out those characters and then changing the schema, or adding a new column that's just numbers which you can use for joins and indexes and such. (2) My answer is about the 2nd cast. That cast is in the join predicate. See my first paragraph for why this doesn't work. – siride Sep 26 '13 at 02:57
  • yeah...ive read why the 2nd cast doesnt work..do got any suggestion on that..cause one of the data has "13213."...so it give the error... when i try to remove all the characters ...it give me data overflow... – Jesun Bicar Sep 26 '13 at 03:10
  • 1
    @JesunBicar: my second bullet and third bullet points give possible solutions. But seriously, try to fix your data. The fact that you have non-numeric data in an ostensibly numeric column is going to cause problem after problem. You can pay off the technical debt now, or pay it off later, with interest. – siride Sep 26 '13 at 03:17
  • yeah.. i know.. bout the second bullet.. i really got no idea on sql case.. still new to sql scripts..well i know the basics.. can you help me with the case statement? – Jesun Bicar Sep 26 '13 at 03:28
  • @JesunBicar: just use the CASE...END expression just as I have it. You can copy and paste. I would do some reading on CASE expressions. They are like IF statements in other languages. Here's one article from Google: http://sqlmag.com/t-sql/t-sql-101-case-function – siride Sep 26 '13 at 03:48
  • @siride the real mvp right here. This worked like a charm for me. – nulltron Sep 24 '21 at 23:06