Home All Groups Group Topic Archive Search About

select based on containing only certain characters

Author
30 Jun 2006 9:46 PM
JR
I need to select rows in a table where a particular column contains
only the following characters and not in any particular order.  "AZ{<"
So "A", "Z", "{", and "<" without the quotes.  If that's all there is
in the data for a column, select it.

Example:

Positive hit if data in column was: AZ{<ZAZZZZZZZAAAAAAAAAA
Negative hit if data in column was: AZ{<ZAZZZZZZZAAAAAAAAAAG
^^^ second one has a G at the end.

Thanks.

JR

Author
30 Jun 2006 9:57 PM
Stu
Your performance may not be great, but you could do it like this:

DECLARE @t TABLE (c1 varchar(50))

INSERT INTO @t
VALUES ('AZ{<ZAZZZZZZZAAAAAAAAAA')

INSERT INTO @t
VALUES ('AZ{<ZAZZZZZZZAAAAAAAAAAG')

SELECT c1
FROM @t
WHERE c1 NOT LIKE '%[^AZ<{]%'

HTH,
Stu


JR wrote:
Show quote
> I need to select rows in a table where a particular column contains
> only the following characters and not in any particular order.  "AZ{<"
> So "A", "Z", "{", and "<" without the quotes.  If that's all there is
> in the data for a column, select it.
>
> Example:
>
> Positive hit if data in column was: AZ{<ZAZZZZZZZAAAAAAAAAA
> Negative hit if data in column was: AZ{<ZAZZZZZZZAAAAAAAAAAG
> ^^^ second one has a G at the end.
>
> Thanks.
>
> JR
Author
30 Jun 2006 10:23 PM
JR
How about if I had a | (pipe) or > (greater than sign.  These do not
seem to be returning anything.  So if I do something like

DECLARE @t TABLE (c1 varchar(50))

INSERT INTO @t
VALUES ('AZ|>ZAZZZZZZZAAAAAAAAAA')

INSERT INTO @t
VALUES ('AZ|>ZAZZZZZZZAAAAAAAAAAG')

INSERT INTO @t
VALUES ('AZ')

SELECT c1
FROM @t
WHERE c1 NOT LIKE '%[^AZ>|]%'

All I get is the last AZ value.  Not the first two.

Stu wrote:
Show quote
> Your performance may not be great, but you could do it like this:
>
> DECLARE @t TABLE (c1 varchar(50))
>
> INSERT INTO @t
> VALUES ('AZ{<ZAZZZZZZZAAAAAAAAAA')
>
> INSERT INTO @t
> VALUES ('AZ{<ZAZZZZZZZAAAAAAAAAAG')
>
> SELECT c1
> FROM @t
> WHERE c1 NOT LIKE '%[^AZ<{]%'
>
> HTH,
> Stu
>
>
> JR wrote:
> > I need to select rows in a table where a particular column contains
> > only the following characters and not in any particular order.  "AZ{<"
> > So "A", "Z", "{", and "<" without the quotes.  If that's all there is
> > in the data for a column, select it.
> >
> > Example:
> >
> > Positive hit if data in column was: AZ{<ZAZZZZZZZAAAAAAAAAA
> > Negative hit if data in column was: AZ{<ZAZZZZZZZAAAAAAAAAAG
> > ^^^ second one has a G at the end.
> >
> > Thanks.
> >
> > JR
Author
30 Jun 2006 10:26 PM
JR
I mean not the first one.
JR wrote:
Show quote
> How about if I had a | (pipe) or > (greater than sign.  These do not
> seem to be returning anything.  So if I do something like
>
> DECLARE @t TABLE (c1 varchar(50))
>
> INSERT INTO @t
> VALUES ('AZ|>ZAZZZZZZZAAAAAAAAAA')
>
> INSERT INTO @t
> VALUES ('AZ|>ZAZZZZZZZAAAAAAAAAAG')
>
> INSERT INTO @t
> VALUES ('AZ')
>
> SELECT c1
> FROM @t
> WHERE c1 NOT LIKE '%[^AZ>|]%'
>
> All I get is the last AZ value.  Not the first two.
>
> Stu wrote:
> > Your performance may not be great, but you could do it like this:
> >
> > DECLARE @t TABLE (c1 varchar(50))
> >
> > INSERT INTO @t
> > VALUES ('AZ{<ZAZZZZZZZAAAAAAAAAA')
> >
> > INSERT INTO @t
> > VALUES ('AZ{<ZAZZZZZZZAAAAAAAAAAG')
> >
> > SELECT c1
> > FROM @t
> > WHERE c1 NOT LIKE '%[^AZ<{]%'
> >
> > HTH,
> > Stu
> >
> >
> > JR wrote:
> > > I need to select rows in a table where a particular column contains
> > > only the following characters and not in any particular order.  "AZ{<"
> > > So "A", "Z", "{", and "<" without the quotes.  If that's all there is
> > > in the data for a column, select it.
> > >
> > > Example:
> > >
> > > Positive hit if data in column was: AZ{<ZAZZZZZZZAAAAAAAAAA
> > > Negative hit if data in column was: AZ{<ZAZZZZZZZAAAAAAAAAAG
> > > ^^^ second one has a G at the end.
> > >
> > > Thanks.
> > >
> > > JR
Author
1 Jul 2006 3:11 AM
Stu
Hmm.  I'm running it on a SQL 2005 setup, and it worked fine for me; I
got two rows returned; the exception was the one with the G.

As far as I know, neither the < or the | simple should have any meaning
in a wildcard scenario.

Can you verify your results again?

Stu

JR wrote:
Show quote
> I mean not the first one.
> JR wrote:
> > How about if I had a | (pipe) or > (greater than sign.  These do not
> > seem to be returning anything.  So if I do something like
> >
> > DECLARE @t TABLE (c1 varchar(50))
> >
> > INSERT INTO @t
> > VALUES ('AZ|>ZAZZZZZZZAAAAAAAAAA')
> >
> > INSERT INTO @t
> > VALUES ('AZ|>ZAZZZZZZZAAAAAAAAAAG')
> >
> > INSERT INTO @t
> > VALUES ('AZ')
> >
> > SELECT c1
> > FROM @t
> > WHERE c1 NOT LIKE '%[^AZ>|]%'
> >
> > All I get is the last AZ value.  Not the first two.
> >
> > Stu wrote:
> > > Your performance may not be great, but you could do it like this:
> > >
> > > DECLARE @t TABLE (c1 varchar(50))
> > >
> > > INSERT INTO @t
> > > VALUES ('AZ{<ZAZZZZZZZAAAAAAAAAA')
> > >
> > > INSERT INTO @t
> > > VALUES ('AZ{<ZAZZZZZZZAAAAAAAAAAG')
> > >
> > > SELECT c1
> > > FROM @t
> > > WHERE c1 NOT LIKE '%[^AZ<{]%'
> > >
> > > HTH,
> > > Stu
> > >
> > >
> > > JR wrote:
> > > > I need to select rows in a table where a particular column contains
> > > > only the following characters and not in any particular order.  "AZ{<"
> > > > So "A", "Z", "{", and "<" without the quotes.  If that's all there is
> > > > in the data for a column, select it.
> > > >
> > > > Example:
> > > >
> > > > Positive hit if data in column was: AZ{<ZAZZZZZZZAAAAAAAAAA
> > > > Negative hit if data in column was: AZ{<ZAZZZZZZZAAAAAAAAAAG
> > > > ^^^ second one has a G at the end.
> > > >
> > > > Thanks.
> > > >
> > > > JR
Author
2 Jul 2006 10:21 PM
JR
OK, tested again and I'm getting the first and last value.  However I
shouldn't get the second value as it doesn't contain the pipe or
greater than characters, just "AZ".  I'm not sure, however when I test
this with fields of type text only the last one is returned.


Stu wrote:
Show quote
> Hmm.  I'm running it on a SQL 2005 setup, and it worked fine for me; I
> got two rows returned; the exception was the one with the G.
>
> As far as I know, neither the < or the | simple should have any meaning
> in a wildcard scenario.
>
> Can you verify your results again?
>
> Stu
>
> JR wrote:
> > I mean not the first one.
> > JR wrote:
> > > How about if I had a | (pipe) or > (greater than sign.  These do not
> > > seem to be returning anything.  So if I do something like
> > >
> > > DECLARE @t TABLE (c1 varchar(50))
> > >
> > > INSERT INTO @t
> > > VALUES ('AZ|>ZAZZZZZZZAAAAAAAAAA')
> > >
> > > INSERT INTO @t
> > > VALUES ('AZ|>ZAZZZZZZZAAAAAAAAAAG')
> > >
> > > INSERT INTO @t
> > > VALUES ('AZ')
> > >
> > > SELECT c1
> > > FROM @t
> > > WHERE c1 NOT LIKE '%[^AZ>|]%'
> > >
> > > All I get is the last AZ value.  Not the first two.
> > >
> > > Stu wrote:
> > > > Your performance may not be great, but you could do it like this:
> > > >
> > > > DECLARE @t TABLE (c1 varchar(50))
> > > >
> > > > INSERT INTO @t
> > > > VALUES ('AZ{<ZAZZZZZZZAAAAAAAAAA')
> > > >
> > > > INSERT INTO @t
> > > > VALUES ('AZ{<ZAZZZZZZZAAAAAAAAAAG')
> > > >
> > > > SELECT c1
> > > > FROM @t
> > > > WHERE c1 NOT LIKE '%[^AZ<{]%'
> > > >
> > > > HTH,
> > > > Stu
> > > >
> > > >
> > > > JR wrote:
> > > > > I need to select rows in a table where a particular column contains
> > > > > only the following characters and not in any particular order.  "AZ{<"
> > > > > So "A", "Z", "{", and "<" without the quotes.  If that's all there is
> > > > > in the data for a column, select it.
> > > > >
> > > > > Example:
> > > > >
> > > > > Positive hit if data in column was: AZ{<ZAZZZZZZZAAAAAAAAAA
> > > > > Negative hit if data in column was: AZ{<ZAZZZZZZZAAAAAAAAAAG
> > > > > ^^^ second one has a G at the end.
> > > > >
> > > > > Thanks.
> > > > >
> > > > > JR
Author
3 Jul 2006 2:49 AM
Stu
Hey JR,

Can you post either actual data definition language scripts OR a sample
that illustrates the problem (ie, a column of type text with sample
data)? I think I understand what you are describing, but it would be
nice to make sure we're on the same page.

Stu


JR wrote:
Show quote
> OK, tested again and I'm getting the first and last value.  However I
> shouldn't get the second value as it doesn't contain the pipe or
> greater than characters, just "AZ".  I'm not sure, however when I test
> this with fields of type text only the last one is returned.
>
>
> Stu wrote:
> > Hmm.  I'm running it on a SQL 2005 setup, and it worked fine for me; I
> > got two rows returned; the exception was the one with the G.
> >
> > As far as I know, neither the < or the | simple should have any meaning
> > in a wildcard scenario.
> >
> > Can you verify your results again?
> >
> > Stu
> >
> > JR wrote:
> > > I mean not the first one.
> > > JR wrote:
> > > > How about if I had a | (pipe) or > (greater than sign.  These do not
> > > > seem to be returning anything.  So if I do something like
> > > >
> > > > DECLARE @t TABLE (c1 varchar(50))
> > > >
> > > > INSERT INTO @t
> > > > VALUES ('AZ|>ZAZZZZZZZAAAAAAAAAA')
> > > >
> > > > INSERT INTO @t
> > > > VALUES ('AZ|>ZAZZZZZZZAAAAAAAAAAG')
> > > >
> > > > INSERT INTO @t
> > > > VALUES ('AZ')
> > > >
> > > > SELECT c1
> > > > FROM @t
> > > > WHERE c1 NOT LIKE '%[^AZ>|]%'
> > > >
> > > > All I get is the last AZ value.  Not the first two.
> > > >
> > > > Stu wrote:
> > > > > Your performance may not be great, but you could do it like this:
> > > > >
> > > > > DECLARE @t TABLE (c1 varchar(50))
> > > > >
> > > > > INSERT INTO @t
> > > > > VALUES ('AZ{<ZAZZZZZZZAAAAAAAAAA')
> > > > >
> > > > > INSERT INTO @t
> > > > > VALUES ('AZ{<ZAZZZZZZZAAAAAAAAAAG')
> > > > >
> > > > > SELECT c1
> > > > > FROM @t
> > > > > WHERE c1 NOT LIKE '%[^AZ<{]%'
> > > > >
> > > > > HTH,
> > > > > Stu
> > > > >
> > > > >
> > > > > JR wrote:
> > > > > > I need to select rows in a table where a particular column contains
> > > > > > only the following characters and not in any particular order.  "AZ{<"
> > > > > > So "A", "Z", "{", and "<" without the quotes.  If that's all there is
> > > > > > in the data for a column, select it.
> > > > > >
> > > > > > Example:
> > > > > >
> > > > > > Positive hit if data in column was: AZ{<ZAZZZZZZZAAAAAAAAAA
> > > > > > Negative hit if data in column was: AZ{<ZAZZZZZZZAAAAAAAAAAG
> > > > > > ^^^ second one has a G at the end.
> > > > > >
> > > > > > Thanks.
> > > > > >
> > > > > > JR
Author
1 Jul 2006 12:24 AM
--CELKO--
>> I need to select rows in a table where a particular column contains only thfollowing characters and not in any particular order.  "AZ{<" <<

SELECT word
  FROM Foobar
WHERE REPLACE
               (REPLACE
                  (REPLACE
                    (REPLACE (word, 'A', ''),
                  'Z', ''),
                '{', ''),
              '<', '')  = '';

Note that spaces count as characters.
Author
2 Jul 2006 10:14 PM
JR
--CELKO-- wrote:
> >> I need to select rows in a table where a particular column contains only thfollowing characters and not in any particular order.  "AZ{<" <<
>
> SELECT word
>   FROM Foobar
> WHERE REPLACE
>                (REPLACE
>                   (REPLACE
>                     (REPLACE (word, 'A', ''),
>                   'Z', ''),
>                 '{', ''),
>               '<', '')  = '';
>
> Note that spaces count as characters.

Thanks for the reply.  I should have mentioned that the field in
question here is of type "text" so SQL Server seems unhappy with this.
Probably the equals clause or something.

AddThis Social Bookmark Button