Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Database > SQL Language
| Search | Today's Posts | Mark Forums Read
SQL Language SQL Language discussions not specific to a particular RDBMS program or vendor.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Language section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 25th, 2004, 11:12 AM
Registered User
 
Join Date: Oct 2004
Location: , , .
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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.

 
Old October 25th, 2004, 11:23 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: Bangalore, KA, India.
Posts: 2,480
Thanks: 0
Thanked 1 Time in 1 Post
Default

Try this.
Code:
Update TABLENAME set EMAILCOL = Replace(EMAILCOL,'oldEmailAddress','NewEmailAddress') where PRIMARYKEYCOL = ItsValue
Cheers!

_________________________
- Vijay G
Strive for Perfection
 
Old October 25th, 2004, 12:06 PM
Registered User
 
Join Date: Oct 2004
Location: , , .
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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.

 
Old October 25th, 2004, 09:29 PM
Friend of Wrox
 
Join Date: Jun 2003
Location: Bangalore, KA, India.
Posts: 2,480
Thanks: 0
Thanked 1 Time in 1 Post
Default

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




Similar Threads
Thread Thread Starter Forum Replies Last Post
sql query i need seearam MySQL 7 November 30th, 2008 03:14 AM
Output Query to txt file from SQL Query everest SQL Server 2005 4 November 22nd, 2007 01:49 AM
SQL Query!! dpkbahuguna Beginning VB 6 5 October 12th, 2007 12:39 AM
Help with SQL query sattaluri Access 2 August 11th, 2006 09:26 AM
SQL query PinkyCat Classic ASP Databases 3 March 11th, 2005 01:41 PM





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