Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

SQL Select

Posted on 2016-08-08
6
Medium Priority
?
83 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
[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
6 Comments
 
LVL 70

Accepted Solution

by:
Scott Pletcher earned 1000 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 30

Assisted Solution

by:Olaf Doschke
Olaf Doschke earned 1000 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
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 
LVL 70

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 30

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 41

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

TCP/IP Network Protocol Cheat Sheet

TCP/IP is a set of network protocols which is best known for connecting the machines that make up the Internet. The truth is that TCP/IP is one of the oldest network protocols and its survival is mainly based on its simplicity and universality.

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

670 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