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 |
srgade82
Starting Member
4 Posts |
Posted - 2010-12-03 : 12:44:18
|
HI I have a obj_key (varchar) column which has around 3000 records identicali need to update the records after 997 rows. the 998th record should be updated by (obj_key+0001) till the next 997 rows ie (till 1994th record) after next 997 records the column should be updated by(obj_key+0002)....so on till the records end.Can any one help me on how to do this.Thanks in advancesagar |
|
Bustaz Kool
Master Smack Fu Yak Hacker
1834 Posts |
Posted - 2010-12-03 : 13:46:27
|
How are you imposing an order on the records or, more specifically, how do you determine which record is the first, second, 997th, 998th, etc?=======================================Elitism is the slur directed at merit by mediocrity. -Sydney J. Harris, journalist (1917-1986) |
|
|
srgade82
Starting Member
4 Posts |
Posted - 2010-12-03 : 15:53:09
|
starting from the first record is 1 , 2,..till.3000.sagar |
|
|
Bustaz Kool
Master Smack Fu Yak Hacker
1834 Posts |
Posted - 2010-12-03 : 17:40:23
|
1 what? 2 what? 3000 what?All you've said is that the first one is first, the second one is second. If I looked at the data, what column and value would lead me to know that this record was first? Since the column/records are identical, how does a person determine their order?=======================================Elitism is the slur directed at merit by mediocrity. -Sydney J. Harris, journalist (1917-1986) |
|
|
srgade82
Starting Member
4 Posts |
Posted - 2010-12-03 : 19:35:37
|
Tablecolumn 1 column21 abc2 sass3 sdds4 sgsg. .. .after 997 records column2 record should be added by 0001. the next 997 column records look like xyz0001,fsdd0001,dsds0001....after 997 records the column records should by updated by 0002, they look like for example fdfd0002,fdrr0002....sagar |
|
|
srgade82
Starting Member
4 Posts |
Posted - 2010-12-04 : 13:52:49
|
sorry it looks this wayTablecolumn 1 column21 abc2 abc3 abc4 abc. .. .after 997 records column2 record should be added by 0001. the next 997 column records look like xyz0001,fsdd0001,dsds0001....after 997 records the column records should by updated by 0002, they look like for example fdfd0002,fdrr0002....sagar |
|
|
Sachin.Nand
2937 Posts |
Posted - 2010-12-05 : 10:54:13
|
For sake of testing run the query belowSELECT NUMBERINTO #tblFROM MASTER..spt_valuesWHERE TYPE = 'p' AND NUMBER >= 1SELECT *FROM #tbl;WITH cte AS (SELECT MIN(NUMBER) MIN, MAX(NUMBER) MAX, Row_number()OVER(ORDER BY (SELECT 1))rid FROM #tbl GROUP BY NUMBER / 998)UPDATE tSET t.NUMBER = t.NUMBER + ridFROM #tbl t INNER JOIN (SELECT rid, NUMBER FROM cte CROSS APPLY(SELECT NUMBER FROM #tbl t WHERE t.NUMBER BETWEEN MIN AND MAX)t)t1 ON t1.NUMBER = t.NUMBERSELECT *FROM #tblDROP TABLE #tbl PBUH |
|
|
dineshrajan_it
Posting Yak Master
217 Posts |
Posted - 2010-12-06 : 06:32:08
|
declare @sample1 table(col1 int identity(1,1) not null,col2 varchar(100))declare @sample2 table(col1 int identity(1,1) not null,col2 varchar(100))insert into @sample1(col2)select top 1000 left(ltrim(m1.name),5) frommaster..spt_values m1cross join master..spt_values m2where m1.name is not nullinsert into @sample2(col2)select top 1000 left(ltrim(m1.name),5) frommaster..spt_values m1cross join master..spt_values m2where m1.name is not nullORDER BY M1.NAME DESCdeclare @tab table(col1 int ,col2 varchar(100))insert into @tabselect * from @sample1union allselect * from @sample2update @tabset col2 = col2 + case when col1 >996 then Replicate(0,5-DataLength(CONVERT(varchar(100),cast(col1-996 AS int)))) + CONVERT(varchar(100),cast(col1-996 AS int)) else SPACE(0) endselect * from @tab order by col1 descIam a slow walker but i never walk back |
|
|
|
|
|
|
|