how to truncate text automatically in insertion

Yang, Shu Zi (TC/TC) 0 Reputation points
2023-08-30T14:37:11.4333333+00:00

Hi there,

my insertion script are sometimes interrupted by length violation issue. how can the overlength text got cut automatically by the SQL Server. We can afford the abruption.

thanks,

Sheldon

SQL Server Other
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Erland Sommarskog 121.4K Reputation points MVP Volunteer Moderator
    2023-08-30T21:53:24.8666667+00:00

    If the target column is nvarchar(50), then do something like:

    INSERT targettbl(..., col, ...)
        SELECT ..., convert(nvarchar(50), sourcecol), ....
        FROM   ...
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.