Using a comma-separated parameter in an IN clause

后端 未结 5 1979
余生分开走
余生分开走 2021-01-05 11:34

I have \'param1, param2, parma3\' coming from SSRS to a stored procedure as a varchar parameter: I need to use it in a query\'s IN cla

5条回答
  •  花落未央
    2021-01-05 12:11

    If you are using SQL 2016 and above string_split you can use.

    -- @param is where you keep your comma separated values example: 
    declare @param = 'param1,param2,param3'
    select * from table1 where col1 in (select TRIM(value) from string_split(@param,',')
    

    More information about string_split check offical documemt

    Furthermore, TRIM() is used to trim values from white spaces.

提交回复
热议问题