Connecting Tech Pros Worldwide Help | Site Map

SELECT and regular expression

Martin Lukasik
Guest
 
Posts: n/a
#1: Apr 24 '06
Hi,

I'm not a big friend of MSSQL, but I have to do one query I've done for
mySQL.
But I don't know how...

I have to select 'user' from 'db' where first letter is E or N, second is B
or 0 and after that there are 6 or 7 digits I know.
How can I do that?

In mySQL it would be something like:

SELECT * FROM `table` WHERE `account` regexp '^[EN][B0]123456$' ORDER BY
`Id`;


Thanks in advance,
Martin


Tony Rogerson
Guest
 
Posts: n/a
#2: Apr 24 '06

re: SELECT and regular expression


where ( account like 'EB%
or account like 'NB%' )
and ( account like 'E0%
or account like 'N0%' )
and len( account ) between 6 and 7

--
Tony Rogerson
SQL Server MVP
http://sqlserverfaq.com - free video tutorials


"Martin Lukasik" <marcin@milea.pl.i.hate.this.spam> wrote in message
news:68f2$444cf0e4$c1263429$24018@ZOO.CO.UK...[color=blue]
> Hi,
>
> I'm not a big friend of MSSQL, but I have to do one query I've done for
> mySQL.
> But I don't know how...
>
> I have to select 'user' from 'db' where first letter is E or N, second is
> B or 0 and after that there are 6 or 7 digits I know.
> How can I do that?
>
> In mySQL it would be something like:
>
> SELECT * FROM `table` WHERE `account` regexp '^[EN][B0]123456$' ORDER BY
> `Id`;
>
>
> Thanks in advance,
> Martin
>
>[/color]


Martin Lukasik
Guest
 
Posts: n/a
#3: Apr 24 '06

re: SELECT and regular expression



"Tony Rogerson" <tonyrogerson@sqlserverfaq.com> wrote in message
news:e2isig$2qt$1$8300dec7@news.demon.co.uk...[color=blue]
> where ( account like 'EB%
> or account like 'NB%' )
> and ( account like 'E0%
> or account like 'N0%' )
> and len( account ) between 6 and 7[/color]

Thank you,

Is there any easier way?
What if first characters are A, D, C, 4, X, Z, Y? It's not really nice to
type every permutation.
There must be some regular expression...

Martin


markc600@hotmail.com
Guest
 
Posts: n/a
#4: Apr 24 '06

re: SELECT and regular expression


where account like '[EN][BO][0-9][0-9][0-9][0-9][0-9][0-9]'
or account like '[EN][BO][0-9][0-9][0-9][0-9][0-9][0-9][0-9]'

Hugo Kornelis
Guest
 
Posts: n/a
#5: Apr 24 '06

re: SELECT and regular expression


On Mon, 24 Apr 2006 16:37:44 +0100, Martin Lukasik wrote:
[color=blue]
>Hi,
>
>I'm not a big friend of MSSQL, but I have to do one query I've done for
>mySQL.
>But I don't know how...[/color]
(snip)[color=blue]
>In mySQL it would be something like:
>
>SELECT * FROM `table` WHERE `account` regexp '^[EN][B0]123456$' ORDER BY
>`Id`;[/color]

Hi Martin,

Try

SELECT * -- Use column list instead!!
FROM table
WHERE account LIKE '[EN][B0]123456%'
ORDER BY Id

--
Hugo Kornelis, SQL Server MVP
Tony Rogerson
Guest
 
Posts: n/a
#6: Apr 24 '06

re: SELECT and regular expression


Hang on Martin, you didn't ask for that.

Whats your actual requirement?

You can use true regular expressions by using CLR function written in say C#
/ VB.NET.

LEFT( account, 1 ) IN ( .... )

The LIKE is more performant because it can better use an index.

--
Tony Rogerson
SQL Server MVP
http://sqlserverfaq.com - free video tutorials


"Martin Lukasik" <marcin@milea.pl.i.hate.this.spam> wrote in message
news:26bee$444cf727$c1263429$26427@ZOO.CO.UK...[color=blue]
>
> "Tony Rogerson" <tonyrogerson@sqlserverfaq.com> wrote in message
> news:e2isig$2qt$1$8300dec7@news.demon.co.uk...[color=green]
>> where ( account like 'EB%
>> or account like 'NB%' )
>> and ( account like 'E0%
>> or account like 'N0%' )
>> and len( account ) between 6 and 7[/color]
>
> Thank you,
>
> Is there any easier way?
> What if first characters are A, D, C, 4, X, Z, Y? It's not really nice to
> type every permutation.
> There must be some regular expression...
>
> Martin
>
>[/color]


andyblum@gmail.com
Guest
 
Posts: n/a
#7: Apr 24 '06

re: SELECT and regular expression


Check out the following link from the code project.

http://www.codeproject.com/managedcpp/xpregex.asp

It provides C++ external functions for SQL Server 2000 that I assume is
usable from 2005 as well. Unlike some onelse who said that LIKE is
faster it is not, this flies!

Andy Blum

Erland Sommarskog
Guest
 
Posts: n/a
#8: Apr 24 '06

re: SELECT and regular expression


(andyblum@gmail.com) writes:[color=blue]
> Check out the following link from the code project.
>
> http://www.codeproject.com/managedcpp/xpregex.asp
>
> It provides C++ external functions for SQL Server 2000 that I assume is
> usable from 2005 as well. Unlike some onelse who said that LIKE is
> faster it is not, this flies![/color]

Yes, but in SQL 2005 you call functions written in a CLR language,
where you can use the Regexp classes in the .Net Framework. This is a
lot more effecient than calling extended stored procedures.


--
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Martin Lukasik
Guest
 
Posts: n/a
#9: Apr 26 '06

re: SELECT and regular expression


> Hi Martin,[color=blue]
>
> Try
>
> SELECT * -- Use column list instead!!
> FROM table
> WHERE account LIKE '[EN][B0]123456%'
> ORDER BY Id[/color]

That's exactly what I was looking for.
Thank you!

m.


LLik
Guest
 
Posts: n/a
#10: May 1 '06

re: SELECT and regular expression


There are a few good blogs about reg ex and ms-sql 2005 with CLR such
as http://blogs.msdn.com/sqlclr/archive.../29/regex.aspx

Also microsoft also provides the code to do it in c# and vb.net in the
sql server 2005 samples download
http://www.microsoft.com/downloads/d...displaylang=en

Hope that is the correct one. look in the find example and they have
all the code the a regex parser.

Mike C#
Guest
 
Posts: n/a
#11: May 18 '06

re: SELECT and regular expression


http://www.sqlservercentral.com/colu...olkitpart2.asp

"Martin Lukasik" <marcin@milea.pl.i.hate.this.spam> wrote in message
news:68f2$444cf0e4$c1263429$24018@ZOO.CO.UK...[color=blue]
> Hi,
>
> I'm not a big friend of MSSQL, but I have to do one query I've done for
> mySQL.
> But I don't know how...
>
> I have to select 'user' from 'db' where first letter is E or N, second is
> B or 0 and after that there are 6 or 7 digits I know.
> How can I do that?
>
> In mySQL it would be something like:
>
> SELECT * FROM `table` WHERE `account` regexp '^[EN][B0]123456$' ORDER BY
> `Id`;
>
>
> Thanks in advance,
> Martin
>
>[/color]


Closed Thread