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 |
|
s_anr
Yak Posting Veteran
81 Posts |
Posted - 2009-11-26 : 10:16:10
|
Hi i have a field with following value :USE TESTTABLEGOCREATE TABLE Example (Description varchar(500))GOINSERT INTO [TESTTABLE].[dbo].[Example] ([Description])VALUES ('hello This is a test query. Thank you.Please be advised of the following :Name: Doe,JohnID: 123456Preferred Name: NoneType: My typeClass: My ClassLast Day of Year: 31-Dec-2009Domain: MYDomainUsername: My UsernameEmail: my@email.comContact: Jane,Mary')GOINSERT INTO [TESTTABLE].[dbo].[Example] ([Description])VALUES ('hello This is a test query. Thank you.Please be advised of the following :Name:Doe, JohnID: 123456 Preferred Name: NoneType: My typeClass: My ClassLast Day of Year: 31-Dec-2009Domain: MYDomainUsername: My UsernameEmail: my@email.comContact: Jane,Mary')GOINSERT INTO [TESTTABLE].[dbo].[Example] ([Description])VALUES ('hello This is a test query. Thank you.Please be advised of the following :Name: Doe,John ID: 123456 Preferred Name: NoneType: My typeClass: My ClassLast Day of Year: 31-Dec-2009Domain: MYDomainUsername: My UsernameEmail: my@email.comContact: Jane,Mary')GOI want to extract three things from the above data stored ina single field1. LAST NAME (All characters before COMMA till a special character is met)2. FIRST NAME (All characters AFTER COMMA till a special character is met)2. ID (All numbers after the word ID. Ignore all special characters before and after the numbers)The Output will be :FIRST NAME LAST NAME IDJohn Doe 123456John Doe 123456John Doe 123456 Please help how can I use substrings to extract this data in SQL 2005 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2009-11-27 : 01:39:55
|
| http://sqlblogcasts.com/blogs/madhivanan/archive/2009/11/18/parsing-a-string.aspxMadhivananFailing to plan is Planning to fail |
 |
|
|
s_anr
Yak Posting Veteran
81 Posts |
Posted - 2009-11-27 : 02:58:06
|
| The blog was helpdful. Thank you so much. |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2009-11-27 : 04:00:25
|
quote: Originally posted by s_anr The blog was helpdful. Thank you so much.
You are welcome MadhivananFailing to plan is Planning to fail |
 |
|
|
april198474
Starting Member
11 Posts |
Posted - 2009-11-27 : 05:28:58
|
| Charindex will help you, try the following code:declare @begin1 int, @end1 int, @begin2 int, @end2 int, @begin3 int, @end3 intdeclare @des varchar(500), @FirstName varchar(20), @LastName varchar(20), @ID varchar(20)USE TESTTABLEGOcreate table #Temp( FirstName varchar(20), LastName varchar(20), ID varchar(20))declare cursorDes cursor for(select [description] from [TESTTABLE].[dbo].[Example])open cursorDesfetch next from cursorDes into @Deswhile @@fetch_status = 0begin set @begin1 = (select top 1 charindex('Name:', @des)) + 5 set @end1 = (select top 1 charindex(',', @des) ) - @begin1 set @begin2 = @end1 + @begin1 + 1 set @end2 = (select top 1 charindex('ID:', @des)) - @begin2 set @begin3 = (select top 1 charindex('ID:', @des)) + 3 set @end3 = (select top 1 charindex('Preferred Name:', @des)) - @begin3 set @FirstName = (select rtrim(ltrim(substring( @des, @begin1, @end1)))) set @LastName = (select rtrim(ltrim(substring( @des, @begin2, @end2)))) set @ID = (select rtrim(ltrim(substring( @des, @begin3, @end3)))) insert into #Temp([Lastname], [FirstName], [id]) values ( @FirstName, @LastName, @ID ) fetch next from cursorDes into @desendselect * from #tempclose cursorDesdeallocate cursorDesdrop table #TempGO |
 |
|
|
s_anr
Yak Posting Veteran
81 Posts |
Posted - 2009-11-27 : 05:51:31
|
| sorry to say but it still gives the same error when i run it on the actual database. |
 |
|
|
MuralikrishnaVeera
Posting Yak Master
129 Posts |
Posted - 2014-06-05 : 08:07:41
|
Following may work for youSELECT * INTO #tempFROM (SELECT SUBSTRING([Description],CHARINDEX('Name',[Description]),CHARINDEX('Preferred Name',[Description])-CHARINDEX('Name',[Description])) AS col FROM [TESTTABLE].[dbo].[Example])aSELECT REPLACE(SUBSTRING(col,CHARINDEX(',',Col)+1,CHARINDEX('ID',col)-CHARINDEX(',',Col)-1),' ','') AS FirstName , REPLACE(SUBSTRING(col,CHARINDEX(':',Col)+1,CHARINDEX(',',col)-CHARINDEX(':',Col)-1),' ','') AS LastName , REPLACE(SUBSTRING(col,CHARINDEX('ID',Col)+3,LEN(Col)),' ','') AS IDFROM #temp---------------Murali KrishnaYou live only once ..If you do it right once is enough....... |
 |
|
|
|
|
|
|
|