all groups > sql server programming > december 2004 >
You're in the

sql server programming

group:

only the first word


Re: only the first word Adam Machanic
12/7/2004 9:13:08 PM
sql server programming: SELECT SUBSTRING(YourCol, 1, CHARINDEX(' ', YourCol) - 1) AS FirstWord
FROM YourTable

--
Adam Machanic
SQL Server MVP
http://www.sqljunkies.com/weblog/amachanic
--


[quoted text, click to view]

Re: only the first word David Gugick
12/7/2004 9:15:56 PM
[quoted text, click to view]

Maybe something like this.
- Trim the value to remove leading spaces
- Look for the first space
- If there is no space, then convert the 0 from charindex to a null
using NULLIF
- Use COALESCE to return the value of the first non null value (either
the first space if it exists or the length of the string)
- Select the LEFT characters of the return value

Declare @n nvarchar(100)
set @n = N' Where is the cat'
Select LEFT(ltrim(@n), COALESCE(NULLIF(charindex(SPACE(1), ltrim(@n)),
0) - 1, len(ltrim(@n))))
set @n = N'Thisisacat'
Select LEFT(ltrim(@n), COALESCE(NULLIF(charindex(SPACE(1), ltrim(@n)),
0) - 1, len(ltrim(@n))))
set @n = N'This is a cat'
Select LEFT(ltrim(@n), COALESCE(NULLIF(charindex(SPACE(1), ltrim(@n)),
0) - 1, len(ltrim(@n))))
set @n = N'Cats are for people too'
Select LEFT(ltrim(@n), COALESCE(NULLIF(charindex(SPACE(1), ltrim(@n)),
0) - 1, len(ltrim(@n))))



-----------
Where

----------------
Thisisacat

--------
This

--------
Cats

--
David Gugick
Imceda Software
www.imceda.com
only the first word alejandro
12/7/2004 11:35:31 PM
How can i separe only the first word of a nvarchar field??

Sample:

Big barrel
Small cat
little dog

i want this return :
big
small
little
only the first word of the field, i have try with LEFT, but this function
only works with N letters,

Thanks in advance
Alejandro Carnero.

AddThis Social Bookmark Button