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

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Using Substring with a Where Clause
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

xxMarkDxx
Starting Member

United Kingdom
3 Posts

Posted - 04/13/2012 :  04:36:30  Show Profile  Reply with Quote
Hi i hope someone can help i am new to using T-SQL so i will apoligise if this is a basic question

I have the following piece of code that i run to update a field in a table

Use BchTelephony
Update dbo.BchInboundData
Set BchCode = Substring (Name,5,3)

what i am trying to do is only ren the update if the substring returns a number , is there a way off doing this without having to add a where clause that will have to say Not 'A' Not 'B' tc etc

Any help witll be gratefully aprreciated

sunitabeck
Flowing Fount of Yak Knowledge

5152 Posts

Posted - 04/13/2012 :  05:43:37  Show Profile  Reply with Quote
You can add a where clause. If it is all positive integers (i.e., you want to look for only digits 0 through 9), then you can do this:
WHERE
   SUBSTRING(Name,5,3) NOT LIKE '%[^0-9]%'
Or if you have negative numbers also use '%[^0-9-]%', decimal points, then'%[^0-9-.]%' etc.

Another way is to use the isnumeric function.
WHERE
   ISNUMERIC(SUBSTRING(Name,5,3)) = 1
Go to Top of Page

xxMarkDxx
Starting Member

United Kingdom
3 Posts

Posted - 04/13/2012 :  06:37:24  Show Profile  Reply with Quote
Thanks sunitabeck that is great
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
48076 Posts

Posted - 04/13/2012 :  11:37:09  Show Profile  Reply with Quote
quote:
Originally posted by sunitabeck

You can add a where clause. If it is all positive integers (i.e., you want to look for only digits 0 through 9), then you can do this:
WHERE
   SUBSTRING(Name,5,3) NOT LIKE '%[^0-9]%'
Or if you have negative numbers also use '%[^0-9-]%', decimal points, then'%[^0-9-.]%' etc.

Another way is to use the isnumeric function.
WHERE
   ISNUMERIC(SUBSTRING(Name,5,3)) = 1




ISNUMERIC is not fully reliable

see

http://beyondrelational.com/modules/2/blogs/70/posts/10803/enhanced-isnumeric-function.aspx

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

sunitabeck
Flowing Fount of Yak Knowledge

5152 Posts

Posted - 04/13/2012 :  11:48:52  Show Profile  Reply with Quote
quote:
ISNUMERIC is not fully reliable
I would prefer to call it GENEROUS rather than unreliable. Everything that returns a 1 for ISNUMERIC (such as $,-,1e0,2d2,space character etc.) can be converted to at least one numeric type. So SQL Server is being generous/liberal in considering what is numeric.
SELECT CAST('$' AS MONEY);
SELECT CAST('-' AS INT);
SELECT CAST('1d0' AS FLOAT);
SELECT CAST (' ' AS INT);
SELECT CAST('2e2' AS FLOAT);
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
48076 Posts

Posted - 04/13/2012 :  11:53:53  Show Profile  Reply with Quote
quote:
Originally posted by sunitabeck

quote:
ISNUMERIC is not fully reliable
I would prefer to call it GENEROUS rather than unreliable. Everything that returns a 1 for ISNUMERIC (such as $,-,1e0,2d2,space character etc.) can be converted to at least one numeric type. So SQL Server is being generous/liberal in considering what is numeric.
SELECT CAST('$' AS MONEY);
SELECT CAST('-' AS INT);
SELECT CAST('1d0' AS FLOAT);
SELECT CAST (' ' AS INT);
SELECT CAST('2e2' AS FLOAT);



I meant its not fully reliable when you're using it for stringent numeric checks like in this scenraio

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
  Previous Topic Topic Next Topic  
 New 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.08 seconds. Powered By: Snitz Forums 2000