tags:

views:

821

answers:

7

I have a project I am working on. based off a backup SQl Server database from a production server. They have over 16,000 user email addresses, and I want to corrupt them so the system (which has automatic emailers) will not send any emails to valid addresses.

But I still want the users, and I want them in a way that I can reverse what I do (which is why I dont want to delete them).

The SQL I am trying is:

UPDATE Contact SET
EmailAddress = EmailAddress + '.x'

But it isnt working, what am I doing wrong?

Error Message is as follows:

---------------------------
Microsoft SQL Server Management Studio Express
---------------------------
SQL Execution Error.

Executed SQL statement: UPDATE Contact SET EmailAddress = EmailAddress + '.x'
Error Source: .Net SqlClient Data Provider
Error Message: String or binary data would be truncated. The statement has been terminated.
---------------------------
OK   Help   
---------------------------
A: 

try:

UPDATE Contact SET EmailAddress = EmailAddress || '.x';

the || is the string (varchar) concatanation operator in SQL.

HINT: Error messages would help if asking more questions.

Martlark
-1 for '||' is used in Oracle '+' is used in SQL Server
bstoney
hey you learn something everyday!
Martlark
+2  A: 

Can you be more specific about any errors that you get? I've just knocked up an example and it works fine.

Edit - EmailAddress fields you're trying to update are already close to the full size for the field, to make sure the edit applies to all the required record, you need to change add 2 to the column size for that field

BTW Sql to convert it back again

update Contact 
set EmailAddress = SUBSTRING(EmailAddress , 0 , len(EmailAddress ) - 1)
where SUBSTRING(EmailAddress , len(EmailAddress ) - 1, 2) = '.x'
MrTelly
there you go, cheers ;)
Ash
A: 

You're looking for the updatetext function: http://msdn.microsoft.com/en-us/library/ms189466.aspx

JohnW
There HAS to be an easier way then that, surely.
Ash
A: 

First result on Google searching for the error message says:

"String or binary data would be truncated" MS Sql error

This problem occurs when you trying to insert to field a string that exceeds fields length. The only solution I could find was to set a bigger field length.

Ref: http://www.dotnetjunkies.com/WebLog/skiff/archive/2005/01/31/49336.aspx

vmarquez
Plenty of ways to get the desired result of invalid email address that don't require increasing the field length.
Aidan Ryan
You are right Aidan but OP's question was: "But it isnt working, what am I doing wrong?"
vmarquez
+2  A: 

The issue is that EmailAddress +".x" results in some of your data being to long for the given field. You could do:

select * from Contact where len(EmailAddress +".x") > LENFIELD

Replace LENFIELD with the length of the column defined on the table. If you just want to mung the data why not just set all the fields to a single email address? Or modify the rows that are causing the error to occur to be shorter.

JoshBerke
I am pretty sure that its not - varchar(150) - but I will check.
Ash
Ok, so one record is returned when i execute that code, but it must have added whitespace becasue its not that long. I will try and trim.
Ash
I am preety sure that is what the error message means
JoshBerke
Ahh good trim that bad boy up and you should be good to go
JoshBerke
Ok so the solution was pretty simple:UPDATE ContactSET EmailAddress = RTRIM(EmailAddress) + '.x'
Ash
A: 

It looks to me like appending the extra text will make one or more of the email addresses longer than the field size. Rather than appending why don't you replace the last character with a different one?

Aidan Ryan
+2  A: 

Are these fully-qualified email addresses, with @domain.name ? In that case, you could use UPDATE... SELECT REPLACE to change the @ to, say, *.

Dan Breslau