Connecting Tech Pros Worldwide Forums | Help | Site Map

Filtering records with question mark at the end

Newbie
 
Join Date: Sep 2006
Posts: 16
#1: Oct 27 '08
Can anyone help with filtering records ended with "?".

here is the code I have tried:

Expand|Select|Wrap|Line Numbers
  1.  strWhere = strWhere & "([stock_site] not like ""*" & "'?'"") AND "
  2.  strWhere = strWhere & "([goods_status] not Like ""*" & "scrap" & "*"") AND "
Not working at all. Any help is appreciated.

ADezii's Avatar
Expert
 
Join Date: Apr 2006
Location: Philadelphia
Posts: 5,218
#2: Oct 27 '08

re: Filtering records with question mark at the end


Quote:

Originally Posted by Jiwei06xie

Can anyone help with filtering records ended with "?".

here is the code I have tried:

Expand|Select|Wrap|Line Numbers
  1.  strWhere = strWhere & "([stock_site] not like ""*" & "'?'"") AND "
  2.  strWhere = strWhere & "([goods_status] not Like ""*" & "scrap" & "*"") AND "
Not working at all. Any help is appreciated.

Here is one of several Methods:
Expand|Select|Wrap|Line Numbers
  1. strWhere & "Where Not Right(TableName.[stock_site], 1) = '?' AND "
Moderator
 
Join Date: Feb 2008
Location: Beauly, near Inverness, Scotland
Posts: 1,576
#3: Oct 27 '08

re: Filtering records with question mark at the end


Hi, and Welcome to Bytes!

As you have found, the question mark character is a bit of a problem to use within a Where clause, as it is a wildcard character with a special meaning. There is a means to overcome it, however, using the InStr function, which returns the position of a character within the string if it exists, or 0 if it doesn't.

Your Where clause can be recast as

Expand|Select|Wrap|Line Numbers
  1. strWhere = "WHERE Instr([Stock_Site], " & Chr(63) & ") > 0"
(Chr(63) is just the questionmark character referred to by converting its ASCII code.)

This will match if the questionmark occurs anywhere in the string. If you need it to be the end of the string only - and I would caution that this would preclude strings matching if it is anywhere else - you could use

Expand|Select|Wrap|Line Numbers
  1. strWhere = "WHERE Instr([Stock_Site], " & Chr(63) & ") = Len([Stock_Site])"
-Stewart
Newbie
 
Join Date: Sep 2006
Posts: 16
#4: Oct 27 '08

re: Filtering records with question mark at the end


Thank you very much Stewart for your reply.

I will have a try later today.

Kindest regards, Jiwei

Quote:

Originally Posted by ADezii

Here is one of several Methods:

Expand|Select|Wrap|Line Numbers
  1. strWhere & "Where Not Right(TableName.[stock_site], 1) = '?' AND "

Newbie
 
Join Date: Sep 2006
Posts: 16
#5: Oct 27 '08

re: Filtering records with question mark at the end


It's a magic. Working perfect with my case.

Thank you very much Dezii.

Regards, Jiwei

Quote:

Originally Posted by ADezii

Here is one of several Methods:

Expand|Select|Wrap|Line Numbers
  1. strWhere & "Where Not Right(TableName.[stock_site], 1) = '?' AND "

ADezii's Avatar
Expert
 
Join Date: Apr 2006
Location: Philadelphia
Posts: 5,218
#6: Oct 27 '08

re: Filtering records with question mark at the end


Quote:

Originally Posted by Stewart Ross Inverness

Hi, and Welcome to Bytes!

As you have found, the question mark character is a bit of a problem to use within a Where clause, as it is a wildcard character with a special meaning. There is a means to overcome it, however, using the InStr function, which returns the position of a character within the string if it exists, or 0 if it doesn't.

Your Where clause can be recast as

Expand|Select|Wrap|Line Numbers
  1. strWhere = "WHERE Instr([Stock_Site], " & Chr(63) & ") > 0"
