Split sql parameter on comma

前端 未结 4 775
春和景丽
春和景丽 2021-01-14 20:04

Below code works for \'Main Stream\' or \'Premium\' as parameter, however I am trying to make it work for both of them as you can see below, but it doesn\'t return any resul

4条回答
  •  抹茶落季
    2021-01-14 20:55

    You could try something like

    --Split
    DECLARE @textXML XML
    DECLARE @data NVARCHAR(MAX), 
            @delimiter NVARCHAR(5)
            
    SELECT  @data = 'Main Stream , Premium',
            @delimiter = ','
            
    SELECT    @textXML = CAST('' + REPLACE(@data, @delimiter, '') + '' AS XML)
    SELECT  T.split.value('.', 'nvarchar(max)') AS data
    FROM    @textXML.nodes('/d') T(split)
    

    You could either store this in a temp table, or use it in the IN clause.

    For @Hoy comment

    You could look at nodes() Method (xml Data Type)

    The nodes() method is useful when you want to shred an xml data type instance into relational data. It allows you to identify nodes that will be mapped into a new row.

    Also, have a look at xml Data Type Methods

    You could then use it as

    select * 
    FROM    sales  
    where   myCategory IN   (
                                SELECT  T.split.value('.', 'nvarchar(max)')
                                FROM    @textXML.nodes('/d') T(split)
                            )
    

提交回复
热议问题