I am a new with T-SQL. So, please help me to write the sql.
I have table Price (Code column is primary column):
Code Value
A1 234
A2 525
A3 566
I will input a string and the sql need to return a table.
Ex1: input ‘A2’ -> return:
Code Value
A2 525
Ex2: input ‘A1 A3’ -> return:
Code Value
A1 234
A3 566
Ex3: input ‘A1 A3 A1’ -> return:
Code Value
A1 234
A3 566
Ex4: input ‘A1 A4’ -> return:
Code Value
A1 234
Please help me. I am using SQL Server 2005. Tks.
It’s very effective, but it has two limitations:
You can’t use regular SQL parameters in an
INclause, so you’ll have append it to your SQL string automatically, which, in some cases, could open SQL Injections.It’s not exactly the input format you requsted: instead of
A2 A2it’s'A1', 'A2'.Good luck anyway!
EDIT: If you really want to use the
A1 A2format, you can’t useINand you’d have to split the string and then check if it contains the current[Price].Code. Just note that it will be much less effective than my first example.T-SQL doesn’t support
Splitby default, so you’ll have to add it manually:And then, you could do something like this:
Here’s the source for the Split function.