SELECT and regular expression

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Martin Lukasik

    #1

    SELECT and regular expression

    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

    #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.p l.i.hate.this.s pam> wrote in message
    news:68f2$444cf 0e4$c1263429$24 018@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]


    Comment

    • Martin Lukasik

      #3
      Re: SELECT and regular expression


      "Tony Rogerson" <tonyrogerson@s qlserverfaq.com > wrote in message
      news:e2isig$2qt $1$8300dec7@new s.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


      Comment

      • markc600@hotmail.com

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

        Comment

        • Hugo Kornelis

          #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

          Comment

          • Tony Rogerson

            #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.p l.i.hate.this.s pam> wrote in message
            news:26bee$444c f727$c1263429$2 6427@ZOO.CO.UK. ..[color=blue]
            >
            > "Tony Rogerson" <tonyrogerson@s qlserverfaq.com > wrote in message
            > news:e2isig$2qt $1$8300dec7@new s.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]


            Comment

            • andyblum@gmail.com

              #7
              Re: SELECT and regular expression

              Check out the following link from the code project.



              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

              Comment

              • Erland Sommarskog

                #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, esquel@sommarsk og.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

                Comment

                • Martin Lukasik

                  #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.


                  Comment

                  • LLik

                    #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/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.

                    Comment

                    • Mike C#

                      #11
                      Re: SELECT and regular expression

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

                      "Martin Lukasik" <marcin@milea.p l.i.hate.this.s pam> wrote in message
                      news:68f2$444cf 0e4$c1263429$24 018@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]


                      Comment

                      Working...