2

I have this:

declare @data varchar = 'UI'

I need to fix this so it'll work with a "IN" statement.

'UI' => 'U','I'

Having a hard time getting this done. Anyone care to help?

Thanks!

JJ.
  • 9,580
  • 37
  • 116
  • 189
  • No chance a `LIKE` would work? – James Hill Apr 27 '17 at 00:35
  • unfortunately no. has to be an IN statement – JJ. Apr 27 '17 at 00:39
  • No time to write this tonight, but this should get you going in the right direction: http://stackoverflow.com/questions/30782744/split-a-string-with-no-delimiters-into-columns – James Hill Apr 27 '17 at 00:48
  • If you need it to work with an `IN` statement, then ideally you want to create a custom function that returns a table of characters in a string. e.g. `SELECT a, b, c FROM myTable WHERE someVal IN (SELECT chars FROM dbo.fn_SplitStringSingleChars(@data));` (where dbo.fn_SplitStringSingleChars is the name of your custom function here). Otherwise, you'd be required to do some quite unnecessary dynamic SQL... – ZLK Apr 27 '17 at 00:59

3 Answers3

3

You can use an ad-hoc tally table in concert with Stuff() and XML.

Example

declare @data varchar(max) = 'UI'

Select S = Stuff((Select ',' +S
    From (
            Select S=''''+substring(@data,N,1)+''''
             From (Select Top (Len(@data)) N=Row_Number() Over (Order By (Select null)) From master..spt_values ) N
        ) B1
    For XML Path ('')),1,1,'') 

Returns

'U','I'

Edit If you are open to a UDF

Select S = Stuff((Select ',''' +RetVal + ''''
 From (Select * From [dbo].[udf-Str-Parse-Char](@data)) A 
 For XML Path ('')),1,1,'') 

The UDF

CREATE FUNCTION [dbo].[udf-Str-Parse-Char] (@String varchar(max))
Returns Table
As
Return (
    with   cte1(N) As (Select 1 From (Values(1),(1),(1),(1),(1),(1),(1),(1),(1),(1)) N(N)),
           cte2(N) As (Select Top (IsNull(DataLength(@String),0)) Row_Number() over (Order By (Select NULL)) From cte1 a,cte1 b,cte1 c,cte1 d,cte1 e,cte1 f)

    Select RetSeq=N
          ,RetVal=Substring(@String,N,1) 
     From  cte2
) 
--Max 1 Million Observations
--Select * from [dbo].[udf-Str-Parse-Char]('this is a string') 
John Cappelletti
  • 79,615
  • 7
  • 44
  • 66
0

Simple way:

declare @data varchar = 'UI'
select '''' + @data + ''''

to use this in IN Statement:

DECLARE @this_Command varchar(1000)
set @this_Command = 'select * from yourtable where column1 in(''' + @data + ''')'
exec(@this_Command);
Vijunav Vastivch
  • 4,153
  • 1
  • 16
  • 30
0

The answer from @JohnCappelletti will serve you well, but unless I'm missing something, I cant help but feel there are easier approaches available.

declare @data varchar = 'UI'

Select * from t
where charindex(field,@data)>0

This where clause will evaluate to true if field matches any of the characters in the @data parameter. There is an edge case which could cause issues: if field wasn't always a single character then charindex could give you a multi-character match which would have been avoided by @JohnCappelletti's answer. Should that be a relevant concern, you could do this:

Select * from t
where charindex(field,@data)>0 AND
len(field)=1

If you know @data won't contain any 'special' characters (such as ] or ^), then you can use like and would do it as follows:

Select * from t
where field like '[' + @data + ']'

The like will evaluate to true if field matches any of the characters in the @data parameter. If you wanted to do it this way and you do (or could) have special characters in there, you'd need to escape those characters first.

Given that, I'd normally opt for the charindex approach. like also tends to be less efficient than other functions, though others will know better than I whether it is in this case.

Steve Lovell
  • 2,564
  • 2
  • 13
  • 16
  • The issue with the `LIKE` one is you have to be sure that `@data` doesn't contain any surprises. E.g `@data = '^abc'` could cause some surprises (as could anything containing a `]`). – Damien_The_Unbeliever Apr 27 '17 at 07:07
  • Thanks @Damien_The_Unbeliever. I was going to include a note to that effect and forgot. I've updated my answer. – Steve Lovell Apr 27 '17 at 07:33