April 30, 2007 at 5:06 pm
Hi,
I have a column that has values:
and I need to create a new column that has the values concatinated with a sequence at the end so the new column would look like:
In Oracle, I would create a sequence and a trigger that would populate it on insert, create the new column and then set new_column= converted_lot || LPAD(to_char(substr(seq_no,2,4)),4,'0'). I've been looking for examples and reading BOL but can't find anything that talks about sequences in respect to what I'm trying to do.
Any help would be appreciated!
Thanks,
Isabelle
Thanks!
Bea Isabelle
May 1, 2007 at 12:55 am
SELECT Col1, 10000 * Col1 + ROW_NUMBER() OVER (ORDER BY Col1 DESC) AS Col2
ORDER BY Col1 DESC
N 56°04'39.16"
E 12°55'05.25"
May 1, 2007 at 6:54 am
Peter's method is absolutely a great way to take care of existing data. The problem is, wadaya gonna do with new data?
If you really need to do such a thing, my recommendation is to add an IDENTITY column and a calculated column to concatenate the Col1 and IDENTITY columns. That way, the impact on existing code is nil.
Formula for the concatenated column should be something like this (following your example)...
RIGHT('00000'+CAST(Col1 AS VARCHAR(5)),5)+RIGHT('0000'+CAST(identitycol AS VARCHAR(4)),4)
Sure, there are tricks you can do with STR and REPLACE, but the code above remains deterministic so you can put an index on the column if you need to.
I'm really curious about this... why do you need to do such a thing? The reason I ask is because you've limited table to a mere 9999 entries...
[Edit] Oh yeah... almost forgot... if this is just for display purposes, do it in the app.
--Jeff Moden
Change is inevitable... Change for the better is not.
May 1, 2007 at 11:17 am
Hi,
I'm sorry, but I forgot that this is a SQL 2000 database (we have both 2000 and 2005 and I got them confused) so when I try to use the row_number, I get the error:
Server: Msg 195, Level 15, State 10, Line 2
'ROW_NUMBER' is not a recognized function name.
Any suggestions for SQL 2000? This is only a one time thing. We are converting to a new ERP system and I need to move our legacy inventory information over and create a new lot number by taking the old lot code and adding a sequence to it so that every record is unique. Moving forward, the new EPR system will take care of generating that.
Thanks!
Isabelle
Thanks!
Bea Isabelle
May 1, 2007 at 2:43 pm
If this is a SQL Server 2000 problem, why do you post in a SQL Server 2005 forum?
DECLARE @Stage TABLE (RecID INT IDENTITY, ID INT)
INSERT @Stage (ID)
SELECT Col1 FROM Table1 ORDER BY Col1 DESC
UPDATE t1
SET t1.ID = 10000 * s.ID + s.RecID
FROM Table1 AS t1
INNER JOIN @Stage AS s ON s.ID = t1.ID
N 56°04'39.16"
E 12°55'05.25"
May 1, 2007 at 2:49 pm
As I said, since we have both 2000 and 2005, I got confused and forgot this was in our SQL 2000 database. Sorry.
I actually created an identity column which created sequence numbers and used the following:
SET MOVEX_LOT_NO = SUBSTRING([conv_lot_no],1,6) + RIGHT('0000' + CONVERT(varchar, sequence), 4)
Thanks for all the info and help.
Isabelle
Thanks!
Bea Isabelle
May 1, 2007 at 3:31 pm
Like I said, that takes care of existing data... whatcha gonna do for new data that enters into the table?
--Jeff Moden
Change is inevitable... Change for the better is not.
May 1, 2007 at 3:34 pm
Any new inventory item entered into the system will get the number generated automatically by the application. This is just a one time deal to get in our inventory from our old system into the new ERP.
Thanks!
Thanks!
Bea Isabelle
Viewing 8 posts - 1 through 7 (of 7 total)
You must be logged in to reply to this topic. Login to reply