Sqlserver
 sql >> Database >  >> RDS >> Sqlserver

Perché SELECT DISTINCT a, b FROM... restituisce meno record di SELECT DISTINCT A + '|' + B DA...?

Gli spazi finali potrebbero causare questo. Per i confronti di stringhe queste vengono ignorate.

CREATE TABLE #T
(
a varchar(10),
b varchar(10),
c varchar(10)
)

INSERT INTO #T
SELECT 'a ' as a, 'b' as b, 'c ' as c union all
SELECT 'a' as a, 'b' as b, 'c ' as c

SELECT DISTINCT a, b, c  
FROM #T /*1 result*/

SELECT DISTINCT a + '|' + b + '|' + c + '|'   
FROM #T /*2 results*/


SELECT DISTINCT LTRIM(RTRIM(a)) + '|' + LTRIM(RTRIM(b)) + '|' +
                LTRIM(RTRIM(c)) + '|'   
FROM #T /*1 result*/