Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
SNandra
Starting Member
4 Posts |
Posted - 2008-02-25 : 04:31:25
|
| Hi I have a set of postcodes in mtable egCodesAB1 4ABABC1 4ABDE23 4ffB1 3RRB24 3RRI want to create a select query which just picks up the first part of the postcodes. Eg:AB1 ABC1 DE23 B1 B24 Any ideas? |
|
|
ayamas
Aged Yak Warrior
552 Posts |
Posted - 2008-02-25 : 04:47:39
|
| declare @str varchar(50)set @str ='AB1 4AB'select substring(@str,1,charindex(' ',@str)) |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-02-25 : 05:05:02
|
| or SELECT CASE WHEN CHARINDEX(' ',Codes) >0 THEN LEFT(Codes,CHARINDEX(' ',Codes)-1) ELSE Codes END |
 |
|
|
SNandra
Starting Member
4 Posts |
Posted - 2008-02-25 : 05:20:03
|
| CheersI went with this in the endSelect SUBSTRING(Codes, 1, CHARINDEX(' ', Codes)) as PartPostCodeFROM PostCodes |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2008-02-25 : 06:00:05
|
quote: Originally posted by SNandra CheersI went with this in the endSelect SUBSTRING(Codes, 1, CHARINDEX(' ', Codes)) as PartPostCodeFROM PostCodes
orSelect PARSENAME(REPLACE(codes,' ','.'),2) as PartPostCodeFROM PostCodesprovided codes has only two partsMadhivananFailing to plan is Planning to fail |
 |
|
|
SNandra
Starting Member
4 Posts |
Posted - 2008-02-25 : 06:04:08
|
Yes that works too. |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2008-02-25 : 06:06:48
|
quote: Originally posted by SNandra Yes that works too.
There can be N number of methods for string manipulation MadhivananFailing to plan is Planning to fail |
 |
|
|
|
|
|
|
|