dbTalk Databases Forums  

Pattern Matching

comp.databases.sybase comp.databases.sybase


Discuss Pattern Matching in the comp.databases.sybase forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Subind
 
Posts: n/a

Default Pattern Matching - 01-06-2009 , 01:54 PM






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,

Reply With Quote
  #2  
Old   
Leonid Gvirtz
 
Posts: n/a

Default Re: Pattern Matching - 01-07-2009 , 03:38 AM






On Jan 6, 9:54*pm, Subind <subind... (AT) gmail (DOT) com> wrote:
Quote:
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


Reply With Quote
  #3  
Old   
Carl Kayser
 
Posts: n/a

Default Re: Pattern Matching - 01-07-2009 , 05:08 AM




"Subind" <subind123 (AT) gmail (DOT) com> wrote

Quote:
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,

For your particular example ("123") it could be

if (@s) like '[0-9][0-9][0-9]')

If you are on 15.0.1 or later use

if (isnumeric (@s) = 1)

At the moment I can't think of a more general solution (since you use a
varchar (30) and I assume that the arguments can be anything from NULL up to
30 characters "long").




Reply With Quote
  #4  
Old   
Carl Kayser
 
Posts: n/a

Default Re: Pattern Matching - 01-07-2009 , 06:39 AM




"Carl Kayser" <kayser_c (AT) bls (DOT) gov> wrote

Quote:
"Subind" <subind123 (AT) gmail (DOT) com> wrote in message
news:41680f6c-c15a-4d43-89f5-ea5fc7d21ef6 (AT) f11g2000vbf (DOT) googlegroups.com...
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,


For your particular example ("123") it could be

if (@s) like '[0-9][0-9][0-9]')

If you are on 15.0.1 or later use

if (isnumeric (@s) = 1)

At the moment I can't think of a more general solution (since you use a
varchar (30) and I assume that the arguments can be anything from NULL up
to 30 characters "long").

A pre-15.0.1 brute force solution:

declare @i smallint,
@special tinyint

set @i = 1,
@special = 0

while (@i < = 255)
begin
if (@i < 48 or @i > 57)
begin
if (char_index (char (@i), @s) > 0)
begin
set @i = 255,
@special = 1
end
end
set @i = @i + 1
end

if (@special = 1)
begin
print "This string contains special characters"
end
else
begin
print "This string contains only numbers"
end




Reply With Quote
  #5  
Old   
Bret_Halford
 
Posts: n/a

Default Re: Pattern Matching - 01-08-2009 , 03:30 PM



On Jan 6, 12:54*pm, Subind <subind... (AT) gmail (DOT) com> wrote:
Quote:
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,

Do you mean "numbers" or "numeric digits"?

i.e., in terms of your problem, are any of the following strings
"numbers"?

1.0
-1
2.3e58
-2.3e-65
INF
NAN
-0.0
one thousand, two hundred and fifty-four
cinco
ichi
0xff


Reply With Quote
  #6  
Old   
ThanksButNo
 
Posts: n/a

Default Re: Pattern Matching - 01-08-2009 , 04:55 PM



On Jan 8, 1:30 pm, Bret_Halford <b... (AT) sybase (DOT) com> wrote:
Quote:
On Jan 6, 12:54 pm, Subind <subind... (AT) gmail (DOT) com> 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,

Do you mean "numbers" or "numeric digits"?

i.e., in terms of your problem, are any of the following strings
"numbers"?

1.0
-1
2.3e58
-2.3e-65
INF
NAN
-0.0
one thousand, two hundred and fifty-four
cinco
ichi
0xff
Regarding "cinco" -- That's not a number.

Eso es un número.

\:-\


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.