Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

find first space on text field

Status
Not open for further replies.

spiff2002

IS-IT--Management
Jan 31, 2003
40
I need to write a query that will split a text field after the first space found, i.e:
if the contents of the text field is 'red small car 4 door' the query will return 'small car 4 door'
thanks in adavance for any help
 
is the datatype a text field or varchar?

- Paul
- If at first you don't succeed, find out if the loser gets anything.
 
Provided it is a varchar, try running these two queries. It should explain things to you. For more info look up CHARINDEX, LEFT, and RIGHT functions in Books online.

If it is text datatype, you will not be able to do this unfortunately.

Code:
select 'red small car 4 door' 

select right('red small car 4 door', len('red small car 4 door') - charindex(' ', 'red small car 4 door' ))

Hope it helpsm

Alex

Ignorance of certain subjects is a great part of wisdom
 
This will work if it is a Char or Varchar datatype.

Code:
SELECT SUBSTRING('red small car 4 door',CHARINDEX(' ','red small car 4 door'),LEN('red small car 4 door'))

- Paul
- If at first you don't succeed, find out if the loser gets anything.
 
Can't you convert the text to a char or varchar??

[small]"Mom........MEATLOAF!!!! F***!!!!"[/small]
<.
 
Here's a converted example (I know it's not text, but it does show a convert)
(tested)
:
Code:
select ltrim(rtrim(substring(convert(varchar(50), 'red small car 4 door'), charindex(' ', convert(varchar(100), 'red small car 4 door')), 50)))

[small]"Mom........MEATLOAF!!!! F***!!!!"[/small]
<.
 
>>Can't you convert the text to a char or varchar??

But what if the column datatype is TEXT?

won't it get truncated if you try to stuff 5000 characters into a varchar() field.

- Paul
- If at first you don't succeed, find out if the loser gets anything.
 
I did test that, and that's what I meant column datatype is text.

varchar can hold 8000 and I that would only be an issue if spiff2002's text field data is large enough.

On a side note, I like Alex's query above cause it doesn't use that dirty substring method like mine did.

If that column is a text field, combine Alex and mine's query for Mighty Morphin Query Ranger Number 1.

[pipe] <--- Not tobacco



[small]"Mom........MEATLOAF!!!! F***!!!!"[/small]
<.
 
sorry my bad
it is a varchar field not a text field.
I used ptheriault code and it worked like a charm thank you all.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top