Content Entry

从字符串中删除不需要的字符[SQL]

Published: 2007-12-21 Categories: Tech Tags: Replace Translate SQL DB2 MySQL SQL Server Oracle PostgreSQL

O'Reilly , SQL Cookbook
by Antbony Molinaro

解决方案:replace(), translate()

DB2
select ename,
replace(translate(ename, 'aaaa', 'AEIOU'), 'a', '') stripped1,
sal,
replace(cast(sal as char(4)), '0', '') stripped2
from emp

MySQL, SQL Server : 不支持 translate()
select ename
replace(replace(replace(replace(replace(ename, 'A', ''), 'E', ''), 'I', ''), 'O', ''), 'U', '')
as stripped1,
sal,
replace(sal, 0, '') stripped2
from emp

Oracle, PostgreSQL
select ename
replace(translate(ename, 'AEIOU', 'aaaaa'), 'a')
as stripped1,
sal,
replace(sal, 0, '') as stripped2
from emp

hooto.com的blog留言在经历几次迁移后,一直存在一个问题,留言人出现'username(游客)(游客)'字样,遗留问题,以前是将guest标记直接添加到username,新的版本则根据user_id是否为有效值判断,所以... 一直懒得没处理,现在好了,一句sql就行,之前对sql的认知太少了,需要练习。

fixed (MySQL 4.1)

update blog_comments
set username = replace(replace(replace(replace(username, '(', ''), '游', ''), '客', ''), ')', '')

comments loading