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

Filtering records with question mark at the end

P: 17
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.
Oct 27 '08 #1
Share this Question
Share on Google+
8 Replies


ADezii
Expert 5K+
P: 8,638
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 "
Oct 27 '08 #2

Expert Mod 2.5K+
P: 2,545
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
Oct 27 '08 #3

P: 17
Thank you very much Stewart for your reply.

I will have a try later today.

Kindest regards, Jiwei

Here is one of several Methods:
Expand|Select|Wrap|Line Numbers
  1. strWhere & "Where Not Right(TableName.[stock_site], 1) = '?' AND "
Oct 27 '08 #4

P: 17
It's a magic. Working perfect with my case.

Thank you very much Dezii.

Regards, Jiwei

Here is one of several Methods:
Expand|Select|Wrap|Line Numbers
  1. strWhere & "Where Not Right(TableName.[stock_site], 1) = '?' AND "
Oct 27 '08 #5

ADezii
Expert 5K+
P: 8,638
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.
Oct 27 '08 #6

ADezii
Expert 5K+
P: 8,638
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).
Oct 27 '08 #7

Expert Mod 2.5K+
P: 2,545
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
Oct 27 '08 #8

ADezii
Expert 5K+
P: 8,638
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.
Oct 27 '08 #9

Post your reply

Sign in to post your reply or Sign up for a free account.