根據分隔符的位置獲取值

2022-04-01 16:17:49 字數 1779 閱讀 4413

go

ifexists(select

1from sysobjects where id=

object_id('

fn_getvaluebyseppos

') and xtype='fn

')begin

drop

function

dbo.fn_getvaluebyseppos

endgo

--功能:根據分隔符的位置獲取值

--引數:@string 輸入的字串;@sep 分隔符(預設為逗號);@pos 分隔符的位置(預設為第乙個分隔符);@beforeorafter (0 表示前面,1表示後面)取指定位置分隔符前面(後面)的字串(預設取分隔符前面的字串)

create

function

dbo.fn_getvaluebyseppos

(

@string

varchar(max

),

@sep

varchar(10

),

@pos

int,

@beforeorafter

int)

returns

varchar(max)as

begin

declare

@result

varchar(max

)

select

@result

='',@string

=isnull(@string,''),@sep

=isnull(@sep,'

,'),@pos

=isnull(@pos,1),@beforeorafter

=isnull(@beforeorafter,0

)

if@string=''

begin

return

@result

enddeclare

@temp

table

( rn

intidentity(1,1

), value

varchar(max

) )

insert

into

@temp

( value )

select

value

from dbo.fn_split1(@string,@sep

)

if@beforeorafter=0

begin

--前面

select

@result

=stuff((select

top (@pos) @sep

+ value from

@temp

order

by rn asc

for xml path('')),1,1,''

)

endelse

begin

--後面

select

@result

=stuff((select

top (@pos) @sep

+ value from

@temp

order

by rn desc

for xml path('')),1,1,''

)

endreturn

@result

endgo

java 根據系統獲取檔案分隔符

在linux系統上是 在windows系統上是 考慮到跨平台,應盡量使用file.separator。根據系統 獲取 string str file.separator 引用 file類有幾個類似separator的靜態字段,都是與系統相關的,在程式設計時應盡量使用。separatorchar pu...

Oracle 根據分隔符分隔字串

為了讓pl sql 函式返回資料的多個行 必須通過返回乙個 ref cursor 或乙個資料集合來完成 ref cursor 的這種情況侷限於可以從查詢中選擇的資料 而整個集合在可以返回前 必須進行具體化 oracle 9i 通過引入的管道化表函式糾正了後一種情況 表函式是返回整個行的集 通常作為乙...

Hive的列分隔符和行分隔符

在建立hive表時,預設行分隔符 a 列分隔符 n 這兩項也是可以設定的。在實際開發中,一般預設使用預設的分隔符,當然有些場景下也會自定義分隔符。spark hive use test db 建立外部表 create external table test tb user id bigint com...