0

I have url like:

http://mysites.xyz.com/_layouts/ng/ActivityStream.aspx/id/2624D92223261D370D7287C9E83CAEEA/Activity%20Stream%20Post.

I need to get the 2624D92223261D370D7287C9E83CAEEA. But not able to do so. my current query is

 SUBSTRING(@URL2,LEN('http://mysites.xyz.com/_layouts/ng/ActivityStream.aspx/id/'),LEN(@URL2)-LEN('http://mysites.xyz.com/_layouts/ng/ActivityStream.aspx/id/')- CHARINDEX('/',REVERSE(@URL2))))

Please suggest.

6
  • 2
    What SQL type are you using? (Which database) eg. PSQL (PostgreSQL), MySQL, T-SQL (MS SQL Server), etc. Commented Jun 4, 2015 at 12:34
  • 1
    You could do it easily using any server side language like php or ruby and then pass the result to your SQL query. Commented Jun 4, 2015 at 12:37
  • 2
    Is it always /id/<some alpha-numerics>/Activity/..., or can it change? Commented Jun 4, 2015 at 12:37
  • By just going through the functions, I guessed its SQL Server! Commented Jun 4, 2015 at 12:38
  • yes zohar, the structure is always the same but the length of <some alpha-numerics> keeps on changing. Commented Jun 4, 2015 at 12:41

2 Answers 2

4

Try this:

SELECT SUBSTRING(
       @URL2, 
       CHARINDEX('/id/', @URL2)+4, 
       CHARINDEX('/', @URL2, CHARINDEX('/id/', @URL2)+5)
        - (CHARINDEX('/id/', @URL2)+4))

Note: This assumes that the id is always followed by at least one more slash.

Breakdown:
The first argument of the substring is the string that contains the full expression.
The second one is the first index after /id/. The third one is the desired length - calculated by the first index of / after /id/ - the first index after /id/.

update

To cope with strings that does not contain a slash after the id value, use case:

SELECT SUBSTRING(
       @URL, 
       CHARINDEX('/id/', @URL)+4, 
       CASE WHEN CHARINDEX('/', @URL, CHARINDEX('/id/', @URL)+5) > 0 THEN
       CHARINDEX('/', @URL, CHARINDEX('/id/', @URL)+5)
        - (CHARINDEX('/id/', @URL)+4)
       ELSE
          LEN(@URL)
       END
       )
Sign up to request clarification or add additional context in comments.

1 Comment

You get this error when a the url does not contain a slash after the id value. This is why I've asked you this question in the first place. I'll edit my question in a few minutes to cope with this new information.
0

If the structure is always the same then you can try:

SELECT REPLACE(RIGHT(@s, LEN(@s) - 58), '/Activity%20Stream%20Post', '')

or:

SELECT REPLACE(REPLACE(@s,'/Activity%20Stream%20Post', ''), 'http://mysites.xyz.com/_layouts/ng/ActivityStream.aspx/id/', '') 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.