需要一个sql函数,功能如下
+-----+------+
| id | ct |
+-----+------+
| 1 | cea |
+-----+------+
| 3 | ebb |
+-----+------+
| 4 | bgg |
+-----+------+
select id from [table] where doit(ct) like '%abc%' -- 结果 是 1
----------------------------------------
这个doit 的SQL Function 的功能是,(以第一条记录为查到记录说明)1.倒序 cea 变成 aec2.替换 把 e 替换成 bSQL符合查询
-------------------------------------------求这个 doit 怎么写
declare @T table (id varchar(14),ct varchar(3))
insert into @T
select '1','cea' union all
select '3','ebb' union all
select '4','bgg'SELECT COUNT(1)
FROM @T
WHERE REPLACE(REVERSE(ct), 'e', 'b') LIKE '%abc%'
/*
1
*/
insert into tb select 1,'cea'
insert into tb select 2,'ebb'
insert into tb select 3,'bgg'
go
create function doit
(@ct nvarchar(10))
returns varchar(10)
as
begin
return replace(reverse(@ct),'e','b')
end
go
select id from tb where dbo.doit(ct) like '%abc%'
id
-----------
1(1 行受影响)
go
drop table tb
drop function dbo.doit
@P VARCHAR(100)
)
RETURNS VARCHAR(100)
AS
BEGIN
RETURN REPLACE(
REVERSE(@P) -- 1.倒序 cea 变成 aec
,'e','b') -- 2.替换 把 e 替换成 b
END
GO
--调用
SELECT DBO.doit('cea')
-- 结果
abc
如果替换太多了,感觉你业务就有问题了!
if object_id('tb','U') is not null
drop table tb
go
create table tb
(
id int,
ct varchar(10)
)
go
insert into tb
select 1,'cea' union all
select 2,'ebb' union all
select 3,'bgg'
go
select * from tb where replace(reverse(ct),'e','b') like '%abc%'
/*
id ct
----------- ----------
1 cea(1 行受影响)
*/