Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
rox
Starting Member
4 Posts |
Posted - 2012-07-03 : 15:28:22
|
Hello I have a question. I have the following data: S1_98911111S2_98911111 9090_12345679091_1234567I have the following sql statement :SELECT test from tableWHERE (test LIKE '%_1234567'or test LIKE '%_11111') From this statement I get all the above data results. I want to get only 9090_1234567 and 9091_1234567. How can i check if the number is the same number after the underscore. So in this case I should not get the S1_98911111 because 11111 is not the exact numbers after the underscore.Please I need some help. Thanks in advance. |
|
sunitabeck
Master Smack Fu Yak Hacker
5155 Posts |
Posted - 2012-07-03 : 15:35:48
|
quote: Originally posted by rox Hello I have a question. I have the following data: S1_98911111S2_98911111 9090_12345679091_1234567I have the following sql statement :SELECT test from tableWHERE (test LIKE '%_1234567'or test LIKE '%_11111') From this statement I get all the above data results. I want to get only 9090_1234567 and 9091_1234567. How can i check if the number is the same number after the underscore. So in this case I should not get the S1_98911111 because 11111 is not the exact numbers after the underscore.Please I need some help. Thanks in advance.
I may be missing something in your question, but isn't it as simple as removing the OR and the part after that from the where clause?SELECT test from tableWHERE test LIKE '%[_]1234567' |
 |
|
sunitabeck
Master Smack Fu Yak Hacker
5155 Posts |
Posted - 2012-07-03 : 15:38:41
|
BTW, underscore is the wild character for any character, so if you really want to look for underscore, enclose that in square brackets as I have done in my previous reply.In other words, a where clause such as test LIKE '%_1234567' will match even this: 9091X1234567 |
 |
|
rox
Starting Member
4 Posts |
Posted - 2012-07-03 : 15:56:44
|
Hi sunitabeck, Thanks for your help. I resolved the problem. I had to enclose the underscore in square brackets |
 |
|
|
|
|