Solved

SQL Syntax to pull part of String Field back..? (CharIndex..?)

Posted on 2013-05-23
6
505 Views
Last Modified: 2013-05-29
My data field looks like this:


"Adventist Health System - Winter Park, FL"

This is a single String/Text Field and is consistent with the " - " and the ", " between the HospitalName and CityName, and the CityName and StateCode. All entries in this field follow this same format:  HospitalName " - " CityName ", " StateCode

I have a requirement to pull ONLY the CITYNAME from the above string field.

So that would be I need to pull the TEXT between the " - " and the ", "

I need the function and syntax to use....THANKS
0
Comment
Question by:MIKE
6 Comments
 
LVL 23

Expert Comment

by:nemws1
ID: 39191897
You want PATINDEX and SUBSTRING.  I broke this down into more steps than necessary, just so you can understand it better.

DECLARE @str VARCHAR(100) = 'Adventist Health System - Winter Park, FL';
DECLARE @city VARCHAR(100);

DECLARE @sep1loc INT = PATINDEX('% - %', @str) + 3;
DECLARE @sep2loc INT = PATINDEX('%, %', @str);
DECLARE @citystrlen INT = @sep2loc - @sep1loc;
SELECT @sep1loc, @sep2loc, DATALENGTH(@str), @citystrlen
SELECT SUBSTRING(@str, @sep1loc, @citystrlen);

Open in new window

0
 
LVL 16

Expert Comment

by:Surendra Nath
ID: 39191899
the below one should help
declare @t varchar(200) 
set @t = 'Adventist Health System - Winter Park, FL'
select substring(@t,charindex('-',@t)+1,charindex(',',@t)-charindex('-',@t)-1)

Open in new window

0
 
LVL 23

Expert Comment

by:nemws1
ID: 39191913
Did you want this in a function?  Easy enough.

USE tempdb
GO

CREATE FUNCTION dbo.city_from_hospital_name
(@instr VARCHAR(1000))
RETURNS VARCHAR(1000)
AS
BEGIN
	DECLARE @sep1loc INT = PATINDEX('% - %', @instr) + 3;
	DECLARE @sep2loc INT = PATINDEX('%, %', @instr);
	DECLARE @citystrlen INT = @sep2loc - @sep1loc;
	-- SELECT @sep1loc, @sep2loc, DATALENGTH(@str), @citystrlen
	RETURN SUBSTRING(@instr, @sep1loc, @citystrlen);
END
GO

DECLARE @str VARCHAR(100) = 'Adventist Health System - Winter Park, FL';
SELECT dbo.city_from_hospital_name(@str);

Open in new window

0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 1

Expert Comment

by:dbote
ID: 39191998
If you don't want to you variables or functions here is a straight SQL example depending on the data you may have to play with the string manipulation.  

select 'Adventist Health System - Winter Park, FL' [Hospital],
      charindex('-','Adventist Health System - Winter Park, FL') [DashStartPosition],
      charindex(', ','Adventist Health System - Winter Park, FL') [StateStartPosition],
      ltrim(substring('Adventist Health System - Winter Park, FL',charindex('-','Adventist Health System - Winter Park, FL') +1,(charindex(', ','Adventist Health System - Winter Park, FL')-1) - (charindex('-','Adventist Health System - Winter Park, FL')))) [CityName]
0
 
LVL 31

Expert Comment

by:awking00
ID: 39192060
select substring(datafield,charindex('-',datafield) + 2,charindex(',',datafield) - charindex('-',datafield) - 2)
from yourtable;
0
 
LVL 69

Accepted Solution

by:
ScottPletcher earned 500 total points
ID: 39192833
A CROSS APPLY makes the code so much easier to read, follow and maintain; and often prevents having to repeat CHARINDEXes and other functions:


SELECT
    LEFT(city_and_state, CHARINDEX(',', city_and_state) - 1) AS city
FROM (
    SELECT 'Adventist Health System - Winter Park, FL' AS string UNION ALL
    SELECT 'Baptist Hospital - Pensacola, FL'
) AS test_data
CROSS APPLY (
    --strip the orig 'name - city, st' string in the table to just 'city, st'
    SELECT SUBSTRING(string, CHARINDEX('-', string) + 2, 200) AS city_and_state
) AS ca1
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

758 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

19 Experts available now in Live!

Get 1:1 Help Now