Solved

SQL - How do I strip everything out of a string after a specific character (e.g. everything after a ";")?

Posted on 2009-07-13
8
167 Views
Last Modified: 2012-05-07
In SQL, how do I strip everything out of a string after a specific character (e.g. everything after a ";")?

Original: "I want this information; I do not want this information"
Desired result: "I want this information"
0
Comment
Question by:dbrennecke
[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
  • Learn & ask questions
  • 3
  • 3
  • 2
8 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 24844263

select left(yourfield + ';', charindex(';', yourfield + ';' )-1) from yourtable
0
 
LVL 93

Expert Comment

by:Patrick Matthews
ID: 24844269
Hello dbrennecke,

SELECT CASE
      WHEN CHARINDEX(';', SomeColumn) > 0 THEN LEFT(SomeColumn, CHARINDEX(';', SomeColumn) - 1)
      ELSE SomeColumn END AS SomeColumn
FROM SomeTable

Regards,

Patrick
0
 
LVL 93

Expert Comment

by:Patrick Matthews
ID: 24844275
dbrennecke,

angelIII's method is to be preferred; his method of concatenating a semicolon automatically handles the
contingency I was using the CASE expression to handle.

Regards,

Patrick
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:dbrennecke
ID: 24844487
Angellll,
Myfield is of type text, resulting in the following error:
"The data types text and varchar are incompatible in the add operator."
My lame attempts to correct this have fallen short :(
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 24844504
sql 2005 => change the TEXT data type of your field(s) into VARCHAR(MAX).
solves all those kind of archaic problem with TEXT data type :)
0
 

Author Comment

by:dbrennecke
ID: 24844520
unfortunately, I don't own the schema and can't change the declaration... so I need to do this on the fly
0
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 24844541
ok:
select left(CAST(yourfield as vachar(MAX))+ ';', charindex(';', CAST(yourfield as vachar(MAX))+ ';' )-1) from yourtable

Open in new window

0
 

Author Closing Comment

by:dbrennecke
ID: 31603038
Thanks for hanging with me :)
0

Featured Post

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

Question has a verified solution.

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

Recently, when I was asked to create a new SQL 2005 cluster, Microsoft released a new service pack for MS SQL 2005 what is Service Pack 3. When I finished the installation of MS SQL 2005 I found myself troubled why the installation of SP3 failed …
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

626 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