Solved

Splitting  a field in SQL

Posted on 2016-09-16
9
37 Views
Last Modified: 2016-10-09
I have this string BPL-TS02/COMPANY-NAME\jsmith.

I do I just extract the jsmith element? I cannot just use RIGHT as the name will vary in the number of characters.
0
Comment
Question by:CMChalcraft
[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
9 Comments
 
LVL 29

Expert Comment

by:Pawan Kumar
ID: 41801115
What you need as the output from the string, I shall write it for you
0
 
LVL 29

Accepted Solution

by:
Pawan Kumar earned 250 total points (awarded by participants)
ID: 41801117
Please try this

---

DECLARE @ AS VARCHAR(1000) = 'BPL-TS02/COMPANY-NAME\jsmith'
SELECT SUBSTRING(@,CHARINDEX('\',@,0)+1,(DATALENGTH(@)+1)-22)

--

Open in new window

0
 
LVL 29

Expert Comment

by:Pawan Kumar
ID: 41801118
Please try this

--

DECLARE @ AS VARCHAR(1000) = 'BPL-TS02/COMPANY-NAME\jsmith'
SELECT SUBSTRING(@,CHARINDEX('\',@,0)+1,(DATALENGTH(@)+1)-CHARINDEX('\',@,0))

--

Open in new window

0
ClickHouse in a General Analytical Workload

We have mentioned ClickHouse in some recent posts, where it showed excellent results.

In this article on Experts Exchange, we’ll look at how ClickHouse performs in a general analytical workload using the star schema benchmark test.

 
LVL 34

Assisted Solution

by:ste5an
ste5an earned 125 total points (awarded by participants)
ID: 41801119
E.g.
DECLARE @Text NVARCHAR(255) = N'BPL-TS02/COMPANY-NAME\jsmith';

SELECT SUBSTRING(@Text, CHARINDEX('\', @Text) + 1, 255)

Open in new window

1
 
LVL 69

Assisted Solution

by:Scott Pletcher
Scott Pletcher earned 125 total points (awarded by participants)
ID: 41801644
RIGHT is easiest as long as the delimiter before the final string is always a "\" (or is always known to be a certain char(s)):

SELECT string, RIGHT(string, CHARINDEX('\', REVERSE(string)) - 1)
FROM (
    VALUES('BPL-TS02/COMPANY-NAME\jsmith'),
                  ('BPL-TS02/COMPANY-NAME\a'),
                  ('BPL-TS02/COMPANY-NAME\averyveryveryveryverylongname')
) AS sample_data(string)
0
 
LVL 51

Expert Comment

by:Vitor Montalvão
ID: 41827794
There are good solutions here, so should be closed by accepting the Experts comments.
0
 
LVL 51

Expert Comment

by:Vitor Montalvão
ID: 41827796
Pawan, even your comment is right, ste5an's and Scott's also are.
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

628 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