Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why cast/convert from int returns an asterisk

Someone recently asked me this question and I thought I'd post it on Stack Overflow to get some input.

Now obviously both of the following scenarios are supposed to fail.

#1:

DECLARE @x BIGINT SET @x = 100 SELECT CAST(@x AS VARCHAR(2)) 

Obvious error:

Msg 8115, Level 16, State 2, Line 3
Arithmetic overflow error converting expression to data type varchar.

#2:

DECLARE @x INT SET @x = 100 SELECT CAST(@x AS VARCHAR(2)) 

Not obvious, it returns a * (One would expect this to be an arithmetic overflow as well???)


Now my real question is, why??? Is this merely by design or is there history or something sinister behind this?

I looked at a few sites and couldn't get a satisfactory answer.

e.g. http://beyondrelational.com/quiz/sqlserver/tsql/2011/questions/Why-does-CAST-function-return-an-asterik--star.aspx

http://msdn.microsoft.com/en-us/library/aa226054(v=sql.80).aspx

Please note I know/understand that when an integer is too large to be converted to a specific sized string that it will be "converted" to an asterisk, this is the obvious answer and I wish I could downvote everyone that keeps on giving this answer. I want to know why an asterisk is used and not an exception thrown, e.g. historical reasons etc??

like image 478
Helix Avatar asked Feb 03 '12 05:02

Helix


People also ask

Is it better to use CAST () or convert ()?

CAST is part of the ANSI-SQL specification; whereas, CONVERT is not. In fact, CONVERT is SQL implementation-specific. CONVERT differences lie in that it accepts an optional style parameter that is used for formatting.

What is CAST () and convert () functions in SQL Server?

The T-SQL language offers two functions to convert data from one data type to a target data type: CAST and CONVERT. In many ways, they both do the exact same thing in a SELECT statement or stored procedure, but the SQL Server CONVERT function has an extra parameter to express style.

Why we use CAST operator in SQL?

The SQL CAST function is mainly used to convert the expression from one data type to another data type. If the SQL Server CAST function is unable to convert a declaration to the desired data type, this function returns an error. We use the CAST function to convert numeric data into character or string data.


2 Answers

For even more fun, try this one:

DECLARE @i INT SET @i = 100 SELECT CAST(@i AS VARCHAR(2)) -- result: '*' go  DECLARE @i INT SET @i = 100 SELECT CAST(@i AS NVARCHAR(2)) -- result: Arithmetic overflow error 

:)


The answer to your query is: "Historical reasons"

The datatypes INT and VARCHAR are older than BIGINT and NVARCHAR. Much older. In fact they're in the original SQL specs. Also older is the exception-suppressing approach of replacing the output with asterisks.

Later on, the SQL folks decided that throwing an error was better/more consistent, etc. than substituting bogus (and usually confusing) output strings. However for consistencies sake they retained the prior behavior for the pre-existing combinations of data-types (so as not to break existing code).

So (much) later when BIGINT and NVARCHAR datatypes were added, they got the new(er) behavior because they were not covered by the grandfathering mentioned above.

like image 140
RBarryYoung Avatar answered Sep 27 '22 16:09

RBarryYoung


You can read on the CAST and CONVERT page on the "Truncating and Rounding Results" section. Int, smallint and tinyint will return * when the result length is too short to display when converted to char or varchar. Other numeric to string conversions will return an error.

like image 35
Dinesh Avatar answered Sep 27 '22 18:09

Dinesh