I have a query like:
SELECT ...
FROM ...
WHERE .. AND SomeID = @someID
I’m inheriting this massive stored procedure that has some dynamic elements to it, so I need to get the value of someID from a column in another table i.e. perform a lookup.
How can I do that?
The other column looks like:
TypeValue
'SomeID=1232,OtherID=3383'
Some the column TypeValue is VARCHAR(1000), and it contains a comma seperated string of type & value pairs.
So I need to split on the , and then split on the = so somehow get:
SET @SomeID = SELECT TypeValue FROM MyTABLE WHERE ..
How can I do this?
(note, this is sql server 2000)
Assuming you’re stuck with bad design, this doesn’t require dynamic SQL:
Results:
If you know which row you want:
Result:
This doesn’t guard against someone putting a string into the SomeID value, of course, so may be safer to only convert to an INT if it is proven to be numeric. But you get the point.