Link to home
Create AccountLog in
Avatar of usslindstrom
usslindstromFlag for Japan

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?
Avatar of p_nuts
p_nuts
Flag of Netherlands image

You can cast it as a varchar and do a regex on it.
ASKER CERTIFIED SOLUTION
Avatar of Thomasian
Thomasian
Flag of Philippines image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Avatar of usslindstrom

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" ?
SOLUTION
Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
Thank you VERY much for the solution and explination of how to implement it.

Much appreciated!