p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   SQL Language (http://p2p.wrox.com/forumdisplay.php?f=100)
-   -   SQL Query- Please help! (http://p2p.wrox.com/showthread.php?t=20363)

clearsky October 25th, 2004 11:12 AM

SQL Query- Please help!
 
Hello,

I am trying to write a SQL query that would update email address on a field. The filed is text format and contains several email addresses in a field. I just want to update one address among them and keep the rest as is. Could you please provide me feedback regarding writing a script for this? I would highly appreciate it.

The query will be run against SQL server DB.

Thanks in advance for your help.


happygv October 25th, 2004 11:23 AM

Try this.
Code:

Update TABLENAME set EMAILCOL = Replace(EMAILCOL,'oldEmailAddress','NewEmailAddress') where PRIMARYKEYCOL = ItsValue
Cheers!

_________________________
- Vijay G
Strive for Perfection

clearsky October 25th, 2004 12:06 PM

Thanks for your reply. This doesn’t seem to work as the field is text format as oppose to varChar.

I get this message:

[Microsoft][ODBC SQL Server Driver][SQL Server]Argument data type text is invalid for argument 1 of replace function.


happygv October 25th, 2004 09:29 PM

Yes, for text datatypes Replace, wouldn't work. There is a workaround for this.
Code:

Declare @val varchar(8000)
Select @val=EMAILCOL from TABLENAME where PRIMARYKEYCOL = ItsValue
-- select @val
update TABLENAME set EMAILCOL = replace(@val,'oldval','newval') where PRIMARYKEYCOL = ItsValue
Select EMAILCOL from TABLENAME where PRIMARYKEYCOL = ItsValue

This would work only if the length of that column doesn't exceed 8000 characters. I would suggest you not to use text for that column. Check what is the maximum length of that column? If that is not more than 8000, you may change that to varchar(8000) in the table structure. Assuming a MailId doesn't exceed 40 characters at the maximum, does that column contain more that 200 EMail Ids? If that is going to be less than that, you can blindly modify its datatype to be varchar(8000 or less)

Hope that helps.
Cheers!

_________________________
- Vijay G
Strive for Perfection


All times are GMT -4. The time now is 01:31 PM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.