--1. 建立fn_split函式. ( 切分字串, 返回乙個列名為id的表 )
if exists(
select *
from dbo.sysobjects
where id = object_id('fn_split')
and (type = 'fn' or type = 'tf' or type = 'if')
)drop function fn_split
go
create function [dbo].[fn_split]
( @str varchar(max),
@separator varchar(10)
)returns table
as return
( --example: select id from fn_split('a,b,d,c',',')
select b.id
from (
(--a 的作用只是生成 'abd
c' 的xml格式的資料, 提供資料來源
select [value] = convert(xml, '' + replace(@str, @separator, '
') + '')
) a
(--b 的作用是將a中的 xml 資料的值列舉出來轉換成行
select id = n.v.value('.', 'varchar(100)') from a.[value].nodes('/v') n(v)
) b)
)go
/*
by 砸死牛頓的蘋果
qq 343774501
*/-- 輔助數字表(可單獨使用)
if object_id('getnums') is not null
drop function getnums
gocreate function [dbo].[getnums]
( @n int
)returns table
as return
with t1 as(
select 1 n union all select 1
),t2 as(
select 1 n
from t1,
t1 a,
t1 b,
t1 c
),t3 as(
select 1 n
from t2,
t2 a,
t2 b,
t2 c
),t4 as(
select 1 n
from t3,
t3 a
)select top(@n) row_number()over(
order by(
select 1
)) n
from t4
order by
ngo-- 字串分割函式
if object_id('fn_split') is not null
drop function fn_split
gocreate function fn_split
( @s nvarchar(max),
@split nvarchar(2)
)returns table
as return
with t0 as (
select substring(@s, n, 1)ch,n
from getnums(len(@s))
),t1 as(
select ch,
rid = n -row_number()over(order by n) + 1
from t0
where ch <> @split
) select rid,
keys = (
select '' + ch
from t1 b
where b.rid = a.rid for xml path('')
)from t1 a
group by
ridgo-- 舉例測試
select *
from fn_split('aaa,bb,cc,123', ',')
/*rid keys
1 aaa
2 bb
3 cc
4 123
*/
自定義實現strcmp,atoi,itoa函式
1.mystrcmp函式,字串比較 abc x abc abc ab aa 第乙個大於第二個則返回正數,相等返回0,第乙個小於第二個返回負數。include include intmystrcmp const char str1,const char str2 int tmp 0 while tmp...
Hibernate使用MySQL自定義函式
新建表 create table sys user id varchar 32 not null comment id name varchar 50 not null comment 登入名 parent id varchar 32 default null comment 父id primary...
Spark sparksql中使用自定義函式
中分別用物件導向和面向函式兩種寫法自定義了兩個函式 low2up 小寫轉大寫 up2low 大寫轉小寫 import org.apache.spark.sql.types.stringtype import org.apache.spark.sql.object sparksqlfunction s...