Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Server - Compare int values in a select clause

Tags:

sql-server

This is what I thought would be a simple select clause, however the following is giving me grief! I am using SQL Server 2008.

Basically I want to compare two integer values and return the boolean result in the select clause. Here is a simple example:

DECLARE @A INT
DECLARE @B INT
SET @A = 1
SET @B = 2

SELECT @A = @B

Currently the only output is "Command(s) completed successfully." Where I reasonably believe it is assigning @A to @B.

I thought this would be simple but have not been able to achieve this.

Any help would be great! Thanks

like image 276
Russell Avatar asked Mar 25 '10 22:03

Russell


People also ask

How do I compare two numeric values in SQL?

SQL Equal to ( = ) operator The equal to operator is used for equality test within two numbers or expressions.

How use greater than and less than in SQL query?

In SQL, you can use the >= operator to test for an expression greater than or equal to. Let's use the same customers table as the previous example. In this example, the SELECT statement would return all rows from the customers table where the customer_id is greater than or equal to 6000.

What is == in SQL?

== (Equal) (SSIS Expression)

Which SQL logical operator is used to compare a value with a null value?

The NULL operator is used to compare a value with a NULL value.


2 Answers

try

SELECT CASE WHEN @A = @B THEN 1 ELSE 0 END

instead

like image 174
Manu Avatar answered Sep 28 '22 12:09

Manu


Select Case When @A = @B Then 1 Else 0 End
like image 29
Thomas Avatar answered Sep 28 '22 14:09

Thomas