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
159 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
  • 3
  • 3
  • 2
8 Comments
 
LVL 142

Expert Comment

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

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

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 92

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
 

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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 142

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 142

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

by Mark Wills PIVOT is a great facility and solves many an EAV (Entity - Attribute - Value) type transformation where we need the information held as data within a column to become columns in their own right. Now, in some cases that is relatively…
I've encountered valid database schemas that do not have a primary key.  For example, I use LogParser from Microsoft to push IIS logs into a SQL database table for processing and analysis.  However, occasionally due to user error or a scheduled task…
This video discusses moving either the default database or any database to a new volume.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

757 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