Solved

Splitting  a field in SQL

Posted on 2016-09-16
9
31 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
9 Comments
 
LVL 24

Expert Comment

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

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 24

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
Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

 
LVL 33

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:ScottPletcher
ScottPletcher 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 46

Expert Comment

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

Expert Comment

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

Featured Post

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
grouping logic 6 49
Get Duration of last Status Update 4 31
T-SQL: Nested CASE Statements 4 24
SQL Select - checking duplicate across multiple columns 3 11
In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
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.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

920 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now