Re: Pattern Matching
- From: Leonid Gvirtz <lgvirtz@xxxxxxxxx>
- Date: Wed, 7 Jan 2009 01:38:53 -0800 (PST)
On Jan 6, 9:54 pm, Subind <subind...@xxxxxxxxx> wrote:
Hi,
How to match a string containing only numbers? The below method
doesn't works.
Please help.
declare @s varchar(30)
select @s="123"
if(@s like '[0-9]')
print "This string contains only numbers"
else
print "This string contains special characters"
Thanks,
Hi
I think it is easier to check for presence of unwanted characters in
the string, for example:
declare @s varchar(30)
select @s="123"
if(@s not like '%[A-z]%')
print "This string contains only numbers"
else
print "This string contains special characters"
go
You may want to add additional unwanted characters to the list. In
addition, you may just try to convert the string to int with convert
function, but I found it difficult to handle the possible error
message properly in T-SQL.
Hope it helps
Leonid Gvirtz
.
- References:
- Pattern Matching
- From: Subind
- Pattern Matching
- Prev by Date: Pattern Matching
- Next by Date: Re: Pattern Matching
- Previous by thread: Pattern Matching
- Next by thread: Re: Pattern Matching
- Index(es):
Relevant Pages
|