2

The below query is throwing:

Msg 8152, Level 16, State 2, Line 1 String or binary data would be truncated. The statement has been terminated.

update table1
set cust_line1_name = c.cust_line1_name
from table1 d
inner join dbo.table2 c
on c.cust_nbr = d.cust_id
and c.bus_grp_id = d.bus_grp_id

table1

 cust_line1_name - varchar(24)

table2

 cust_line1_name - nvarchar(140)

I checked whether any data which is having length greater than 24 is getting updated but there is no such data

SELECT LEN(c.cust_line1_name), c.cust_line1_name
from table1 d
inner join dbo.table2 c
on c.cust_nbr = d.cust_id
and c.bus_grp_id = d.bus_grp_id
WHERE  LEN(c.cust_line1_name)> 24

The above query is returing zero result. The issue is in SQL server 2008 Could someone help

2
  • I think you have missed a link between table1 and tables d and c Commented Mar 3, 2016 at 14:27
  • Do you have any triggers on this table? Commented Mar 3, 2016 at 14:39

4 Answers 4

3

Converting to varchar from nvarchar is going to lose the data that isn't able to be converted. Anything in a foreign language from table2.cust_line1_name (nvarchar(140)) would not fit into your varchar 24

The correct course of action:

if table1 column has data:

  1. Add a nvarchar(140) column to table 1
  2. Copy data from current varcahar(24) column
  3. Drop varchar(24) column
  4. Rename the new nvarchar(140) column to old column name

If the table1 column has no data just drop it, and make your new column for populating with data from table2 the same size and data type as the one in table2.

Sign up to request clarification or add additional context in comments.

Comments

0

You have missed the link between your update table (table1) and two tables in from clause:

Your query:

update table1
set cust_line1_name = c.cust_line1_name
from table1 d
inner join dbo.table2 c
on c.cust_nbr = d.cust_id
and c.bus_grp_id = d.bus_grp_id

You must add a WHERE condition to decide how you want to update your table1

For example, you can add

WHERE table1.id = d.id

Because d is the same table (table1)

Otherwise, how you have written your query, you have tried to put in a scalar field a list of rows (result of join between d and c table)

2 Comments

i added where still it is not working update table1 set cust_line1_name = c.cust_line1_name from table1 d inner join dbo.table2 c on c.cust_nbr = d.cust_id and c.bus_grp_id = d.bus_grp_id WHERE c.cust_nbr = d.cust_id
@Thomas: No, you're wrong, not c.cust_nbr = d.cust_id, but you must link table1.cust_id = d.cust_id
0
update table1
set cust_line1_name = left(c.cust_line1_name, 24)
from table1 d, dbo.table2 c
Where c.cust_nbr = d.cust_id
and c.bus_grp_id = d.bus_grp_id

Comments

0

make a change in the condition and then it works fine. Query is:

UPDATE table1
        SET cust_line1_name = c.cust_line1_name
        FROM table1 d INNER JOIN dbo.table2 c
        ON c.cust_nbr = d.cust_id
        AND c.bus_grp_id = d.bus_grp_id
        AND  LEN(c.cust_line1_name)<= 24

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.