Friday, February 24, 2012

How to copy the value of one field into another using SQL Server 2000

I am a little embarrased to be asking this question because I know that the answer must be simple but I am still a nube at SQL. I am stuck with a simple query that copies one field of data into another. Below is the script that I am trying to execute.

UPDATE table
SET field2 = field1

I get an error from SQL Server that says that the String or binary data would be truncated. I am assuming that the SQL server is trying to concatenate all the value of field1 into field2. All I am trying to do is copy the values of one field into another for each and every row in the table. Thanks in advance.I am assuming that the SQL server is trying to concatenate all the value of field1 into field2.

No, the UPDATE command goes through each record that matches your WHERE criteria (which is blank here, so it goes through every record) and performs the SET portion on individual records. In your example, after the UPDATE is performed, field2 should be equal to field1 for the entire table.

I get an error from SQL Server that says that the String or binary data would be truncated.

That probably means that the two fields are different types. BTW, VARCHAR(25) and VARCHAR(15) are "different types" to SQL. Obviously a 25 character string isn't going to fit in a field that is only 15 characters long.|||...so you will need to either expand both columns to the same size, or use the CAST(), CONVERT(), or LEFT() functions to trim the long string to the shorter length. SQL Server will not do it for you because it entails a loss of data.|||Thanks guys!. The field size was the problem.

No comments:

Post a Comment