Is it always 1 in SQL?

I am trying to define this standard SQL behavior for comparing a number with a character or string version of the same number. Returns whether SELECT 1 = '1'(or the like) for a "true" value ( true, 1, 't'etc.)? I have confirmed how much on PostgreSQL and MySQL, but I can not find the resource for SQL in general.

Update: . The challenge is that I'm trying to figure out if using the number without quotes will work when selecting / inserting / updating / etc. from a non-numeric field whose value is a number.

+3
source share
10 answers

SELECT 1='1'gives TRUE, as it '1'is the correct constructor for INTin the entire implementation known to me.

But SQL uses strong typing, see the following:

# SELECT 1=CAST('1' AS TEXT);
ERROR:  operator does not exist: integer = text
LINE 1: SELECT 1=CAST('1' AS TEXT);
                ^
HINT:  No operator matches the given name and argument type(s). You might need to add  explicit type casts.

Regarding the standard (SQL 92, 99 and 2003), this seems wrong:

     <literal> ::=
            <signed numeric literal>
          | <general literal>

     <general literal> ::=
            <character string literal>
          | <national character string literal>
          | <bit string literal>
          | <hex string literal>
          | <datetime literal>
          | <interval literal>

     <signed numeric literal> ::=
          [ <sign> ] <unsigned numeric literal>

     <unsigned numeric literal> ::=
            <exact numeric literal>
          | <approximate numeric literal>

     <exact numeric literal> ::=
            <unsigned integer> [ <period> [ <unsigned integer> ] ]
          | <period> <unsigned integer>

     <unsigned integer> ::= <digit>...

     <character string literal> ::=
          [ <introducer><character set specification> ]
          <quote> [ <character representation>... ] <quote>
            [ { <separator>... <quote> [ <character representation>... ] <quote> }... ]

because it <quote>is found only in <bit string literal>, <hex string literal>... but not in numeric literals ...

+6
source

SQL Server

if 1 = '1'
print 'yes'
else
print 'no'

conclusion: yes

This conversion, see here for a complete list of implicit and explicit conversion options: CAST and CONVERT (Transact-SQL)

+2
source

, SQL Server SELECT 1 = '1' . , , , 1 = '1'

if (1 = '1') begin
    print 'true'
end else begin
    print 'false'
end

:

true
+2

"SQL " "" .

MySQL PostgreSQL, Oracle SQL Server, WHERE WHEN.

- .

mydata , :

SELECT  1
WHERE   @mydata

SELECT  1
FROM    dual
WHERE   :mydata

, SQL .

.

, , .

+2

1 - , '1' - CHAR, . , ,

+1

MySQL 5.x SQL Server 2005 '1' 1 , true.

.

+1

, SQL, 1 = '1' .

0

( ) :

SELECT 1 = '1'

, :

SELECT '1'::text = 1

. , , . , PostgreSQL .

0

:

, :

SELECT *
FROM MyTable 
WHERE StringColumn = 1

. , , sql , . MS SQL Server " varchar" blah " int."

, , , . :

SELECT *
FROM MyTable 
WHERE StringColumn = '1'
0

" " SELECT 1. .

-1

Source: https://habr.com/ru/post/1729930/


All Articles