Please start any new threads on our new site at We've got lots of great SQL Server experts to answer whatever question you can come up with.

Our new SQL Server Forums are live! Come on over! We've restricted the ability to create new threads on these forums.

SQL Server Forums
Profile | Active Topics | Members | Search | Forum FAQ
Register Now and get your question answered!
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 CONVERT (INT, myfield) or CAST (myfield AS INT)?
 Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Starting Member

18 Posts

Posted - 08/04/2014 :  14:58:22  Show Profile  Reply with Quote
Neither are working.

I have this function:

Create Function dbo.GetNumberic(@ItemDesc VarChar(8000))
Returns VarChar(8000)
    Return Left(
             SubString(@ItemDesc, PatIndex('%[0-9]%', @ItemDesc), 8000), 
             PatIndex('%[^0-9]%', SubString(@ItemDesc, PatIndex('%[0-9]%', @ItemDesc), 8000))'X'-1)

and it extracts the numbers out of the alphanumeric field BUT the field is still a string and i need to match the numeric values to another table (membership number) and that's a numeric field.

So I tried the CONVERT (INT, dbo.GetNumeric(ItemDesc)) but it is not working. Nor is the CAST (dbo.GetNumeric(ItemDesc) AS INT).

Can you please help.

Edited by - techsupport14 on 08/04/2014 15:00:11

Almighty SQL Goddess

38200 Posts

Posted - 08/04/2014 :  15:02:32  Show Profile  Visit tkizer's Homepage  Reply with Quote

it is not working

Could you elaborate? Is it throwing a error? If so, please post it. Is it not showing the correct result? If so, please show us what it is doing and what it should be doing.

Tara Kizer
SQL Server MVP since 2007
Go to Top of Page

Constraint Violating Yak Guru

252 Posts

Posted - 08/04/2014 :  21:25:49  Show Profile  Reply with Quote
It might be the function you are using. That function has an error in it.

Line PatIndex('%[^0-9]%', SubString(@ItemDesc, PatIndex('%[0-9]%', @ItemDesc), 8000))'X'-1)

The 'X'-1)

. it will also only find the first numeric sequence in a string. but if that is all you need, you're good. Could just be the error.

You might try something like the following in the function body:

DECLARE @AlphaNUmeric Varchar(25) = 'XDHDH123hh$4ll'
DECLARE @I int = 1
DECLARE @Return varchar(100) = ''

WHILE @I < LEN(@AlphaNUmeric)
SELECT @Return = @Return + CASE WHEN ASCII(SUBSTRING(@AlphaNUmeric,@I,1)) BETWEEN 48 and 57 THEN SUBSTRING(@AlphaNUmeric,@I,1) ELSE '' END
SET @I = @I + 1


Go to Top of Page

Starting Member

18 Posts

Posted - 08/05/2014 :  00:03:01  Show Profile  Reply with Quote
Thank you. I figured it out :)
Go to Top of Page

Aged Yak Warrior

550 Posts

Posted - 08/05/2014 :  13:03:44  Show Profile  Reply with Quote
Yeah, pretty clear the code meant to be this:

PatIndex('%[^0-9]%', SubString(@ItemDesc, PatIndex('%[0-9]%', @ItemDesc), 8000)+'X')-1)
Go to Top of Page
  Previous Topic Topic Next Topic  
 Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.07 seconds. Powered By: Snitz Forums 2000