Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Server - boolean literal?

How to write literal boolean value in SQL Server? See sample use:

select * from SomeTable where PSEUDO_TRUE 

another sample:

if PSEUDO_TRUE begin   select 'Hello, SQL!' end  

Note: The query above has nothing to do with how I'm going to use it. It is just to test the literal boolean.

like image 424
dpp Avatar asked Aug 24 '11 05:08

dpp


People also ask

Is there boolean in SQL Server?

There is no boolean data type in SQL Server. However, a common option is to use the BIT data type. A BIT data type is used to store bit values from 1 to 64. So, a BIT field can be used for booleans, providing 1 for TRUE and 0 for FALSE.

Which is the correct Boolean literal?

A Boolean literal is a character-string delimited on the left by the separator B" and on the right by the quotation mark separator. The character-string consists only of the character 0 or 1. The value of a Boolean literal is the character itself, excluding the delimiting separators.

Is 1 true or false in SQL?

SQL - Boolean Data A Boolean table column will contain either string values of "True" and "False" or the numeric equivalent representation, with 0 being false and 1 being true.

What is SQL Server literal?

In SQL Server, a literal is the same as a constant. We'll cover several types of literals - string, integer, decimal, and datetime literals.


2 Answers

SQL Server doesn't have a boolean data type. As @Mikael has indicated, the closest approximation is the bit. But that is a numeric type, not a boolean type. In addition, it only supports 2 values - 0 or 1 (and one non-value, NULL).

SQL (standard SQL, as well as T-SQL dialect) describes a Three valued logic. The boolean type for SQL should support 3 values - TRUE, FALSE and UNKNOWN (and also, the non-value NULL). So bit isn't actually a good match here.

Given that SQL Server has no support for the data type, we should not expect to be able to write literals of that "type".

like image 192
Damien_The_Unbeliever Avatar answered Oct 12 '22 12:10

Damien_The_Unbeliever


select * from SomeTable where 1=1 
like image 42
nocache Avatar answered Oct 12 '22 14:10

nocache