Main Page | Report this Page
Computers Forum Index  »  Computer - Databases - Sybase  »  Pattern Matching...
Page 1 of 1    

Pattern Matching...

Author Message
Subind...
Posted: Tue Jan 06, 2009 7:54 pm
Guest
Hi,
How to match a string containing only numbers? The below method
doesn't works.
Please help.

declare at (no spam) s varchar(30)
select at (no spam) s="123"

if( at (no spam) s like '[0-9]')
print "This string contains only numbers"
else
print "This string contains special characters"



Thanks,
 
Leonid Gvirtz...
Posted: Wed Jan 07, 2009 9:38 am
Guest
On Jan 6, 9:54 pm, Subind <subind... at (no spam) gmail.com> wrote:
Quote:
Hi,
How to match a string containing only numbers? The below method
doesn't works.
Please help.

declare at (no spam) s varchar(30)
select at (no spam) s="123"

if( at (no spam) 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 at (no spam) s varchar(30)
select at (no spam) s="123"
if( at (no spam) 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
 
Carl Kayser...
Posted: Wed Jan 07, 2009 5:08 pm
Guest
"Subind" <subind123 at (no spam) gmail.com> wrote in message
news:41680f6c-c15a-4d43-89f5-ea5fc7d21ef6 at (no spam) f11g2000vbf.googlegroups.com...
Quote:
Hi,
How to match a string containing only numbers? The below method
doesn't works.
Please help.

declare at (no spam) s varchar(30)
select at (no spam) s="123"

if( at (no spam) 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 ( at (no spam) s) like '[0-9][0-9][0-9]')

If you are on 15.0.1 or later use

if (isnumeric ( at (no spam) 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").
 
Carl Kayser...
Posted: Wed Jan 07, 2009 6:39 pm
Guest
"Carl Kayser" <kayser_c at (no spam) bls.gov> wrote in message
news:gk22h1$cri$1 at (no spam) blsnews.bls.gov...
Quote:

"Subind" <subind123 at (no spam) gmail.com> wrote in message
news:41680f6c-c15a-4d43-89f5-ea5fc7d21ef6 at (no spam) f11g2000vbf.googlegroups.com...
Hi,
How to match a string containing only numbers? The below method
doesn't works.
Please help.

declare at (no spam) s varchar(30)
select at (no spam) s="123"

if( at (no spam) 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 ( at (no spam) s) like '[0-9][0-9][0-9]')

If you are on 15.0.1 or later use

if (isnumeric ( at (no spam) 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 at (no spam) i smallint,
at (no spam) special tinyint

set at (no spam) i = 1,
at (no spam) special = 0

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

if ( at (no spam) special = 1)
begin
print "This string contains special characters"
end
else
begin
print "This string contains only numbers"
end
 
Bret_Halford...
Posted: Thu Jan 08, 2009 9:30 pm
Guest
On Jan 6, 12:54 pm, Subind <subind... at (no spam) gmail.com> wrote:
Quote:
Hi,
How to match a string containing only numbers? The below method
doesn't works.
Please help.

declare at (no spam) s varchar(30)
select at (no spam) s="123"

if( at (no spam) 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
 
ThanksButNo...
Posted: Thu Jan 08, 2009 10:55 pm
Guest
On Jan 8, 1:30 pm, Bret_Halford <b... at (no spam) sybase.com> wrote:
Quote:
On Jan 6, 12:54 pm, Subind <subind... at (no spam) gmail.com> wrote:

Hi,
How to match a string containing only numbers? The below method
doesn't works.
Please help.

declare at (no spam) s varchar(30)
select at (no spam) s="123"

if( at (no spam) 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.

\:-\
 
 
Page 1 of 1    
All times are GMT
The time now is Mon Mar 22, 2010 10:19 am