我想修剪SQL中字符串中的所有特殊字符。我见过很多人使用子字符串方法删除一定数量的字符,但在这种情况下,字符串每一边的长度都是未知的。
任何人都知道怎么做?
答案 0 :(得分:1)
使用replace
功能会帮助您
我的意思是说当你想删除特殊字符时,使用替换函数
替换这个空格更多关于替换: http://technet.microsoft.com/en-us/library/ms186862.aspx
答案 1 :(得分:1)
在MS SQL中,这将删除所有加号:
SELECT REPLACE(theField, '+', '') FROM theTable
这是你需要的东西吗?
答案 2 :(得分:1)
我写了这个函数用于LEFT修剪任何字符
CREATE FUNCTION TRIMCHAR
(
-- Add the parameters for the function here
@str varchar(200) , @chartotrim varchar(1)
)
RETURNS varchar(200)
AS
BEGIN
DECLARE @temp varchar(2000)
SET @temp = @str
WHILE CHARINDEX ( @chartotrim , @temp ) =1
BEGIN
SET @temp = RIGHT( @temp , LEN(@temp)-1)
END
RETURN @temp
END
GO
答案 3 :(得分:0)
USE [YourDataBase]
GO
/****** Object: UserDefinedFunction [Accounts].[fn_CurrentFeeorArrears] Script Date: 02/18/2014 12:54:15 ******/
/*****Developed By rameez****/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE FUNCTION [FN_REMOVE_SPECIAL_CHARACTER]
(
@INPUT_STRING varchar(300))
RETURNS VARCHAR(300)
AS
BEGIN
--declare @testString varchar(100),
DECLARE @NEWSTRING VARCHAR(100)
-- set @teststring = '@ram?eez(ali)'
SET @NEWSTRING = @INPUT_STRING ;
With SPECIAL_CHARACTER as
(
--SELECT '>' as item
--UNION ALL
--SELECT '<' as item
--UNION ALL
--SELECT '(' as item
--UNION ALL
--SELECT ')' as item
--UNION ALL
--SELECT '!' as item
--UNION ALL
--SELECT '?' as item
--UNION ALL
--SELECT '@' as item
--UNION ALL
--SELECT '*' as item
--UNION ALL
--SELECT '%' as item
--UNION ALL
SELECT '$' as item
)
SELECT @NEWSTRING = Replace(@NEWSTRING, ITEM, '') FROM SPECIAL_CHARACTER
return @NEWSTRING
END
select dbo.[FN_REMOVE_SPECIAL_CHARACTER] ('r$@ameez')