Login or Sign up Help | Site Map
Connecting Tech Pros Worldwide

SELECT and regular expression

Question posted by: Martin Lukasik (Guest) on April 24th, 2006 03:55 PM
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


Would you like to answer this question?
Sign up for a free account, or Login (if you're already a member).
Tony Rogerson's Avatar
Tony Rogerson
Guest
n/a Posts
April 24th, 2006
04:15 PM
#2

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's Avatar
Martin Lukasik
Guest
n/a Posts
April 24th, 2006
04:15 PM
#3

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's Avatar
markc600@hotmail.com
Guest
n/a Posts
April 24th, 2006
04:56 PM
#4

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's Avatar
Hugo Kornelis
Guest
n/a Posts
April 24th, 2006
08:25 PM
#5

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's Avatar
Tony Rogerson
Guest
n/a Posts
April 24th, 2006
08:55 PM
#6

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's Avatar
andyblum@gmail.com
Guest
n/a Posts
April 24th, 2006
09:15 PM
#7

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's Avatar
Erland Sommarskog
Guest
n/a Posts
April 24th, 2006
09:55 PM
#8

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, Join Bytes!

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pr...oads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodin...ions/books.mspx

Martin Lukasik's Avatar
Martin Lukasik
Guest
n/a Posts
April 26th, 2006
02:35 PM
#9

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's Avatar
LLik
Guest
n/a Posts
May 1st, 2006
01:15 PM
#10

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/archiv...6/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/...&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#'s Avatar
Mike C#
Guest
n/a Posts
May 18th, 2006
01:15 AM
#11

Re: SELECT and regular expression
http://www.sqlservercentral.com/col...oolkitpart2.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]



 
Not the answer you were looking for? Post your question . . .
184,215 Experts ready to help you find a solution.
Sign up for a free account, or Login (if you're already a member).

Latest Articles: Read & Comment
  • Didn't find the answer you were looking for?
    Post Your Question
  • Top Community Contributors