New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added limitation of allowed input value #2527
Conversation
The value which is converted to the target type is limited to 8000 bytes. If it exceed this limit a String or binary data would be truncated. is thrown. I believe this need to be known because one can add LEFT() for example to solve such case. Here are some example demonstrating this limitation with varchar and nvarchar inputs. -- does not work DECLARE @test VARCHAR(MAX); SELECT @test = CAST(REPLICATE('a', 8000) AS VARCHAR(MAX)) + '1'; SELECT DATALENGTH(@test) SELECT TRY_CAST(@test AS INT) GO -- does work DECLARE @test VARCHAR(MAX); SELECT @test = CAST(REPLICATE('a', 8000) AS VARCHAR(MAX)); SELECT DATALENGTH(@test) SELECT TRY_CAST(@test AS INT) GO -- does not work DECLARE @test NVARCHAR(MAX); SELECT @test = CAST(REPLICATE('a', 4000) AS NVARCHAR(MAX)) + '1'; SELECT DATALENGTH(@test) SELECT TRY_CAST(@test AS INT) GO -- does work DECLARE @test NVARCHAR(MAX); SELECT @test = CAST(REPLICATE('a', 4000) AS NVARCHAR(MAX)); SELECT DATALENGTH(@test) SELECT TRY_CAST(@test AS INT) GO
@gotqn : Thanks for your contribution! The author(s) have been notified to review your proposed change. |
I believe this is already documented in the article: Large-value data typesLarge-value data types have the same implicit and explicit conversion behavior as their smaller counterparts - specifically, the nvarchar, varbinary, and varchar data types. However, consider the following guidelines:
|
@MikeRayMSFT I am not seeing this information in the official docs . The only thing we have here is a note, that:
OK, but if you check the following examples:
You can check that the engine is able to convert string of Also, it seems to be a practice in the docs to point if there a limit of the input parameters. Many examples like this and this :
Personally, I have never know that the input of
You will see that this limitations is set only for particular types, because there is not a issue (or truncation) when 8000+ bytes string is converting to string. Maybe, we need to have a note in the remarks saying that for numeric types the input value is limited to Imagine where in |
The value which is converted to the target type is limited to 8000 bytes. If it exceed this limit a String or binary data would be truncated. is thrown. I believe this need to be known because one can add LEFT() for example to solve such case. Here are some example demonstrating this limitation with varchar and nvarchar inputs.
-- does not work
DECLARE @test VARCHAR(MAX);
SELECT @test = CAST(REPLICATE('a', 8000) AS VARCHAR(MAX)) + '1';
SELECT DATALENGTH(@test)
SELECT TRY_CAST(@test AS INT)
GO
-- does work
DECLARE @test VARCHAR(MAX);
SELECT @test = CAST(REPLICATE('a', 8000) AS VARCHAR(MAX));
SELECT DATALENGTH(@test)
SELECT TRY_CAST(@test AS INT)
GO
-- does not work
DECLARE @test NVARCHAR(MAX);
SELECT @test = CAST(REPLICATE('a', 4000) AS NVARCHAR(MAX)) + '1';
SELECT DATALENGTH(@test)
SELECT TRY_CAST(@test AS INT)
GO
-- does work
DECLARE @test NVARCHAR(MAX);
SELECT @test = CAST(REPLICATE('a', 4000) AS NVARCHAR(MAX));
SELECT DATALENGTH(@test)
SELECT TRY_CAST(@test AS INT)
GO