November 11, 2016 at 4:43 am
I get string values as input, e.g 'Documentation.Account', whatever value I get I want to extract it to get anything after a period(.) e.g'Account' I was thinking of using Substring, but I'm not sure how to go on about it.
November 11, 2016 at 4:59 am
What have you tried? If you show us that, we can show you where you're going wrong. Here's a hint: use CHARINDEX to find the position of the first "." and SUBSTRING (or RIGHT) to return everything after it. You may also need to use LEN to get the length of the string, and hence the number of characters after the ".".
John
November 11, 2016 at 5:29 am
hoseam (11/11/2016)
I get string values as input, e.g 'Documentation.Account', whatever value I get I want to extract it to get anything after a period(.) e.g'Account' I was thinking of using Substring, but I'm not sure how to go on about it.
This should get you started
😎
DECLARE @INPUT VARCHAR(50) = 'Documentation.Account';
SELECT
@INPUT AS INPUT_STRING
,SUBSTRING(@INPUT,CHARINDEX(CHAR(46),@INPUT,1) + 1,50) AS SUBSTRING_STRING
,STUFF(@INPUT,1,CHARINDEX(CHAR(46),@INPUT,1),'') AS STUFF_STRING
,RIGHT(@INPUT,CHARINDEX(CHAR(46),REVERSE(@INPUT)) - 1) AS RIGHT_STRING
;
Output
INPUT_STRING SUBSTRING_STRING STUFF_STRING RIGHT_STRING
------------------------ ------------------ -------------- -------------
Documentation.Account Account Account Account
November 11, 2016 at 12:44 pm
hoseam (11/11/2016)
I get string values as input, e.g 'Documentation.Account', whatever value I get I want to extract it to get anything after a period(.) e.g'Account' I was thinking of using Substring, but I'm not sure how to go on about it.
There are some limits but the following will work..
SELECT LeftPart = PARSENAME(somecolumn,2)
,RightPart = PARSENAME(somecolumn,1)
FROM dbo.sometable
--Jeff Moden
Change is inevitable... Change for the better is not.
November 12, 2016 at 4:30 am
Jeff Moden (11/11/2016)
hoseam (11/11/2016)
I get string values as input, e.g 'Documentation.Account', whatever value I get I want to extract it to get anything after a period(.) e.g'Account' I was thinking of using Substring, but I'm not sure how to go on about it.There are some limits but the following will work..
SELECT LeftPart = PARSENAME(somecolumn,2)
,RightPart = PARSENAME(somecolumn,1)
FROM dbo.sometable
Not to mention that PARSENAME is roughly 5 times faster than the other methods I posted earlier.
😎
Viewing 5 posts - 1 through 4 (of 4 total)
You must be logged in to reply to this topic. Login to reply