Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
Rock_query
Yak Posting Veteran
55 Posts |
Posted - 2013-05-08 : 17:57:34
|
Here is my code:DECLARE @minimum AS INT, @maximum AS INTSET @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 ' + @maximumHere is the error message:Msg 245, Level 16, State 1, Line 8Conversion 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
Master Smack Fu Yak Hacker
3873 Posts |
Posted - 2013-05-08 : 18:21:25
|
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)) |
 |
|
Lamprey
Master Smack Fu Yak Hacker
4614 Posts |
Posted - 2013-05-08 : 18:28:38
|
quote: Originally posted by Rock_query Here is my code:Print 'The minimum SalesOrderID is ' + @minimum + ' and the maximum is ' + @maximumMsg 245, Level 16, State 1, Line 8Conversion 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. |
 |
|
Rock_query
Yak Posting Veteran
55 Posts |
Posted - 2013-05-08 : 19:14:03
|
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 ' + @maximumMsg 245, Level 16, State 1, Line 8Conversion 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? |
 |
|
MuMu88
Aged Yak Warrior
549 Posts |
Posted - 2013-05-08 : 19:40:50
|
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)[CODE] SELECT GETDATE() AS UnconvertedDateTime, CAST(GETDATE() AS nvarchar(30)) AS UsingCast, CONVERT(nvarchar(30), GETDATE(), 126) AS UsingConvertTo_ISO8601 ;GO[/CODE]When you use CAST() or CONVERT() you are explicitly converting the data. |
 |
|
|
|
|
|
|