How can I select from list of values in SQL Server

后端 未结 14 1907
隐瞒了意图╮
隐瞒了意图╮ 2020-11-28 18:08

I have very simple problem that I can\'t solve. I need to do something like this:

select distinct * from (1, 1, 1, 2, 5, 1, 6).

Anybody can

14条回答
  •  北荒
    北荒 (楼主)
    2020-11-28 18:43

    I know this is a pretty old thread, but I was searching for something similar and came up with this.

    Given that you had a comma-separated string, you could use string_split

    select distinct value from string_split('1, 1, 1, 2, 5, 1, 6',',')
    

    This should return

    1
    2
    5
    6
    

    String split takes two parameters, the string input, and the separator character.

    you can add an optional where statement using value as the column name

    select distinct value from string_split('1, 1, 1, 2, 5, 1, 6',',')
    where value > 1
    

    produces

    2
    5
    6
    

提交回复
热议问题