Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
Solved

# sql loop help

Posted on 2011-03-16
Medium Priority
302 Views

I have a variable that has an address. State and zip have been pulled out and the state is stored in a variable called @state

@address   may have this in it  5220 Main St Winston Salem..

I have a function that gets the word in a string.. Its called udf.getword and it just needs @address and a number that tells me what word you want to pull out from the left.. so

How do get below using my udf?
I want to loop 4 times to get the below results populated in a variable called @lastSearchValue.

First loop gives me Main St Winston Salem
Second loop gives me St Winston Salem
Third loop gives me Winston Salem
Fourth loop gives me Salem
0
Question by:cheryl9063
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points

LVL 41

Expert Comment

ID: 35151859
When you loop four times, you get four different results. Do you want all those results in one variable? Do you always want to loop it four times or its just an example?
0

LVL 1

Author Comment

ID: 35151944
I want it to loop 4 times and populate 4 different variables

@loop1
@loop2
@loop3
@loop4
0

LVL 32

Accepted Solution

Ephraim Wangoya earned 2000 total points
ID: 35152858
try
``````declare @I integer
declare @temp varchar(max), @var1 varchar(max), @var2 varchar(max), @var3 varchar(max), @var4 varchar(max)

set @I = 1

while @I < 5
begin
if @I = 1
set @var1 = @temp
else if @I = 2
set @var2 = @temp
else if @I = 3
set @var3 = @temp
else if @I = 4
set @var4 = @temp

set @I = @I + 1
end
``````
0

## Featured Post

Question has a verified solution.

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

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
###### Suggested Courses
Course of the Month10 days, 5 hours left to enroll