-1

I am passing a keyword to my SP say @Keyword. This keyword may include

param1:value1 param2:value2

param1:value1 

param2:value2

How can I can grab value1 and value2?

Imran Qadir Baksh - Baloch
  • 32,612
  • 68
  • 179
  • 322

4 Answers4

3

You can use .nodes for xml type and string functions like LEFT / CHARINDEX / SUBSTRING to achieve this

Query

DECLARE @v VARCHAR(MAX) = 'param1:value1 param2:value2'

DECLARE @xml xml = '<x>' + REPLACE(@v,' ','</x><x>') + '</x>'
;WITH CTE AS 
(
SELECT c.value('.','NVARCHAR(MAX)') as val
FROM @xml.nodes('x') t(c)
)
SELECT
LEFT(val,CHARINDEX(':',val)-1),
SUBSTRING(val,CHARINDEX(':',val)+1,LEN(val)-CHARINDEX(':',val))
FROM cte

If possible, you should use static parameters. if not I would suggest you can use Table valued parameters or xml directly as a parameter.

Note: The above code assumes that <x>,</x> and ' ' space are not valid param or value.

Here is another way,

            ALTER FUNCTION [dbo].[GetValueByKey]
            (
                @Key nvarchar(max)
                ,@Str nvarchar(max)
            )
            RETURNS NVARCHAR(MAX)
            AS
            BEGIN

                DECLARE @Result nvarchar(max) = '';
                DECLARE @KeyIndex int = 0; 
                DECLARE @SpaceIndex int = 0; 

                SELECT @KeyIndex = CHARINDEX(@Key + ':', @Str);

                IF(@KeyIndex > 0)
                BEGIN
                    SET @KeyIndex = @KeyIndex + LEN(@Key) + 1;
                    SET @Result = SUBSTRING(@Str, @KeyIndex, LEN(@Str) - @KeyIndex + 1);
                    SELECT @SpaceIndex = CHARINDEX(' ', @Result);
                    IF(@SpaceIndex <= 1)
                    BEGIN
                        SET @SpaceIndex = LEN(@Result)  +1;
                    END
                    SELECT @Result = SUBSTRING(@Result, 0, @SpaceIndex)
                END

                RETURN @Result;
            END
Imran Qadir Baksh - Baloch
  • 32,612
  • 68
  • 179
  • 322
ughai
  • 9,830
  • 3
  • 29
  • 47
2

How about just putting param1 and param2 as parameters to your stored procedure?

CREATE PROCEDURE procedurename
    @param1   datatype
  , @param2   datatype
AS
  content here
GO
Toby
  • 66
  • 2
2

Here is another way using a split function. See this article by Jeff Moden for reference.

Basically, you want to split your @keyword using ' ' (space) as the delimiter. Then using some string functions such as LEFT, SUBSTRING, and CHARINDEX, you can extract the param and its value.

DECLARE @keyword VARCHAR(8000)
SELECT @keyword = 'param1:value1 param2:value2'

;WITH CteSpace AS(
    SELECT *
    FROM dbo.DelimitedSplit8K(@keyword, ' ')
)
SELECT
    Param = LEFT(Item, CHARINDEX(':', Item) - 1),
    Value = SUBSTRING(Item, CHARINDEX(':', Item) +1, LEN(Item) - CHARINDEX(':', Item))
FROM CteSpace
Felix Pamittan
  • 31,544
  • 7
  • 41
  • 67
1

Don't use a delimited string, use a tabled value parameter instead.
Create a user defined table type that contains 2 columns: paramName and value:

CREATE TYPE Keywords AS Table
(
    Keyword_ParamName varchar(10), -- or whatever length that suits your needs
    Keyword_value varchar(200), -- or whatever length that suits your needs
)

Then simply declare the @keyword parameter as this type:

CREATE PROCEDURE stp_doWhatever 
(
    @Keyword dbo.Keywords READONLY -- Note: Readonly is a must!
)
AS
-- do whatever

You can use the @keyword as a table in the stored procedure to perform selects, joins, whatever you need.

Zohar Peled
  • 79,642
  • 10
  • 69
  • 121