offchance
offchance

Reputation: 682

SQL Server equal comparison between int and string?

Looking at the return value for year() on SQL Server and it seems to be an int.

Then why does the following statement return 'equal'?

Why does the comparison operator consider int 2002 and string '2002' equal? Should it?

select 
    case 
        when year('2002-01-14 00:00:00.000') = '2002' then 'equal' 
        else 'not equal' 
    end

Microsoft's Year Doc.

Upvotes: 2

Views: 476

Answers (1)

xQbert
xQbert

Reputation: 35323

Because SQL server is implicitly casting '2002' to the data type of of the first value in the equality check. (int) in this case

Upvotes: 2

Related Questions