如何删除最后一个特殊字符

时间:2018-11-05 12:28:32

标签: sql sql-server

我有示例数据

Name
Mohankim@gmail.com
Mohankim@gmail.com.
ramkim@gmail.com.

我想要类似

的结果集
Name
Mohankim@gmail.com
ramkim@gmail.com

我的脚本

Select *,CASE WHEN Name = PATINDEX('%[.]%',Name) THEN LEFT(Name, LEN(Name) - 1) ELSE FQDN END T 
from mail

建议我

3 个答案:

答案 0 :(得分:3)

如果要删除尾随时间段:

select (case when name like '%.' then left(name, len(name) - 1) else name end) as name
from mail;

如果只需要结果名称的唯一列表:

select distinct (case when name like '%.' then left(name, len(name) - 1) else name end) as name
from mail;

答案 1 :(得分:1)

尝试一下

;WITH CTE(Name)
AS
(
SELECT 'Mohankim@gmail.com@'  UNION ALL
SELECT 'Mohankim@gmail.com#' UNION ALL
SELECT 'Mohankim@gmail.com$' UNION ALL
SELECT 'Mohankim@gmail.com' UNION ALL
SELECT 'ramkim@gmail.com.'
)
SELECT Name,IIF(PATINDEX('%[. , @ # $]%',REVERSE(Name))=1 ,LEFT(Name,LEN(Name)-1),Name)
 AS ReFormatName 
FRom CTE

结果

Name                ReqFormatName
----------------------------------------
Mohankim@gmail.com@ Mohankim@gmail.com
Mohankim@gmail.com# Mohankim@gmail.com
Mohankim@gmail.com$ Mohankim@gmail.com
Mohankim@gmail.com  Mohankim@gmail.com
ramkim@gmail.com.   ramkim@gmail.com

答案 2 :(得分:0)

DECLARE @VARI as VARCHAR(25)
SET @VARI='pradeep_sb@gmail.com.'
print @vari
select SUBSTRING(@VARI,LEN('pradeep_sb@gmail.com.'),1)


select
case
when '.'=SUBSTRING(@VARI,LEN('pradeep_sb@gmail.com.'),1)then SUBSTRING(@VARI,1,LEN(@vari)-1)else @VARI end as a