Solved

Using Declare variable within update statement

Posted on 2014-04-04
4
178 Views
Last Modified: 2014-04-04
I need to change the text within a couple of records where the data contains the word "Annual" and change it to "Open"

declare @var1
@var1 = 'Open'

SELECT [ResourceSet]
      ,[ResourceType]
      ,[CultureCode]
      ,[ResourceKey]
      ,[ResourceValue]
      ,[Category]
      ,[Translated]
  FROM [bcdevtraining].[dbo].[cm_resource_core]
--UPDATE [bcdevtraining].[dbo].[cm_resource_core]
--   SET [ResourceValue] = 'Open Enrollment'
where [ResourceValue] like '%Annual Enrollment%'
GO
First record  = It's {0} &  @var1 &  'Enrollment through {1}. This is your opportunity to change your benefit elections, add or modify dependents, and change beneficiaries for {0}.
Second record = Begin Your Annual Enrollment Now!

Open in new window

0
Comment
Question by:Karen Schaefer
  • 2
  • 2
4 Comments
 
LVL 40

Expert Comment

by:lcohan
ID: 39978678
Why do you need to use variable if that's all you need to do and an update like you described (and enclosed below) will do what you need to:

UPDATE [bcdevtraining].[dbo].[cm_resource_core]
  SET [ResourceValue] = 'Open Enrollment'
where [ResourceValue] like '%Annual Enrollment%'
0
 

Author Comment

by:Karen Schaefer
ID: 39978684
Is there a Way to do a Mass update searching for all instances of "Annual Enrollment" and update the just that portion of the text with "Open Enrollment"?

K
0
 
LVL 40

Accepted Solution

by:
lcohan earned 500 total points
ID: 39978686
Sure and sorry my bad for the first posting - this must do it - use the REPLACE sql function

UPDATE [bcdevtraining].[dbo].[cm_resource_core]
  SET [ResourceValue] = replace([ResourceValue],'Annual Enrollment','Open Enrollment')
WHERE [ResourceValue]='%Annual Enrollment%'

OR you can reduce it to just the two words you mentioned:


UPDATE [bcdevtraining].[dbo].[cm_resource_core]
  SET [ResourceValue] = replace([ResourceValue],'Annual ','Open ')
WHERE [ResourceValue]='%Annual%'
0
 

Author Closing Comment

by:Karen Schaefer
ID: 39978754
thanks for the refresher - just a  little rusty.
0

Featured Post

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Viewers will learn how the fundamental information of how to create a table.

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question