我试图从SQL中定义的字符串输出中删除非标准字符,会有很多,所以我想使用case而不是替换但是我不能让它工作。有什么建议?
select distinct( CASE (SORT_CODE
+ cast(replicate('0',8-len(ACCOUNT_NUMBER))
+ ACCOUNT_NUMBER as char(8))
+ '0'
+ '17' --to be replaced by a check for a specific type
+ cast (replicate('0',11-len(replace(CURRENT_CHARGE_INCL,'.',''))) + replace(CURRENT_CHARGE_INCL,'.','') as char(11))
+ cast(left(LAST_NAME, 10) + replicate(' ',18) as char(18)) + upper(cast(TRADING_NAME + replicate(' ',18) as char( 18))) )
WHEN '.' THEN ' '
WHEN '&' THEN ' '
WHEN ',' THEN ' '
else DD_line
END)
as DD_LINE
答案 0 :(得分:1)
问题是你的陈述没有按照你的想法阅读。 您的陈述基本上是这样的:
SELECT DISTINCT CASE WHEN @string = '.' THEN ' '
WHEN @string = '&' THEN ' '
相反,在您注意到代码的潜在违规列/变量上运行CASE
语句。如果您拥有CREATE
权限,请使用UDF
对每个部分进行处理,或者将其正确写出。您的代码将更容易阅读。
不幸的是,你没有在声明中提供违规字符的位置,所以这样的例子就足够了:
select distinct( (SORT_CODE
+ cast(replicate('0',8-len(
CASE WHEN CHARINDEX('.', ACCOUNT_NUMBER) <> 0 THEN REPLACE(ACCOUNT_NUMBER, '.', ' ')
WHEN CHARINDEX('&', ACCOUNT_NUMBER) <> 0 THEN REPLACE(ACCOUNT_NUMBER, '.', ' ')
WHEN CHARINDEX(',', ACCOUNT_NUMBER) <> 0 THEN REPLACE(ACCOUNT_NUMBER, '.', ' ') END
)) )
+ CAST( CASE WHEN CHARINDEX('.', ACCOUNT_NUMBER) <> 0 THEN REPLACE(ACCOUNT_NUMBER, '.', ' ')
WHEN CHARINDEX('&', ACCOUNT_NUMBER) <> 0 THEN REPLACE(ACCOUNT_NUMBER, '&', ' ')
WHEN CHARINDEX(',', ACCOUNT_NUMBER) <> 0 THEN REPLACE(ACCOUNT_NUMBER, ',', ' ')
ELSE DD_LINE END as char(8))
)
)
复制并粘贴我.xD
答案 1 :(得分:0)
找到一个很好的方法来删除所有非Alpha和没有空格,可能对其他人有用。创建下面的函数然后使用它:dbo.RemoveNonAlphaCharacters(TRADING_NAME)
如果OBJECT_ID(N'dbo.RemoveNonAlphaCharacters',N'FN')不是NULL DROP FUNCTION RemoveNonAlphaCharacters;
go
创建函数[dbo]。[RemoveNonAlphaCharacters](@ Temp VarChar(1000)) 返回VarChar(1000) 如 开始
Declare @KeepValues as varchar(50)
Set @KeepValues = '%[^a-z ]%'
While PatIndex(@KeepValues, @Temp) > 0
Set @Temp = Stuff(@Temp, PatIndex(@KeepValues, @Temp), 1, '')
Return @Temp
结束
去
信用,G Mastros