(Chr(63) is just the questionmark character referred to by converting its ASCII code.)

This will match if the questionmark occurs anywhere in the string. If you need it to be the end of the string only - and I would caution that this would preclude strings matching if it is anywhere else - you could use

Expand|Select|Wrap|Line Numbers
  1. strWhere = "WHERE Instr([Stock_Site], " & Chr(63) & ") = Len([Stock_Site])"
-Stewart

Hello Stewart, I think the OP was specifically looking for values where the '?' appeared at the end of the String. In this case the Instr() Function would not work where there were > 1 '?', the '?' did not appear as the last character, or the '?' appeared in a position other than the last. Kindly correct me if I am wrong.
ADezii's Avatar
Expert
 
Join Date: Apr 2006
Location: Philadelphia
Posts: 5,218
#7: Oct 27 '08

re: Filtering records with question mark at the end


Quote:

Originally Posted by Jiwei06xie

It's a magic. Working perfect with my case.

Thank you very much Dezii.

Regards, Jiwei

We do try to work magic whenever we can! (LOL).
Moderator
 
Join Date: Feb 2008
Location: Beauly, near Inverness, Scotland
Posts: 1,576
#8: Oct 27 '08

re: Filtering records with question mark at the end


Hi ADezii. The OP did indeed say that the question mark was at the end of the string, in which case using InStr and matching against Len (my second suggestion) would indeed fail if there is more than one questionmark (as InStr would return the position of the first occurrence only). I had pointed out that it would not work if the question mark was anywhere else, and it was implicit in my reply (but not spelt out clearly) that this would include use of more than one question mark.

Personally, I am not keen on use of user-entered marker characters that rely on the user placing the character in a specific position. Users are only human and will make mistakes, and such mistakes should not stop other routines from working.

For example, it is easy to type questionmark-space by mistake at the end of a string - and nearly impossible for the user concerned to spot. Even using RTrim would not guarantee a match, as the user could by mistake type something invalid that did not end in a space, like "?." - there are so many possibilities. A solution matching the last character will always fail in these circumstances, which is why my first suggestion - and it was first because it was the one I preferred - was to match for the questionmark anywhere in the string. This would not be dependent on whether or not there was just one question mark, or where it occurred. The OP has not said whether or not there is just one question mark, and whether or not end of text is the only place it could be, hence the provision of two solutions in my post...

-Stewart
ADezii's Avatar
Expert
 
Join Date: Apr 2006
Location: Philadelphia
Posts: 5,218
#9: Oct 28 '08

re: Filtering records with question mark at the end


Quote:

Originally Posted by Stewart Ross Inverness

Hi ADezii. The OP did indeed say that the question mark was at the end of the string, in which case using InStr and matching against Len (my second suggestion) would indeed fail if there is more than one questionmark (as InStr would return the position of the first occurrence only). I had pointed out that it would not work if the question mark was anywhere else, and it was implicit in my reply (but not spelt out clearly) that this would include use of more than one question mark.

Personally, I am not keen on use of user-entered marker characters that rely on the user placing the character in a specific position. Users are only human and will make mistakes, and such mistakes should not stop other routines from working.

For example, it is easy to type questionmark-space by mistake at the end of a string - and nearly impossible for the user concerned to spot. Even using RTrim would not guarantee a match, as the user could by mistake type something invalid that did not end in a space, like "?." - there are so many possibilities. A solution matching the last character will always fail in these circumstances, which is why my first suggestion - and it was first because it was the one I preferred - was to match for the questionmark anywhere in the string. This would not be dependent on whether or not there was just one question mark, or where it occurred. The OP has not said whether or not there is just one question mark, and whether or not end of text is the only place it could be, hence the provision of two solutions in my post...

-Stewart

Makes perfect sense to me Stewart, thanks for the explanation.
Reply


Similar Microsoft Access / VBA bytes