Select distinct values from multiple columns in same table

前端 未结 4 1627
[愿得一人]
[愿得一人] 2021-01-01 10:54

I am trying to construct a single SQL statement that returns unique, non-null values from multiple columns all located in the same table.

 SELECT distinct tb         


        
4条回答
  •  感情败类
    2021-01-01 11:32

    Try this if you have more than two Columns:

    CREATE TABLE #temptable (Name1 VARCHAR(25),Name2 VARCHAR(25))
    
    INSERT INTO #temptable(Name1, Name2)
      VALUES('JON', 'Harry'), ('JON', 'JON'), ('Sam','harry')
    
    SELECT t.Name1+','+t.Name2 Names  INTO #t FROM #temptable AS tSELECT DISTINCT ss.value FROM #t AS t
      CROSS APPLY STRING_SPLIT(T.Names,',') AS ss
    

提交回复
热议问题