SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Get 1st Word in the string


Get 1st Word in the string

Author
Message
Eirikur Eiriksson
Eirikur Eiriksson
SSCoach
SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)

Group: General Forum Members
Points: 15124 Visits: 18599
A simple solution
Cool
with p as
(
select 'asdf1234' as Delivery_Site union all
select 'asdf 1234' union all
select ' asdf asdf ' union all
select ''
)
,TXT_INFO AS
(
SELECT
LTRIM(PP.Delivery_Site) AS Delivery_Site
,CHARINDEX(' ',PP.Delivery_Site) AS HAS_SPACE
,LEN(LTRIM(PP.Delivery_Site)) AS HAS_CHAR
FROM p PP
)
SELECT
CASE
WHEN TI.HAS_CHAR > 0 AND TI.HAS_SPACE > 0 THEN SUBSTRING(TI.Delivery_Site,1,CHARINDEX(' ',TI.Delivery_Site))
WHEN TI.HAS_CHAR > 0 AND TI.HAS_SPACE = 0 THEN TI.Delivery_Site
ELSE NULL
END AS FIRST_WORD
FROM TXT_INFO TI;



Results
FIRST_WORD
-----------
asdf1234
asdf
asdf
NULL

Sean Lange
Sean Lange
One Orange Chip
One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)

Group: General Forum Members
Points: 26119 Visits: 17538
Eirikur Eiriksson (5/1/2014)
A simple solution
Cool
with p as
(
select 'asdf1234' as Delivery_Site union all
select 'asdf 1234' union all
select ' asdf asdf ' union all
select ''
)
,TXT_INFO AS
(
SELECT
LTRIM(PP.Delivery_Site) AS Delivery_Site
,CHARINDEX(' ',PP.Delivery_Site) AS HAS_SPACE
,LEN(LTRIM(PP.Delivery_Site)) AS HAS_CHAR
FROM p PP
)
SELECT
CASE
WHEN TI.HAS_CHAR > 0 AND TI.HAS_SPACE > 0 THEN SUBSTRING(TI.Delivery_Site,1,CHARINDEX(' ',TI.Delivery_Site))
WHEN TI.HAS_CHAR > 0 AND TI.HAS_SPACE = 0 THEN TI.Delivery_Site
ELSE NULL
END AS FIRST_WORD
FROM TXT_INFO TI;



Results
FIRST_WORD
-----------
asdf1234
asdf
asdf
NULL


Nice! I tweaked it one step further to make it a little simpler.


with p as
(
SELECT 'asdf12345678901' AS Delivery_Site UNION
SELECT 'asd f 234 ' UNION
SELECT ' asdf 345 ' UNION
SELECT ' asdf 234567' union all
select ' ' union all
select ''
)
,TXT_INFO AS
(
SELECT
LTRIM(PP.Delivery_Site) AS Delivery_Site
,CHARINDEX(' ', ltrim(PP.Delivery_Site)) AS HAS_SPACE
FROM p PP
)
SELECT *,
CASE
WHEN TI.HAS_SPACE > 0 THEN SUBSTRING(TI.Delivery_Site, 1, HAS_SPACE)
else nullif(TI.Delivery_Site, '')
END AS FIRST_WORD
FROM TXT_INFO TI;



_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Modens splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search