Saturday, 15 June 2013

sql server 2008 r2 - SQL queries producing unexpected results -



sql server 2008 r2 - SQL queries producing unexpected results -

i've got unusual situation 2 sql queries aren't producing expected results. here queries:

query 1:

select distinct somecharvalue table1 bring together table2 on table1.somecharvalue = table2.somecharvalue order somecharvalue

query 2:

select distinct somecharvalue table1 bring together table2 on table1.somecharvalue <> table2.somecharvalue order somecharvalue

i have 2 tables columns of varchar(15). table2 little subset of values in table1, table1 has values stored in table2. problem is, 2 queries should never produce same results, yet do. both queries produce same result values; example, if table1 , table2 contain word 'hello', query 1 should homecoming it, while query 2 should not. however, both queries homecoming 'hello'. doesn't create sense 'hello' in both tables equal , not equal @ same time. ran length query test values, , different size trailing white spaces, after changing these exact match, , verifying hexadecimal value of characters same, same results occur. can't compare numeric key fields since there no key relationship between these tables. can compare exact character values in columns. ideas?

imagine have table1 containing a , b separate rows, , table2 has exact same contents.

now sec query, table1's row a compared both rows in table2. pass on clause when comparing row b in table2, , hence a in result set. b row in table1 pass on clause when compared a row in table2.

you rewrite query as

select distinct somecharvalue table1 somecharvalue not in (select distinct somecharvalue table2) order somecharvalue

sql sql-server-2008-r2

No comments:

Post a Comment