Connecting Tech Pros Worldwide Help | Site Map

SELECT and regular expression

  #1  
Old April 24th, 2006, 04:55 PM
Martin Lukasik
Guest
 
Posts: 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


  #2  
Old April 24th, 2006, 05:15 PM
Tony Rogerson
Guest
 
Posts: n/a

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]


  #3  
Old April 24th, 2006, 05:15 PM
Martin Lukasik
Guest
 
Posts: n/a

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


  #4  
Old April 24th, 2006, 05:56 PM
markc600@hotmail.com
Guest
 
Posts: n/a

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]'

  #5  
Old April 24th, 2006, 09:25 PM
Hugo Kornelis
Guest
 
Posts: n/a

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

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]


  #7  
Old April 24th, 2006, 10:15 PM
andyblum@gmail.com
Guest
 
Posts: n/a

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

  #8  
Old April 24th, 2006, 10:55 PM
Erland Sommarskog
Guest
 
Posts: n/a

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
  #9  
Old April 26th, 2006, 03:35 PM
Martin Lukasik
Guest
 
Posts: n/a

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.


  #10  
Old May 1st, 2006, 02:15 PM
LLik
Guest
 
Posts: n/a

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.

  #11  
Old May 18th, 2006, 02:15 AM
Mike C#
Guest
 
Posts: n/a

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
regular expression issue othellomy@yahoo.com answers 1 November 8th, 2006 01:45 PM
regular expression question Ludwig answers 6 March 27th, 2006 08:25 AM
filter string over regular expression Xavier answers 4 November 19th, 2005 09:04 PM
Regular Expression - Grouping Question IKAS answers 2 November 15th, 2005 05:50 AM
Problem with a Regular Expression in C. Need Help! Mike Andrews answers 2 November 13th, 2005 10:57 PM