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 |
SQL_DEV
Starting Member
1 Post |
Posted - 2007-07-27 : 10:59:19
|
need suggestions to this problem Data is like this(WHEN RETURNING FROM DIFFERENT TABLES)column A column B7001 NULL7002A A7002AB AB7003VA VA7003AV AV7004ABC ABC7005XYZ XYZNEED TO CONVERT TO LOOK LIKE THIS FOR FUTURE COMBINATIONScolumn A column B7001 NULL7002A A7002AB A7002AB B7003VA V7003VA A7003AV A7003AV V7004ABC A7004ABC B7004ABC CANY HELP WILL BE HELPFUL |
|
JoeNak
Constraint Violating Yak Guru
292 Posts |
Posted - 2007-07-27 : 13:31:19
|
--Test DataDeclare @table1 table (colA varchar(20))Insert Into @table1 Values ('7001')Insert Into @table1 Values ('7002A')Insert Into @table1 Values ('7002AB')Declare @table2 table (colA varchar(20), colB varchar(20))Insert Into @table2 values ('7002A', 'A')Insert Into @table2 values ('7002AB', 'AB')--Declare @TempTable table (colA varchar(20), colB varchar(20))Declare @colA varchar(20), @colB varchar(20)Select Top 1 @colA = colA , @colB = colBFrom @table2Order By colAWhile @@RowCount <> 0 Begin While Len(@colB) > 0 Begin Insert Into @TempTable Values (@colA, Left(@colB, 1)) Set @colB = Right(@colB, Len(@colB) - 1) End Select Top 1 @colA = colA , @colB = colB From @table2 Where colA > @colA Order By colA EndSelect t1.colA, TT.colBFrom @table1 t1Left Outer Join @TempTable TT On T1.colA = TT.colA |
|
|
tiheld
Starting Member
1 Post |
Posted - 2007-07-27 : 15:35:38
|
declare @test table ( id nvarchar(7) primary key, val nvarchar(4), parsed bit default 0)declare @parsed table ( id nvarchar(7) not null, val nvarchar(1))declare @currentid nvarchar(7)declare @currentval nvarchar(4)insert into @test (id,val) values('7001', NULL)insert into @test (id,val) values('7002A', 'A')insert into @test(id,val) values('7002AB', 'AB')insert into @test (id,val) values('7003VA', 'VA')insert into @test (id,val) values('7003AV', 'AV')insert into @test (id,val) values('7004ABC', 'ABC')insert into @test (id,val) values('7005XYZ', 'XYZ')select top 1 @currentid = id, @currentval =val from @test while @currentid is not null Begin if @currentval is null insert into @parsed values(@currentid, null) else begin while @currentval is not null and len(@currentval) > 0 begin insert into @parsed (id, val) values(@currentid, substring(@currentval, 1,1)) select @currentval = substring(@currentval, 2, len(@currentval) -1) end endupdate @test set parsed = 1 where id = @currentidset @currentid = nullselect top 1 @currentid = id, @currentval =val from @test where parsed = 0Endselect * from @parsed |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-07-27 : 15:48:24
|
No need for looping. Use set-based method-- Prepare sample dataDECLARE @Sample TABLE (A VARCHAR(7), B VARCHAR(3))INSERT @SampleSELECT '7001', NULL UNION ALLSELECT '7002A', 'A' UNION ALLSELECT '7002AB', 'AB' UNION ALLSELECT '7003VA', 'VA' UNION ALLSELECT '7003AV', 'AV' UNION ALLSELECT '7004ABC', 'ABC' UNION ALLSELECT '7005XYZ', 'XYZ'-- Show the expected outputSELECT s.A, SUBSTRING(s.B, v.Number, 1) AS NewBFROM @Sample AS sCROSS JOIN master..spt_values AS vWHERE v.Number > 0 AND v.Number <= LEN(ISNULL(s.B, '_')) AND v.Type = 'p' E 12°55'05.25"N 56°04'39.16" |
|
|
|
|
|
|
|