| Author |
Topic |
|
WJHamel
Aged Yak Warrior
651 Posts |
Posted - 2012-03-13 : 13:26:43
|
| So i want column B to be everything from column A which exists after the first space in Column A. It feels like it should be a substring, but something tells me charindex is involved. And that's as far as i'm getting.help? |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|
WJHamel
Aged Yak Warrior
651 Posts |
Posted - 2012-03-13 : 13:57:18
|
| [code]Column A Column BOne Red Dog Red DogFour Open Doors Open Doors[/code] |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|
WJHamel
Aged Yak Warrior
651 Posts |
Posted - 2012-03-13 : 14:00:05
|
| Column A is the sample data, column B is the output i'm looking for. |
 |
|
|
WJHamel
Aged Yak Warrior
651 Posts |
Posted - 2012-03-13 : 14:04:31
|
My shot in the dark looked like:UPDATE MNI_ADD SET street = Case when CHARINDEX(' ', address) > 0 then LEFT(address,CHARINDEX(' ', street)-1) else streetfrom dbo.MNI_ADDwhere column A is address and column B is street. |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|
WJHamel
Aged Yak Warrior
651 Posts |
Posted - 2012-03-13 : 14:16:34
|
| thanks. got it. i can see how to work that into my update. I'm just having trouble seeing or understanding excactly what the charindex function is doing. |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2012-03-13 : 14:20:58
|
| It's searching for the first occurrence of a space and outputs where it is at.Run this to see it:select charindex(' ', @s)Then you want ColumnB to start one character later, so I added 1 to that number:select charindex(' ', @s) + 1Now we want the substring to start at the above and go to the end (datalength) and thus arrive at the solution I posted.Hope this helps. We don't just write these out right away, we do them one step at a time and then combine them.Tara KizerMicrosoft MVP for Windows Server System - SQL Serverhttp://weblogs.sqlteam.com/tarad/Subscribe to my blog |
 |
|
|
WJHamel
Aged Yak Warrior
651 Posts |
Posted - 2012-03-13 : 14:23:46
|
| Thank you. 99% of the time just seeing the solution to a problem will allow me to see the mechanics behind it, but this was the other 1% for me for some reason. |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|
|