Solved

SQL String extraction with Case Select

Posted on 2011-03-10
4
270 Views
Last Modified: 2012-06-22
I have a function that takes the last word (the state) out of my address variable @Location and populates a variable called @State.. what a need is a case select that does this

If @State ='Carolina'
Then get the second to the last word from @location and concatenate it with @State

This is the same for Mexico, Jersey, York, Daokato etc..


IF @State = Virginia or VA then LOOK for the second to the last word in @Location and if 'West" is found then concatenate otherwise leave it...
0
Comment
Question by:cheryl9063
  • 2
4 Comments
 
LVL 32

Accepted Solution

by:
ewangoya earned 250 total points
ID: 35099583
something like this

case
  when (@State = 'Carolina' then
      case
          when charindex('West', Location, 1) > 0 then
              'West ' + @State
          else
              @State
       end
   else
      @State
end
0
 
LVL 1

Author Comment

by:cheryl9063
ID: 35099621
How do you get the second to the last word out of a string?
0
 
LVL 15

Assisted Solution

by:derekkromm
derekkromm earned 250 total points
ID: 35099705
declare @t varchar(100)
select @t = 'very large test'

select reverse(left(right(reverse(@t), len(@t) - charindex(' ', reverse(@t))), charindex(' ', right(reverse(@t), len(@t) - charindex(' ', reverse(@t)))) - 1))

0
 
LVL 1

Author Closing Comment

by:cheryl9063
ID: 35099746
Thanks!
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

830 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