zoukankan      html  css  js  c++  java
  • MSSQL移除字符串两边的指定字符

    移除字符串左边的字符:

    复制代码
    CREATE FUNCTION [dbo].[RemoveLeftChar] 
    (
    @Expression varchar(max),
    @char varchar(4)
    )
    RETURNS varchar(max)
    AS
    BEGIN
    WHILE LEN(@Expression)>0 AND CHARINDEX(@char,@Expression)=1
    BEGIN
    SET @Expression=SUBSTRING(@Expression,LEN(@char)+1,LEN(@Expression))
    END
    return @Expression
    END
    复制代码

    移除字符串右边的字符:

    复制代码
    CREATE FUNCTION [dbo].[RemoveRightChar] 
    (
    @Expression varchar(max),
    @char varchar(4)
    )
    RETURNS varchar(max)
    AS
    BEGIN
    DECLARE @Len int
    SET @Len=LEN(@Expression)
    WHILE @Len>0 AND SUBSTRING(@Expression,@Len-len(@char)+1,@Len)=@char
    BEGIN
    SET @Expression=SUBSTRING(@Expression,1,@Len-LEN(@char))
    SET @Len=LEN(@Expression)
    END
    return @Expression
    END
    复制代码

    移除字符串左边的字符:

    1、

    SELECT [dbo].[RemoveLeftChar]('|123|234|345|','|')
    --输出结果
    123|234|345|

    2、

    SELECT [dbo].[RemoveLeftChar]('|||123|234|345|','|')
    --输出结果
    123|234|345|

    移除字符串右边的字符:

    1、

    SELECT [dbo].[RemoveRightChar]('|123|234|345|','|')
    --输出结果
    |123|234|345

    2、

    SELECT [dbo].[RemoveRightChar]('|123|234|345|||','|')
    --输出结果
    |123|234|345

    移除两边的字符(组合)

    SELECT [dbo].[RemoveLeftChar]([dbo].[RemoveRightChar]('|123|234|345|','|'),'|')
    --输出结果
    123|234|345
  • 相关阅读:
    oracle执行.sql文件
    rematch的基本用法
    dva的基本用法
    redux-saga基本用法
    react-redux的基本用法
    redux的基本概念
    mobx基本概念
    centos 编译安装Apache 2.4
    javascript动态添加一组input
    php配置文件语法
  • 原文地址:https://www.cnblogs.com/qufly/p/7039472.html
Copyright © 2011-2022 走看看