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 |
|
cidmi.dovic
Yak Posting Veteran
53 Posts |
Posted - 2009-07-13 : 03:20:06
|
| Hi,The question is simply I have a table with this dataTS [DateTime], NAME [Varchar], ID [Identity INT]The content is something like this2009-07-01 10:00:00 10203010XF 12009-07-01 10:00:05 10203011XF 22009-07-01 10:00:10 10203012XF 3And what I want to do is to expand this information in this way2009-07-01 10:00:00 10203010XF 12009-07-01 10:00:00 10203010XA 42009-07-01 10:00:00 10203010XB 52009-07-01 10:00:05 10203011XF 22009-07-01 10:00:05 10203011XA 62009-07-01 10:00:05 10203011XB 72009-07-01 10:00:10 10203012XF 32009-07-01 10:00:10 10203012XA 82009-07-01 10:00:10 10203012XB 9I mean copy the rows but changing one letter in the field NAME.I don't want to use a cursor to make it, Is this possible any other way?Thanks in advance.The Padrón peppers itch and other don't |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2009-07-13 : 03:30:02
|
[code]DECLARE @sample TABLE( TS datetime, NAME varchar(10), ID int identity)INSERT INTO @sample (TS, NAME)SELECT '2009-07-01 10:00:00', '10203010XF' UNION ALLSELECT '2009-07-01 10:00:05', '10203011XF' UNION ALLSELECT '2009-07-01 10:00:10', '10203012XF'SELECT *FROM @sample/*TS NAME ID ------------------------------------------------------ ---------- ----------- 2009-07-01 10:00:00.000 10203010XF 12009-07-01 10:00:05.000 10203011XF 22009-07-01 10:00:10.000 10203012XF 3(3 row(s) affected)*/INSERT INTO @sample (TS, NAME)SELECT TS, STUFF(NAME, LEN(NAME), 1, c.ch)FROM @sample s CROSS JOIN ( SELECT ch = 'A' UNION ALL SELECT ch = 'B' ) cSELECT *FROM @sample/*TS NAME ID ------------------------------------------------------ ---------- ----------- 2009-07-01 10:00:00.000 10203010XF 12009-07-01 10:00:05.000 10203011XF 22009-07-01 10:00:10.000 10203012XF 32009-07-01 10:00:00.000 10203010XA 42009-07-01 10:00:00.000 10203010XB 52009-07-01 10:00:05.000 10203011XA 62009-07-01 10:00:05.000 10203011XB 72009-07-01 10:00:10.000 10203012XA 82009-07-01 10:00:10.000 10203012XB 9(9 row(s) affected)*/[/code] KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
|
|
|
|
|