Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
538 views
in Technique[技术] by (71.8m points)

sql server - is there a "split" function in t-sql for a SELECT query

I want to use my function like this

SELECT BaseSplit(line,';') FROM table

is there any way to do that ???

I try CROSS APPLY but it not what I want


My table has a column named line

line 
______
15;2;5
NULL
10;3;6

I want to split this column to be like

15
2
5
Null
10
3
6

Is there a string function that does this automatically?
If not, is it possible to write my own function?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

It should work fine, but you need to schema-qualify functions.

IE:

SELECT dbo.BaseSplit(line,';') FROM table

If your function is in a different schema than dbo you should obviously use that instead.

OK - assuming its a table valued function then...

SELECT t.Id, f.*  FROM table AS t CROSS APPLY dbo.BaseSplit(line,';') AS f

That would return a row for each split line + the ID of the that entry in the main table (assuming a column named Id exists in the main table). If you want better that that I'm going to need an example of what output you expect


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...