문제

I need some help with a SQL view please..

I am creating a view to be used for reporting via Crysyal.. One of the fields that I need is a string field that contains three values separated by a character '~' I basically need this splitting out within my Select query within view to three separate fields..

An example is below.. The field is called 'Problem.Description' and contains the following example data..

'Trading~Concession~Telemetry - OCPD / Low Sales'

So, in my sql view I get a single column.. What I need is three columns each with different column names containing the data between the '~'..

For example:

Trading Status Problem1 Problem2 Trading Concession Telemetry - OCPD / Low Sales

I have a trawl around and found a few code examples that work but none will work within my view.

Many thanks in advance :)

도움이 되었습니까?

해결책

you mean split the string at the delimiter ?

declare @a as varchar(100)
set @a='Trading~Concession~Telemetry - OCPD / Low Sales'

select substring(@a,0,patindex('%~%',@a)) as trading_status,reverse(substring(reverse(@a),0,PATINDEX('%~%',(reverse(@a))))) as problem1,
substring(@a,patindex('%~%',@a)+1,10) as problem2

DEMO

Note:its not a dynamic solution but it works in your case

다른 팁

Try this:

select cast(c1 as nvarchar) + '~' + cast(c2 as nvarchar) + '~' + cast(c3 as nvarchar)
from table

You should be able to build such a splitting operation with a combination of SUBSTRING and INSTR. Your DBMS (Oracle, MySQL, SQL-Server?) could provide more convenient functions too. Check the respective documentation for "string functions".

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top