April 19, 2011 at 7:18 am
Hi,
I have a table that is already populated. I need to add a new column & the values in the column should be AA, AB, AC, AD, AE, .................... AZ, BA, BB, BC, BD, ......................,BZ, CA, CB...
How can I achieve this?
Regards,
Nithin
April 19, 2011 at 7:24 am
kr.nithin (4/19/2011)
Hi,I have a table that is already populated. I need to add a new column & the values in the column should be AA, AB, AC, AD, AE, .................... AZ, BA, BB, BC, BD, ......................,BZ, CA, CB...
How can I achieve this?
Regards,
Nithin
Interesting challenge - what's it for?
For better assistance in answering your questions, please read this[/url].
Hidden RBAR: Triangular Joins[/url] / The "Numbers" or "Tally" Table: What it is and how it replaces a loop[/url] Jeff Moden[/url]
April 19, 2011 at 7:48 am
This is a Vendor table. This already has a primary key. The requirement is Alphabets as the unique key rather than the numeric primary key.
April 19, 2011 at 7:51 am
kr.nithin (4/19/2011)
This is a Vendor table. This already has a primary key. The requirement is Alphabets as the unique key rather than the numeric primary key.
So the numeric "primary key" isn't unique?
Don't get me wrong, this can be done - just asking a few questions.
For better assistance in answering your questions, please read this[/url].
Hidden RBAR: Triangular Joins[/url] / The "Numbers" or "Tally" Table: What it is and how it replaces a loop[/url] Jeff Moden[/url]
April 19, 2011 at 8:03 am
Yep In spite of the Numeric Primary Key. We need 2 character Vendor Code for each vendor.
April 19, 2011 at 8:08 am
kr.nithin (4/19/2011)
Yep In spite of the Numeric Primary Key. We need 2 character Vendor Code for each vendor.
How many vendors do you currently have? What is the rate at which you acquire new ones?
For better assistance in answering your questions, please read this[/url].
Hidden RBAR: Triangular Joins[/url] / The "Numbers" or "Tally" Table: What it is and how it replaces a loop[/url] Jeff Moden[/url]
April 19, 2011 at 8:13 am
120 vendors as of now vendors added at the rate of around 2 for a month. Will it be possible to auto-increment if a new vendor is added.
April 19, 2011 at 8:25 am
How's this?
DECLARE @MyTable TABLE (VendorCode CHAR(2));
INSERT INTO @MyTable
SELECT 'AA' UNION ALL
SELECT 'AB' UNION ALL
SELECT 'AC';
WITH cteMiniTally AS
(
-- get numbers from 1-26 (for the letters of the alphabet)
SELECT TOP (26)
RN = ROW_NUMBER() OVER (ORDER BY object_id)
FROM master.sys.all_columns
), cteAvailableVendorCodes AS
(
-- get all codes from AA - ZZ
SELECT VendorCode = CHAR(c1.RN+64) + CHAR(c2.RN+64)
FROM cteMiniTally c1
CROSS JOIN cteMiniTally c2
EXCEPT
-- remove codes already used
SELECT VendorCode
FROM @MyTable
)
-- get the next code to use
SELECT TOP (1)
VendorCode
FROM cteAvailableVendorCodes
ORDER BY VendorCode;
Wayne
Microsoft Certified Master: SQL Server 2008
Author - SQL Server T-SQL Recipes
April 19, 2011 at 8:29 am
LOL Wayne that was quick!
;WITH VendorCodes AS
(
SELECT [letter] = CHAR(64 + rn)
FROM (SELECT rn = ROW_NUMBER() OVER(ORDER BY [Name]) FROM sys.columns
) d WHERE rn < 27
)
SELECT c1.letter, c2.letter
FROM VendorCodes c1
CROSS JOIN VendorCodes c2
ORDER BY c1.letter, c2.letter
For better assistance in answering your questions, please read this[/url].
Hidden RBAR: Triangular Joins[/url] / The "Numbers" or "Tally" Table: What it is and how it replaces a loop[/url] Jeff Moden[/url]
April 19, 2011 at 9:03 am
ChrisM@home (4/19/2011)
LOL Wayne that was quick!
Yep - not that hard. We're thinking along the same lines here.
Wayne
Microsoft Certified Master: SQL Server 2008
Author - SQL Server T-SQL Recipes
April 19, 2011 at 9:13 am
WOW !!!!! That worked like a charm :-):-):-):-):-):-):-):-):-):-):-)
April 19, 2011 at 9:15 am
Hats off :-):-):-):-):-):-) To coooooooooooooool !!!!!!!!!!!1
Viewing 12 posts - 1 through 11 (of 11 total)
You must be logged in to reply to this topic. Login to reply