Solved

SQL Select

Posted on 2016-08-08
6
73 Views
Last Modified: 2016-08-15
Hello, in my table, I need to get text after DBA

for example, sometimes the string will be:   dba:Test123
sometimes :  dba: testing
sometimes:  DBA testing 1234

so in the strings, its not always the same, you have  " : "  sometimes not, sometimes just a space...

how can I do the select based on those rules ?  to sbstring only what is afer
0
Comment
Question by:PhilippeRenaud
6 Comments
 
LVL 69

Accepted Solution

by:
Scott Pletcher earned 250 total points
ID: 41747791
SELECT SUBSTRING(string, CHARINDEX('DBA', string) + 3, 8000)

That will show the entire string if 'DBA' does not appear.
If instead you  want to show an empty string if 'DBA' does not appear in the original string, then do this:

SELECT SUBSTRING(string, CHARINDEX('DBA', string + 'DBA') + 3, 8000)
0
 
LVL 1

Author Comment

by:PhilippeRenaud
ID: 41747803
it doest work... : if I do :

  SELECT SUBSTRING('DBA: Test', CHARINDEX('DBA', 'DBA: Test' + 'DBA') + 1, 8000)

Open in new window



it gives me :

BA: Test
0
 
LVL 29

Assisted Solution

by:Olaf Doschke
Olaf Doschke earned 250 total points
ID: 41747856
Because you didn't add 3, as Scott suggested. With +1 you only skip the D of DBA. As you want to skip the three characters D, B, and also A, you have to add 3 to the index position CHARINDEX returns, as it returns the start position of the string searched, not the end position.

Bye, Olaf.
0
Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

 
LVL 69

Expert Comment

by:Scott Pletcher
ID: 41747933
Sorry, I originally used "+1" and later corrected it to "+3" ... was hoping to get it updated before anyone saw the original :).
0
 
LVL 29

Expert Comment

by:Olaf Doschke
ID: 41747948
That explains it. Nevertheless not impossible to figure out, once you look up what CHARINDEX means.

Bye, Olaf.
0
 
LVL 40

Expert Comment

by:Sharath
ID: 41749369
Do you have DBA repeated in your column? If not, you can try like this.
declare @table table (string varchar(100))
insert @table values ('dba:Test123'),('DBA testing 1234')

select ltrim(replace(replace(string, 'dba:',''),'dba','')) from @table

-- Test123
-- testing 1234

Open in new window

0

Featured Post

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

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

Suggested Solutions

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.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
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.

820 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