Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12»»

Get the substring after second white space Expand / Collapse
Author
Message
Posted Tuesday, February 21, 2012 2:28 AM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Sunday, February 24, 2013 1:33 AM
Points: 83, Visits: 167
I have a requirement here. There is a string '0005 ganeshkumar A999'. I want the last four character 'A999' as output where it is not necessarily a four charactes only. It could be 3 to 6 characters also.

Thanks in advance for your any help coming here, as always.

Regards,
Ganesh.
Post #1255069
Posted Tuesday, February 21, 2012 2:44 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Yesterday @ 3:05 AM
Points: 1,400, Visits: 6,896
ganeshkumar005 (2/21/2012)
I have a requirement here. There is a string '0005 ganeshkumar A999'. I want the last four character 'A999' as output where it is not necessarily a four charactes only. It could be 3 to 6 characters also.

Thanks in advance for your any help coming here, as always.

Regards,
Ganesh.


I've assumed you always want the characters after the last space (the title suggest after the second space, but what if there are more than two?).
I find it easier to reverse the string and look for the first space instead, and then reverse it back. Others may have a more elegant method, but have a play with this code:

--DECLARE @String VarChar(MAX) = '0005 ganeshkumar A999';
--DECLARE @String VarChar(MAX) = 'first second third fourth';
DECLARE @String VarChar(MAX) = 'first second';

DECLARE @EndString VarChar(MAX);

SET @EndString = LTRIM(REVERSE(LEFT(REVERSE(@String),PATINDEX('% %',@String))));

SELECT @EndString



BrainDonor
Linkedin
Post #1255074
Posted Tuesday, February 21, 2012 3:26 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Yesterday @ 10:39 AM
Points: 2,556, Visits: 4,398
another way:

select parsename(replace('0005 ganeshkumar A999',' ','.'),1)



_____________________________________________
"The only true wisdom is in knowing you know nothing"
"O skol'ko nam otkrytiy chudnyh prevnosit microsofta duh!"
(So many miracle inventions provided by MS to us...)

How to post your question to get the best and quick help
Post #1255089
Posted Tuesday, February 21, 2012 5:27 AM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Sunday, February 24, 2013 1:33 AM
Points: 83, Visits: 167
Thanks to both of you. This works fine.
Post #1255183
Posted Tuesday, February 12, 2013 2:21 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Wednesday, February 13, 2013 2:51 PM
Points: 8, Visits: 20
Is there a way to get the first two? For instance I need to pull out 'aabbbb' from 'aa bbbb c ddd ee'. The lengths and the number of spaces will always vary.
Post #1419187
Posted Tuesday, February 12, 2013 2:38 PM


SSCrazy Eights

SSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy Eights

Group: General Forum Members
Last Login: Yesterday @ 1:17 PM
Points: 8,641, Visits: 8,273
newbie2 (2/12/2013)
Is there a way to get the first two? For instance I need to pull out 'aabbbb' from 'aa bbbb c ddd ee'. The lengths and the number of spaces will always vary.


Yes look at what Eugene posted. PARSENAME will do this quite nicely.

http://msdn.microsoft.com/en-us/library/ms188006.aspx


_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Post #1419196
Posted Tuesday, February 12, 2013 2:52 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Wednesday, February 13, 2013 2:51 PM
Points: 8, Visits: 20
I tried that, but it does not seem to work when the amount of spaces vary, for example one column may be '1 north main st' and I need to return '1north', but the next record could be '12 main steet' and I need to return '12main'.
Post #1419205
Posted Tuesday, February 12, 2013 3:40 PM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Yesterday @ 10:03 PM
Points: 284, Visits: 1,248
newbie2 (2/12/2013)
I tried that, but it does not seem to work when the amount of spaces vary, for example one column may be '1 north main st' and I need to return '1north', but the next record could be '12 main steet' and I need to return '12main'.

The fastest method is probably using DelimitedSplit8K to parse the string, then a PIVOT table to turn the rows into columns so you can do the concatenation. If you don't have the function DelimitedSplit8K just do a search on this site and you'll find it.


--first some sample data

IF OBJECT_ID('tempdb..#TempTable') IS NOT NULL
DROP TABLE #TempTable

CREATE TABLE #TempTable (
[ID] INT IDENTITY(1,1) NOT NULL,
[Address] NVARCHAR(50) NULL,
PRIMARY KEY (ID))

INSERT INTO #TempTable
SELECT '1 north main st'
UNION
SELECT '12 main steet'



SELECT
ID
,[1]+[2] AS Street
FROM
(
SELECT
tt.ID
,dsk.ItemNumber
,dsk.Item
FROM
#TempTable AS tt
CROSS APPLY
dbo.DelimitedSplit8K(tt.Address,' ') AS dsk
WHERE
ItemNumber IN (1,2)
) AS src
PIVOT (MAX(Item) FOR ItemNumber IN ([1],[2])) AS pvt


Post #1419225
Posted Tuesday, February 12, 2013 3:51 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Wednesday, February 13, 2013 2:51 PM
Points: 8, Visits: 20
Thanks for the quick response, but that's a little more complicated than I was hoping for. I thought maybe using CHARINDEX and SUBSTRING might do it.
Post #1419229
Posted Tuesday, February 12, 2013 6:27 PM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Yesterday @ 7:24 PM
Points: 2,346, Visits: 3,192
Perhaps something like this?

WITH MyData ([address]) AS (
SELECT '1 north main st'
UNION ALL SELECT '12 main street'
UNION ALL SELECT '14 clover avenue')
SELECT [address], [address1], [address2]=LEFT([address2], CHARINDEX(' ', [address2])-1)
FROM MyData
-- Remove extra white space
CROSS APPLY (
SELECT REPLACE(
REPLACE(
REPLACE(address, ' ', ' ' + CHAR(7))
,CHAR(7) + ' ', CHAR(7))
,CHAR(7), '')) a([address1])
CROSS APPLY (
SELECT STUFF([address1], CHARINDEX(' ', [address1]), 1, '')) b([address2])





No loops! No CURSORs! No RBAR! Hoo-uh!

INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?

Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Are you too recursively challenged?
Splitting strings based on patterns can be fast!
Post #1419264
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse