Dave
Dave

Reputation:

Boolean expression in SQL

I have two ints: @RecurremceTemp and @WeekDifference

The following line errors:

PRINT @WeekDifference >= @RecurrenceTemp

with Incorrect syntax near '>'

And won't execute.

Can someone please tell me how to write such a boolean expression to include in a Select statement i.e.:

Select * 
FROM TableX 
WHERE somevariable = x 
and @WeekDifference >= @RecurrenceTemp

Upvotes: 2

Views: 1246

Answers (3)

Tommy Carlier
Tommy Carlier

Reputation: 8149

Predicates cannot be used in expressions, only in IF and WHILE and CASE WHEN:

PRINT CASE WHEN @WeekDifference >= @RecurrenceTemp THEN 1 ELSE 0 END

Upvotes: 2

Andrew Hare
Andrew Hare

Reputation: 351456

Your query ought to work just fine.

The reason that the print statement fails is due to the fact that SQL Server treats the results of a boolean expressions very differently than other data types. See Operators:

Unlike other SQL Server data types, a Boolean data type cannot be specified as the data type of a table column or variable, and cannot be returned in a result set.

Upvotes: 0

Russ Cam
Russ Cam

Reputation: 125488

Return -1, 0, or 1 from a CASE statement, depending on the comparison. In SQL Sevrer, there is no boolean data type. The nearest data type is BIT, which can have a value of 1, 0 or null.

EDIT:

Your SQL statement

Select * 
FROM TableX 
WHERE somevariable = x 
and @WeekDifference >= @RecurrenceTemp

will select all fields from TableX where somevariable = x and the WeekDifference variable value is greater than or equal to the RecurrenceTemp variable value. Is that not what you want?

Upvotes: 0

Related Questions