By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,767 Members | 1,302 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,767 IT Pros & Developers. It's quick & easy.

SELECT and regular expression

P: n/a
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
Apr 24 '06 #1
Share this Question
Share on Google+
10 Replies


P: n/a
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" <ma****@milea.pl.i.hate.this.spam> wrote in message
news:68**************************@ZOO.CO.UK...
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

Apr 24 '06 #2

P: n/a

"Tony Rogerson" <to**********@sqlserverfaq.com> wrote in message
news:e2*******************@news.demon.co.uk...
where ( account like 'EB%
or account like 'NB%' )
and ( account like 'E0%
or account like 'N0%' )
and len( account ) between 6 and 7


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
Apr 24 '06 #3

P: n/a
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]'

Apr 24 '06 #4

P: n/a
On Mon, 24 Apr 2006 16:37:44 +0100, Martin Lukasik wrote:
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... (snip)In mySQL it would be something like:

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


Hi Martin,

Try

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

--
Hugo Kornelis, SQL Server MVP
Apr 24 '06 #5

P: n/a
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" <ma****@milea.pl.i.hate.this.spam> wrote in message
news:26***************************@ZOO.CO.UK...

"Tony Rogerson" <to**********@sqlserverfaq.com> wrote in message
news:e2*******************@news.demon.co.uk...
where ( account like 'EB%
or account like 'NB%' )
and ( account like 'E0%
or account like 'N0%' )
and len( account ) between 6 and 7


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

Apr 24 '06 #6

P: n/a
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

Apr 24 '06 #7

P: n/a
(an******@gmail.com) writes:
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!


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, es****@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
Apr 24 '06 #8

P: n/a
> Hi Martin,

Try

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


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

m.
Apr 26 '06 #9

P: n/a
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.

May 1 '06 #10

P: n/a
http://www.sqlservercentral.com/colu...olkitpart2.asp

"Martin Lukasik" <ma****@milea.pl.i.hate.this.spam> wrote in message
news:68**************************@ZOO.CO.UK...
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

May 18 '06 #11

This discussion thread is closed

Replies have been disabled for this discussion.