Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Casting as varchar

Author  Topic 

OldMySQLUser
Constraint Violating Yak Guru

301 Posts

Posted - 2009-07-14 : 08:34:33
A SQL Server 2005 table has two fields. Both are varchar (100).

I need to alter the first field from varchar (100) to text and then concantenate both fields if the second field contains any text.

I used:

USE debt
GO
UPDATE debtor

SET KeyInformation = Cast (KeyInformation AS varchar) + NextLegalAction
WHERE LEN(NextLegalAction) > 0


However, there appears to be an implied field length of 30 characters for the varchar casting as it has truncated the first field's contents when performing the cast operation.

How should I have written my sql query to avoid this please?

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2009-07-14 : 08:36:51
UPDATE debtor

SET KeyInformation = Cast (KeyInformation AS varchar(100)) + NextLegalAction
WHERE LEN(NextLegalAction) > 0


Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

OldMySQLUser
Constraint Violating Yak Guru

301 Posts

Posted - 2009-07-14 : 10:15:29
Many thanks. I've realised that myself now.

Would have saved me a lot of hassle if I had thought of it before.

Note to self: when it reaches midnight, and you're on the day shift, stop coding and GO HOME. :)
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2009-07-14 : 10:52:14
quote:
Originally posted by OldMySQLUser

Many thanks. I've realised that myself now.

Would have saved me a lot of hassle if I had thought of it before.

Note to self: when it reaches midnight, and you're on the day shift, stop coding and GO HOME. :)


Alsways specify length when convert to varchar
http://sqlblogcasts.com/blogs/madhivanan/archive/2007/12/04/column-length-and-data-length.aspx

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -