Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 301
  • Last Modified:

SQL Select

Hi,

I was trying few queries on SQL server, i just gave the below query and i was baffled with the result it gave.

Query : Select '1' + 1

Result = 2

I was expecting the sql server to throw an error while i try to add varchar 1 ('1') and the integer 1 (1), instead it added both the numbers and returned the result as 2.
Can anyone guide me on my the select query added those 2 numbers and why it did not throw an error.

Thanks in advance,
Rajeeva
0
rajeeva_nagaraj
Asked:
rajeeva_nagaraj
2 Solutions
 
Vikas GargBusiness Intelligence DeveloperCommented:
Hi,

It did not throw error since '1' is valid when it will be converted to int

but however you try to do this with 'a1' it will definitely throw error.
0
 
Vitor MontalvãoMSSQL Senior EngineerCommented:
The engine will always try to convert implicitly if nothing is said and depending on the data types.
You can see in this MSDN article all implicit conversions that SQL Server will try to perform.
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now