Get everything after and before certain character in SQL Server

前端 未结 15 1907
臣服心动
臣服心动 2020-12-07 18:48

I got the following entry in my database:

images/test.jpg

I want to trim the entry so I get: test

So basically, I want

相关标签:
15条回答
  • 2020-12-07 19:25

    Inspired by the work of Josien, I wondered about a simplification.

    Would this also work? Much shorter:

    SELECT SUBSTRING(col, CHARINDEX ('/', col) + 1, CHARINDEX ('.', col) - CHARINDEX ('/', col) - 1);
    

    (I can't test right now because of right issues at my company SQL server, which is a problem in its own right)

    0 讨论(0)
  • 2020-12-07 19:28
     declare @T table
      (
      Col varchar(20)
      )
    
    
    
      insert into @T 
      Select 'images/test1.jpg'
      union all
      Select 'images/test2.png'
      union all
      Select 'images/test3.jpg'
      union all
      Select 'images/test4.jpeg'
      union all
      Select 'images/test5.jpeg'
    
     Select substring( LEFT(Col,charindex('.',Col)-1),charindex('/',Col)+1,len(LEFT(Col,charindex('.',Col)-1))-1 )
    from @T
    
    0 讨论(0)
  • 2020-12-07 19:28

    I just did this in one of my reports and it was very simple.

    Try this:

    =MID(Fields!.Value,8,4)
    

    Note: This worked for me because the value I was trying to get was a constant not sure it what you are trying to get is a constant as well.

    0 讨论(0)
提交回复
热议问题