Solved

Change String

Posted on 2013-05-10
3
422 Views
Last Modified: 2013-05-10
I have a field in a SQL table that I need to update based on ID

The SQL is

Update myTable
set  ExecSQL =  @newSQL
where id = @id

An example of the string I'm passing in from my Web VB code is
usp_EmailsForSpecialtyStates 'MA','ER' True, 1000, 365, False, 1, Special, 1, 0

And all I need to do is change that first TRue to a false so that the new value would be
usp_EmailsForSpecialtyStates 'MA','ER' False, 1000, 365, False, 1, Special, 1, 0

Some caveates are that the first two sections are single quotes so that has to be handled
Also...
The string could come in as
usp_EmailsForSpecialtyStates 'CA,CT,KY,MA','ER' True, 1000, 365, False, 1, Special, 1, 0

It's the first filed after the first two sections (States and Stecialty)

And if that first True is already a false...ignore the update.
0
Comment
Question by:lrbrister
  • 2
3 Comments
 
LVL 35

Expert Comment

by:David Todd
ID: 39156900
Hi,

So the fileid in your examples is 1000, no?

Regards
  David

PS Suggest that the multiple states thing is a bit of a pest! It means that the string needs to be properly parsed, and not just count the commas!
0
 
LVL 35

Accepted Solution

by:
David Todd earned 500 total points
ID: 39156973
Hi,

Here is first draft code to parse your string. As you asked in SQL topics, this is in SQL.

use tempdb
go

declare @s varchar( max )
set @s = 'usp_EmailsForSpecialtyStates, ''MA'',''ER'', True, 1000, 365, False, 1, Special, 1, 0'

declare @c int
declare @pc int

-- test parse string into components
while 1 = 1 begin
	set @c = charindex( ',', @s, isnull( @pc, 0 ))

	if @c = 0
		break

	declare @p varchar( max )
	set @p = substring( @s, isnull( @pc, 0 ), @c - isnull( @pc, 0 ))

	if left( @p, 1 ) = ' ' 
		set @p = right( @p, len( @p ) - 1 )

	print @p

	set @pc = @c + 1

end

Open in new window

Note that I've added two commas to your string.

Regards
  David
0
 

Author Closing Comment

by:lrbrister
ID: 39157031
That's it. Thanks
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Viewers will learn how the fundamental information of how to create a table.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

746 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now