Solved

SQL Delete all data after character

Posted on 2013-01-14
9
354 Views
Last Modified: 2013-01-14
I have a table that contains # signs and I need to delete all of the data BEFORE the # signs

Example Data:


qid	                qtext
1                      Weather#Sunny
2                      Weather#Rainy and cloudy
3                      Weather#Windy and foggy with possible rain

Open in new window

0
Comment
Question by:swaggrK
9 Comments
 

Author Comment

by:swaggrK
ID: 38775707
Also, data can appear like...

qid	                qtext
1                      Weather#Sunny
2                      Weather#Rainy and cloudy
3                      Weather#Windy and foggy with possible rain
4                      Report#Breezy and foggy with possible rain
5                      Weather Report#Snowy and partly cloudy
                                  

Open in new window

0
 
LVL 65

Expert Comment

by:Jim Horn
ID: 38775712
Get the character index of the #, then take the left characters right before that index.

SELECT LEFT('Weather#Sunny', CHARINDEX('#', 'Weather#Sunny') - 1)

returns 'Weather'
0
 
LVL 92

Expert Comment

by:Patrick Matthews
ID: 38775748
This is a twist, which returns the whole string if there is no # character:

SELECT LEFT(qtext, CHARINDEX('#', qtext + '#') - 1) AS Shorter
FROM SomeTable

Open in new window


As an update:

UPDATE SomeTable
SET qtext = LEFT(qtext, CHARINDEX('#', qtext + '#') - 1)

Open in new window

0
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
LVL 22

Accepted Solution

by:
Steve Wales earned 125 total points
ID: 38775754
I think you're wanting the text AFTER the # sign, so if so:

select SUBSTRING(qtext, CHARINDEX('#', qtext,) + 1, LEN(qtext))

Open in new window


You could wrap an RTRIM() fuction around that as well to chop off any trailing spaces if it's an issue.

Pulling a substring for length greater than the total length of the string doesn't seem to bother SQL Server but if you wanted to work out the exact length by subtracting the start position of the # via another CHARINDEX call, that would be an option too.
0
 
LVL 65

Expert Comment

by:Jim Horn
ID: 38775811
>think you're wanting the text AFTER the # sign
Doh.  Misread the question.

sjwales' comment is correct, although after a typo /  comma is deleted

 
Declare @qtext varchar(50) = 'Weather#Sunny'

select SUBSTRING(@qtext, CHARINDEX('#', @qtext) + 1, LEN(@qtext))

Open in new window

0
 

Author Comment

by:swaggrK
ID: 38775890
Yes, I want to KEEP the text AFTER the # sign.

So, my results would look like...

BEFORE:

qid	                qtext
1                      Weather#Sunny
2                      Weather#Rainy and cloudy
3                      Weather#Windy and foggy with possible rain
4                      Report#Breezy and foggy with possible rain
5                      Weather Report#Snowy and partly cloudy



AFTER:

qid	                qtext
1                      Sunny
2                      Rainy and cloudy
3                      Windy and foggy with possible rain
4                      Breezy and foggy with possible rain
5                      Snowy and partly cloudy

Open in new window

0
 

Author Comment

by:swaggrK
ID: 38775938
I am still not sure how to accomplish this based on the previous suggestions. Thnx.
0
 
LVL 65

Assisted Solution

by:Jim Horn
Jim Horn earned 75 total points
ID: 38775943
<point of order>
The title of this question is 'SQL Delete all data after character', but in the body it states the question is to delete all data before the # character.  Gotta be careful.
</point of order>

So, borrowing from the above, if you wish to permanently change the table so that the text before the # is deleted....

UPDATE YourTableName
SET qtext = SUBSTRING(qtext, CHARINDEX('#', qtext) + 1, LEN(qtext))
0
 

Author Comment

by:swaggrK
ID: 38775963
@jimhorn...thanks for the <point of order>

Also, this worked perfectly...THANKS!
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
I have a large data set and a SSIS package. How can I load this file in multi threading?
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

770 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