usslindstrom
asked on
SQL wildcard question
Experts,
Is it possible to store a value in a SQL table that implies a wildcard on SELECT queries? - Backwards from a normal "LIKE" statement, where the wildcard is stored in the tables themselves?
I can best explain this with examples:
If the SQL table contains data "37" - I'm wanting to match, starting at the left of the string and work to the right.
IE. In the following statement: SELECT [ColumnName] FROM [TableName] WHERE [OtherColumn] = '375' - This wouldn't return any results that just had the value "37"
Is it possible to have a SELECT statement do this sort of logic?
Is it possible to store a value in a SQL table that implies a wildcard on SELECT queries? - Backwards from a normal "LIKE" statement, where the wildcard is stored in the tables themselves?
I can best explain this with examples:
If the SQL table contains data "37" - I'm wanting to match, starting at the left of the string and work to the right.
IE. In the following statement: SELECT [ColumnName] FROM [TableName] WHERE [OtherColumn] = '375' - This wouldn't return any results that just had the value "37"
Is it possible to have a SELECT statement do this sort of logic?
You can cast it as a varchar and do a regex on it.
http://www.simple-talk.com/sql/t-sql-programming/tsql-regular-expression-workbench/
That's some examples.
That's some examples.
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
Thanks for the suggestions guys.
P_Nuts, RegEx is just a bit beyond me at this point in the game. I understand the basics, like periods = a single random character, and question marks represent any number of characters after it's presented... But give me a while to understand what that excellent site you posted is trying to relay.
Thomasian, your example is great, but I'd like to try and understand it if you have a few seconds to explain it to me.
In your example, you're just tacking on the SQL wildcard at the very end. So, it would basically mean that the data in the original form would return "37%" - and then the SQL record would return the value, should I query it for anything that begins in 37?
Like:
SELECT [ColumnName] FROM [TableName] WHERE '375000001' LIKE [OtherColumn] + '%'
Would that also return the record in the table that's only "37" ?
P_Nuts, RegEx is just a bit beyond me at this point in the game. I understand the basics, like periods = a single random character, and question marks represent any number of characters after it's presented... But give me a while to understand what that excellent site you posted is trying to relay.
Thomasian, your example is great, but I'd like to try and understand it if you have a few seconds to explain it to me.
In your example, you're just tacking on the SQL wildcard at the very end. So, it would basically mean that the data in the original form would return "37%" - and then the SQL record would return the value, should I query it for anything that begins in 37?
Like:
SELECT [ColumnName] FROM [TableName] WHERE '375000001' LIKE [OtherColumn] + '%'
Would that also return the record in the table that's only "37" ?
SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
Thank you VERY much for the solution and explination of how to implement it.
Much appreciated!
Much appreciated!