Solved

Splitting  a field in SQL

Posted on 2016-09-16
9
28 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 17

Expert Comment

by:Pawan Kumar Khowal
Comment Utility
What you need as the output from the string, I shall write it for you
0
 
LVL 17

Accepted Solution

by:
Pawan Kumar Khowal earned 250 total points (awarded by participants)
Comment Utility
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 17

Expert Comment

by:Pawan Kumar Khowal
Comment Utility
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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 32

Assisted Solution

by:Stefan Hoffmann
Stefan Hoffmann earned 125 total points (awarded by participants)
Comment Utility
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)
Comment Utility
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 45

Expert Comment

by:Vitor Montalvão
Comment Utility
There are good solutions here, so should be closed by accepting the Experts comments.
0
 
LVL 45

Expert Comment

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

Featured Post

Free Gift Card with Acronis Backup Purchase!

Backup any data in any location: local and remote systems, physical and virtual servers, private and public clouds, Macs and PCs, tablets and mobile devices, & more! For limited time only, buy any Acronis backup products and get a FREE Amazon/Best Buy gift card worth up to $200!

Join & Write a Comment

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.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to shrink a transaction log file down to a reasonable size.

771 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

11 Experts available now in Live!

Get 1:1 Help Now