SQL Scripting

5 pts.
Tags:
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?
ASKED: March 4, 2008  6:20 PM
UPDATED: March 6, 2008  4:57 PM

Answer Wiki

Thanks. We'll let you know when a new response is added.

Like this.
<pre>SELECT C
FROM X
WHERE A = B</pre>

Discuss This Question: 4  Replies

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
  • Denny Cherry
    Check out my SQL Server blog "SQL Server with Mr Denny" for more SQL Server information.
    65,660 pointsBadges:
    report
  • FrankKulash
    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 pointsBadges:
    report
  • Jerry Lees
    Also, don't the rules change if the item is a string ?
    5,335 pointsBadges:
    report
  • FrankKulash
    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 pointsBadges:
    report

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

To follow this tag...

There was an error processing your information. Please try again later.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Thanks! We'll email you when relevant content is added and updated.

Following