5 pts.
 SQL Scripting
Using SQL and given table X with columns labeled A, B, C, and D – how do you get a list of data in column C when column A and B are the same?

Software/Hardware used:
ASKED: March 4, 2008  6:20 PM
UPDATED: March 6, 2008  4:57 PM

Answer Wiki:
Like this. <pre>SELECT C FROM X WHERE A = B</pre>
Last Wiki Answer Submitted:  March 4, 2008  7:14 pm  by  Denny Cherry   64,550 pts.
All Answer Wiki Contributors:  Denny Cherry   64,550 pts.
To see all answers submitted to the Answer Wiki: View Answer History.


Discuss This Question:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _


 

Check out my SQL Server blog “SQL Server with Mr Denny” for more SQL Server information.

 64,550 pts.

 

If you consider a to be the same as b when they are both NULL, then the solution is a little more complicated. Here’s one way:

SELECT	c
FROM	x
WHERE	a = b
OR	(a IS NULL AND b IS NULL);
 1,240 pts.

 

Also, don’t the rules change if the item is a string ?

 5,320 pts.

 

You’ll to do something extra if your definition of “the same” does not correspond to the system’s definition of “=”. (The example of NULLs, above, is a special case of this.)

If a and b are strings, and you want to distinguish between capital and small letters, but the system doesn’t (that is, if considers capital ‘B’ to be equal to small ‘b’), then you have to convert the strings to some other type where the difference in case does matter to the system. In SQL Server, you might say:

SELECT	c
FROM	x
WHERE	CAST (a AS VARBINARY (10)) = 
	CAST (b AS VARBINARY (10));

There may be some system somewhere that considers (e.g.) ‘B’ stroed in a 10-character fixed-length column to be different that the same letter ‘B’ stored in a 20-character fixed-length column. In that case, you might have to explicitly convert the shorter one to the longer type, or do a separate test for length.

 1,240 pts.