SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Error: Converting varchar to INT
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Rock_query
Yak Posting Veteran

52 Posts

Posted - 05/08/2013 :  17:57:34  Show Profile  Reply with Quote
Here is my code:

DECLARE @minimum AS INT, @maximum AS INT

SET @minimum = (SELECT MIN(SalesorderID)
FROM Sales.SalesOrderDetail)
SET @maximum = (SELECT MAX(SalesorderID)
FROM Sales.SalesOrderDetail)

Print 'The minimum SalesOrderID is ' + @minimum + ' and the maximum is ' + @maximum

Here is the error message:

Msg 245, Level 16, State 1, Line 8
Conversion failed when converting the varchar value 'The minimum SalesOrderID is ' to data type int.


In the AdventureWorks2012 database, the salesOrderID column in the MIN and MAX functions has a datatype of INT so I initialized the variables to INT. It seems like SQL is trying to convert the words "The minimum SalesOrderID is" to an INT datatype.


1. What is happening here?

2. I changed the variable datatypes to VARCHAR(5) and everything worked, but I don't understand why. Since the salesOrderID returns an INT value, if I assign that to a variable that has been declared as VARCHAR(5), I would think that would have caused a conflict, but evidently not.

Please answer both questions.

James K
Flowing Fount of Yak Knowledge

3559 Posts

Posted - 05/08/2013 :  18:21:25  Show Profile  Reply with Quote
You probably need to convert the @minimum and @maximum to character data types, like this:
Print 'The minimum SalesOrderID is ' + CAST(@minimum AS VARCHAR(32)) + ' and the maximum is ' + CAST( @maximum AS VARCHAR(32))
Go to Top of Page

Lamprey
Flowing Fount of Yak Knowledge

4612 Posts

Posted - 05/08/2013 :  18:28:38  Show Profile  Reply with Quote
quote:
Originally posted by Rock_query

Here is my code:

Print 'The minimum SalesOrderID is ' + @minimum + ' and the maximum is ' + @maximum

Msg 245, Level 16, State 1, Line 8
Conversion failed when converting the varchar value 'The minimum SalesOrderID is ' to data type int.

1. What is happening here?

2. I changed the variable datatypes to VARCHAR(5) and everything worked, but I don't understand why. Since the salesOrderID returns an INT value, if I assign that to a variable that has been declared as VARCHAR(5), I would think that would have caused a conflict, but evidently not.

Please answer both questions.

1. INT has higher precedence that VARCHAR. Since you are concatenating different data types together SQL is doing an IMPLICIT conversion of the varchar string "The minimum SalesOrderID is " to and INT. That is just one reason you should always do EXPLICT conversions.

2. Similar to #1, SQL is IMPLICITly converting the INT to a VARCHAR(5) for you. Again, always do EXPLICIT conversions so you are working with the same data types.

Edit: misread #2.

Edited by - Lamprey on 05/08/2013 18:32:40
Go to Top of Page

Rock_query
Yak Posting Veteran

52 Posts

Posted - 05/08/2013 :  19:14:03  Show Profile  Reply with Quote
quote:
Originally posted by Lamprey

quote:
Originally posted by Rock_query

Here is my code:

Print 'The minimum SalesOrderID is ' + @minimum + ' and the maximum is ' + @maximum

Msg 245, Level 16, State 1, Line 8
Conversion failed when converting the varchar value 'The minimum SalesOrderID is ' to data type int.

1. What is happening here?

2. I changed the variable datatypes to VARCHAR(5) and everything worked, but I don't understand why. Since the salesOrderID returns an INT value, if I assign that to a variable that has been declared as VARCHAR(5), I would think that would have caused a conflict, but evidently not.

Please answer both questions.

1. INT has higher precedence that VARCHAR. Since you are concatenating different data types together SQL is doing an IMPLICIT conversion of the varchar string "The minimum SalesOrderID is " to and INT. That is just one reason you should always do EXPLICT conversions.

2. Similar to #1, SQL is IMPLICITly converting the INT to a VARCHAR(5) for you. Again, always do EXPLICIT conversions so you are working with the same data types.

Edit: misread #2.




Thank you for your reply. Can you show me an example of an EXPLICIT conversion that you are referring to?
Go to Top of Page

MuMu88
Aged Yak Warrior

547 Posts

Posted - 05/08/2013 :  19:40:50  Show Profile  Reply with Quote

In addition to what James K already provided, examples of explicit conversions from
http://msdn.microsoft.com/en-us/library/ms187928.aspx (a nice technical document that describes data conversions)

SELECT 
   GETDATE() AS UnconvertedDateTime,
   CAST(GETDATE() AS nvarchar(30)) AS UsingCast,
   CONVERT(nvarchar(30), GETDATE(), 126) AS UsingConvertTo_ISO8601  ;
GO


When you use CAST() or CONVERT() you are explicitly converting the data.
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.08 seconds. Powered By: Snitz Forums 2000