SQL分割字串

2021-04-23 06:45:14 字數 1069 閱讀 8415

if

object_id('

dbo.fn_split')

isnot

null

drop

function

dbo.fn_split

gocreate

function

dbo.fn_split

( @inputstr

varchar

(8000

), @seprator

varchar(10

) )

returns

@temp

table

(a varchar

(200

)) as

begin

declare

@iint

set@inputstr

=rtrim

(ltrim

(@inputstr

)) set@i=

charindex

(@seprator

, @inputstr

) while

@i>=

1begin

insert

@temp

values

(left

(@inputstr

, @i-1

)) set

@inputstr

=substring

(@inputstr

, @i+1

, len

(@inputstr) -

@i)

set@i

=charindex

(@seprator

, @inputstr

) end

if@inputstr

<>'/

'insert

@temp

values

(@inputstr

) return

endgo

select

*from

dbo.fn_split(

'10_20_30',

'_')

SQL分割字串

t sql對字串的處理能力比較弱,比如我要迴圈遍歷象1,2,3,4,5這樣的字串,如果用陣列的話,遍歷很簡單,但是t sql不支援陣列,所以處理下來比較麻煩。下邊的函式,實現了象陣列一樣去處理字串。一,用臨時表作為陣列 create function f split c varchar 2000 s...

SQL 字串分割函式

create function dbo f split sourcesql varchar 8000 strseprate varchar 10 returns temp table a varchar 100 as begin declare iint set sourcesql rtrim lt...

SQL分割字串儲存

create function dbo.fn pmt pis splitstr sourcesql varchar 8000 strseprate varchar 10 returns temp table customer int as begin declare i int set source...