Re: Return Part of a text field
- From: Hugo Kornelis <hugo@xxxxxxxxxxxxxxxxxxxxxx>
- Date: Thu, 30 Jun 2005 11:30:30 +0200
On Thu, 23 Jun 2005 09:10:01 -0700, tdom wrote:
>I have a table with a field named "swNotes". The field type is ntext. The
>field can contain a variety of data. What I need to do is pull out just one
>particular piece. Here is an example of 4 different rows of data in the
>"swNotes" field:
>
>--> Color = Green <-- --> Number = 1 <-- --> Notes = Today is Thursday. <--
>--> Number = 25 <-- --> Notes = Hello <--
>--> Weather = Hot <-- --> Notes = I need help. <-- --> Number = 567 <--
>--> Color = Green <-- --> Notes = No number in this row. <--
>
>My goal is to retrieve "Number = xxx".
>So my returned data should be:
>
>Number = 1
>Number = 25
>Number = 567
>Null
>
>Since "Number = " is not in the same position each time substring does not
>work. And in some cases the "Number = xxx" is not provided.
>
>Using Query Analyzer how can I complete this task?
>
>Thanks.
Hi tdom,
You can use SUBSTRING and POSINDEX functions. However, that will only
work if the "--> Number = ?? <--" part is somewhere in the first 4000
characters of the text.
create table test (swNotes ntext)
go
insert test
select '--> Color = Green <-- --> Number = 1 <-- --> Notes = Today is
Thursday. <--'
union all
select '--> Number = 25 <-- --> Notes = Hello <--'
union all
select '--> Weather = Hot <-- --> Notes = I need help. <-- --> Number
= 567 <--'
union all
select '--> Color = Green <-- --> Notes = No number in this row. <--'
go
SELECT CASE
WHEN swNotes LIKE '%--> Number = % <--%'
THEN SUBSTRING(swNotes,
CHARINDEX('--> Number =', swNotes) + 4,
CHARINDEX(' <--',
SUBSTRING(swNotes,
CHARINDEX('--> Number =',
swNotes) + 4,
4000)) - 1)
ELSE NULL
END
FROM test
go
drop table test
go
Best, Hugo
--
(Remove _NO_ and _SPAM_ to get my e-mail address)
.
- Follow-Ups:
- Re: Return Part of a text field
- From: tdom
- Re: Return Part of a text field
- References:
- Return Part of a text field
- From: tdom
- Return Part of a text field
- Prev by Date: Re: select most common value
- Next by Date: Re: Return Part of a text field
- Previous by thread: Return Part of a text field
- Next by thread: Re: Return Part of a text field
- Index(es):
Relevant Pages
|
|