Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Truncate SQL varchar column

Tags:

sql

sql-server

I currently have a column of varchar(3000). I want to reduce that to 400 characters.

When I execute this query:

ALTER TABLE [dbo].[App]
ALTER COLUMN SpecialInstructions VARCHAR(400) NOT NULL

I get this error:

Msg 8152, Level 16, State 14, Line 1
String or binary data would be truncated.

How do I set where previous data in that column would just be cut off? I'm using a development database, so it wouldn't matter if I was losing information. Or is there a better way to do this? Thanks in advance.

like image 913
hengj Avatar asked Jun 23 '26 13:06

hengj


1 Answers

update [dbo].[App]
set  SpecialInstructions = left(SpecialInstructions, 400)

You can optionally add a WHERE clause to make sure only too long values are shortened:

where len(SpecialInstructions) > 400

This is to reduce the number of rows updated. Keeps transaction smaller, incl write-sets.

like image 194
jarlh Avatar answered Jun 26 '26 02:06

jarlh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!