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


Problems with IsNumeric and PatIndex when trying to convert string to Int/BigInt


Problems with IsNumeric and PatIndex when trying to convert string to Int/BigInt

Author
Message
tammyf
tammyf
Grasshopper
Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)

Group: General Forum Members
Points: 19 Visits: 71
Can someone help me understand why this is happening:

If I put the following in a where clause:

CASE WHEN PATINDEX('%[^0-9 ]%',h.oe_po_no) = 0
THEN Cast(RTrim(LTrim(h.oe_po_no)) as BigInt)
ELSE 0
END = 0

I get the following error:

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

I have seen the same situation when using IsNumeric instead of PATINDEX. Why doesn't T-SQL truly recognize numeric strings and allow them to be converted? This is so frustrating.

Thank you.

Tammy
Mansfield
Mansfield
Old Hand
Old Hand (370 reputation)Old Hand (370 reputation)Old Hand (370 reputation)Old Hand (370 reputation)Old Hand (370 reputation)Old Hand (370 reputation)Old Hand (370 reputation)Old Hand (370 reputation)

Group: General Forum Members
Points: 370 Visits: 394
Can you please provide the CREATE TABLE statement of the h table you are referring to, as well as some sample data?

Edit: A small test to get you started anyway. It's probably because of the extra space in your pattern. Remove the '123 456' row from the @test table and see the difference...


declare @test table (po_number nvarchar(50))

insert into @test (po_number)
values ('123 456'),('not a po'),('123456'),('123-456'),('not a po'),('-123456')

select
CASE WHEN PATINDEX('%[^0-9 ]%',po_number) = 0
THEN Cast(RTrim(LTrim(po_number)) as BigInt)
ELSE 0
END
from @test


mickyT
mickyT
Ten Centuries
Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)

Group: General Forum Members
Points: 1253 Visits: 3309
I suspect it may be a space within the number, for example this will cause the error

SELECT CAST('123 456' AS BIGINT)



If you want to compress this situation into a single number then you could do

CASE WHEN PATINDEX('%[^0-9]%',REPLACE(h.oe_po_no,' ','')) = 0
THEN Cast(REPLACE(h.oe_po_no,' ','') as BigInt)
ELSE 0
END = 0


Otherwise to throw a zero
CASE   WHEN PATINDEX('%[^0-9]%',RTRIM(LTRIM(h.oe_po_no))) = 0 
THEN Cast(RTRIM(LTRIM(h.oe_po_no)) as BigInt)
ELSE 0
END = 0


Lynn Pettis
Lynn Pettis
SSC-Insane
SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)

Group: General Forum Members
Points: 24207 Visits: 37978
tammyf (4/11/2013)
Can someone help me understand why this is happening:

If I put the following in a where clause:

CASE WHEN PATINDEX('%[^0-9 ]%',h.oe_po_no) = 0
THEN Cast(RTrim(LTrim(h.oe_po_no)) as BigInt)
ELSE 0
END = 0

I get the following error:

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

I have seen the same situation when using IsNumeric instead of PATINDEX. Why doesn't T-SQL truly recognize numeric strings and allow them to be converted? This is so frustrating.

Thank you.

Tammy


The problem is the space in this: [^0-9 ]. It needs to be this: [^0-9].

Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
tammyf
tammyf
Grasshopper
Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)

Group: General Forum Members
Points: 19 Visits: 71
If there is a way to award points or give credit, I don't know what it is, but all three of you helped me with this. I believe it is probably a space within the string of numbers. When I added the Replace of a space with an empty string, it worked.

Thank you.

If there is a way to mark your answers as the answer, please let me know
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