Forum Discussion
mansoorabid
Jan 26, 2024Copper Contributor
SQL Query Issue
I have two table Table1 and Table2 CREATE TABLE [dbo].[Table1]( [ID] [int] IDENTITY(1,1) NOT NULL, [User_Id] [varchar](50) NULL, [counts] [int] NULL, PRIMARY KEY CLUSTERED ( [ID] ASC...
- Jan 29, 2024I get the error “String or binary data would be truncated.”
As the error message clearly says and the cause is
[dbo].[Table1] => [User_Id] [varchar](50)
[dbo].[table2] => [User_Id] [varchar](100)
LainRobertson
Jan 26, 2024Silver Contributor
I don't see any issue. Everything is behaving precisely as it should.
From table1, [User_Id] is set to a maximum of 50 characters while in table2, [User_Id] is set to 100 characters. Any standards-compliant SQL engine should throw an error if you try and INSERT [User_Id] from table2 into table1.
In your second example, you're truncating [User_Id] back down to a maximum of 30 characters, which fits into the limit of 50 from table 1, so there is no longer an error condition.
So, as I say, everything is fine and behaving exactly as it should.
Cheers,
Lain