Solved

SQL Select

Posted on 2016-08-08
6
70 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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Query Optimization 14 43
TSQL - How to declare table name 26 30
Mysql Left Join Case 10 52
I have a SQL Select with a where clause that has an issue. 6 37
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Viewers will learn how the fundamental information of how to create a table.

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