Get everything after and before certain character in SQL Server

前端 未结 15 1905
臣服心动
臣服心动 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:08

    Simply Try With LEFT ,RIGHT ,CHARINDEX

    select 
    LEFT((RIGHT(a.name,((CHARINDEX('/', name))+1))),((CHARINDEX('.', (RIGHT(a.name, 
                         ((CHARINDEX('/', name))+1)))))-1)) splitstring,
    a.name      
    from 
       (select 'images/test.jpg' as name)a
    
    0 讨论(0)
  • 2020-12-07 19:09

    You can try this:

    Declare @test varchar(100)='images/test.jpg'
    Select REPLACE(RIGHT(@test,charindex('/',reverse(@test))-1),'.jpg','')
    
    0 讨论(0)
  • 2020-12-07 19:10

    Before

    SELECT SUBSTRING(ParentBGBU,0,CHARINDEX('/',ParentBGBU,0)) FROM dbo.tblHCMMaster;
    

    After

    SELECT SUBSTRING(ParentBGBU,CHARINDEX('-',ParentBGBU)+1,LEN(ParentBGBU)) FROM dbo.tblHCMMaster
    
    0 讨论(0)
  • 2020-12-07 19:12

    I found Royi Namir's answer useful but expanded upon it to create it as a function. I renamed the variables to what made sense to me but you can translate them back easily enough, if desired.

    Also, the code in Royi's answer already handled the case where the character being searched from does not exist (it starts from the beginning of the string), but I wanted to also handle cases where the character that is being searched to does not exist.

    In that case it acts in a similar manner by starting from the searched from character and returning the rest of the characters to the end of the string.

    CREATE FUNCTION [dbo].[getValueBetweenTwoStrings](@inputString 
    NVARCHAR(4000), @stringToSearchFrom NVARCHAR(4000), @stringToSearchTo 
    NVARCHAR(4000))
    RETURNS NVARCHAR(4000)
    AS
    BEGIN      
    DECLARE @retVal NVARCHAR(4000)
    DECLARE @stringToSearchFromSearchPattern NVARCHAR(4000) = '%' + 
    @stringToSearchFrom + '%'
    
    SELECT @retVal = SUBSTRING (
           @inputString,
           PATINDEX(@stringToSearchFromSearchPattern, @inputString) + LEN(@stringToSearchFrom),
           (CASE
                CHARINDEX(
                    @stringToSearchTo,
                    @inputString,
                    PATINDEX(@stringToSearchFromSearchPattern, @inputString) + LEN(@stringToSearchFrom))
            WHEN
                0
            THEN
                LEN(@inputString) + 1
            ELSE
                CHARINDEX(
                    @stringToSearchTo,
                    @inputString,
                    PATINDEX(@stringToSearchFromSearchPattern, @inputString) + LEN(@stringToSearchFrom))
            END) - (PATINDEX(@stringToSearchFromSearchPattern, @inputString) + LEN(@stringToSearchFrom))
       )
    RETURN @retVal
    END
    

    Usage:

    SELECT dbo.getValueBetweenTwoStrings('images/test.jpg','/','.') AS MyResult
    
    0 讨论(0)
  • 2020-12-07 19:18

    Below query gives you data before '-' Ex- W12345A-4S

    SELECT SUBSTRING(Column_Name,0, CHARINDEX('-',Column_Name))  as 'new_name'
    from [abc].
    

    Output - W12345A

    0 讨论(0)
  • 2020-12-07 19:19

    use the following function

    left(@test, charindex('/', @test) - 1)
    
    0 讨论(0)
提交回复
热议问题