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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Spliting Column Help

Status
Not open for further replies.

mattm31

Technical User
Mar 9, 2005
79
0
0
GB
I think this is quite straight forward
but i still can't figure it out.

the first thing i want to do is copy
everything before the first space
from Field1 to Field2
and then everything before the first ,
from field1 to field3

ie

Field1
The Dogs, Nuts

Field2
The

Field3
The Dogs
 
You should be able to use CHARINDEX in conjuntion with SUBSTRING to achieve this. You can find info in BOL.
 
EXAMPLE

Code:
[COLOR=blue]create[/color] [COLOR=blue]table[/color] blah(Column1 [COLOR=blue]varchar[/color](666))
[COLOR=blue]insert[/color] blah [COLOR=blue]values[/color] ([COLOR=red]'The Dogs, Nuts'[/color])

[COLOR=blue]select[/color] Column1,[COLOR=#FF00FF]left[/color](Column1,([COLOR=#FF00FF]CHARINDEX[/color]([COLOR=red]' '[/color],Column1))) Column2,
[COLOR=#FF00FF]left[/color](Column1,([COLOR=#FF00FF]CHARINDEX[/color]([COLOR=red]','[/color],Column1))-1) Column3
[COLOR=blue]from[/color] blah

Denis The SQL Menace
--------------------
SQL Server Code,Tips and Tricks, Performance Tuning
Google Interview Questions





 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top