August 25, 2010 at 1:24 pm
I have first name and last name in my table. I have to concatenate these two columns and update in other column. But if name appears more than once I have to concatenate 1 to that.
eg
Firstname LastName url
David Glance DavidGlance
David Glance Davidglance01
David Glance DavidGlance02
John Smith JohnSmith
How can I achieve this?
Prema
August 25, 2010 at 1:32 pm
So you already have the names in the table and an empty 3rd column, which you wish you populate with the full name plus an incremented number if the name is not unique?
August 25, 2010 at 1:47 pm
This should start you off 🙂
--DROP TABLE #Test
CREATE TABLE #Test
( FirstName VARCHAR(100),
LastName VARCHAR(100),
URL VARCHAR(200))
INSERT INTO #Test (FirstName,LastName) VALUES ('Jim','Bob')
INSERT INTO #Test (FirstName,LastName) VALUES ('David','Glance')
INSERT INTO #Test (FirstName,LastName) VALUES ('David','Glance')
INSERT INTO #Test (FirstName,LastName) VALUES ('Keith','Smith')
INSERT INTO #Test (FirstName,LastName) VALUES ('Lucciano','Becchio')
UPDATE #Test SET URL = fst + ' ' + lst + ' ' + CONVERT(VARCHAR(10),cnt)
FROM (SELECT FirstName AS fst, LastName AS lst, COUNT(*) - 1 AS cnt
FROM #Test
GROUP BY FirstName, LastName) upd
WHERE FirstName = fst AND LastName = lst
SELECT * FROM #Test
Cheers,
Jim.
August 25, 2010 at 1:53 pm
I was going a different way to make it constantly updating with a Trigger, so he wouldn't have to constantly run it after every update..
--DROP TABLE TEST
GO
CREATE TABLE test (
id int identity(1,1),
FN varchar(50),
LN varchar(50),
URL varchar(105)
)
GO
CREATE TRIGGER CreateURLName on dbo.Test AFTER INSERT
AS
BEGIN
DECLARE @cnt int,
@currid int,
@fn varchar(50),
@ln varchar(50),
@url varchar(105)
SET @currid = (SELECT max(id) FROM dbo.test)
SET @cnt = (SELECT COUNT(*) FROM dbo.test tINNER JOIN inserted i ON (t.fn = i.fn AND t.ln = i.ln))
SET @fn = (SELECT fn FROM inserted)
SET @ln = (SELECT ln FROM inserted)
IF @cnt > 1
BEGIN
UPDATE test
SET URL = t.FN + t.LN + cast(@cnt-1 as varchar) FROM test t WHERE ID = @currid
END
IF @cnt = 1
BEGIN
UPDATE dbo.Test
SET URL = FN + LN FROM test t WHERE ID = @currid
END
END
GO
INSERT INTO TEST (fn, ln)
SELECT 'Dave','Smith'
INSERT INTO TEST (fn, ln)
SELECT 'Dave','Smith'
INSERT INTO TEST (fn, ln)
SELECT 'Dave','Smith'
INSERT INTO TEST (fn, ln)
SELECT 'Dave','Smith'
INSERT INTO TEST (fn, ln)
SELECT 'Doug','Smith'
INSERT INTO TEST (fn, ln)
SELECT 'Doug','Smith'
INSERT INTO TEST (fn, ln)
SELECT 'Doug','Smith'
SELECT * FROM test
edit: changed the code so the second entry starts concatenates 1 instead of 2
August 25, 2010 at 2:00 pm
Does this give you what you're looking for?
IF NOT OBJECT_ID('tempdb.dbo.#t', 'U') IS NULL DROP TABLE #t
SELECT 'David' AS Firstname, 'Glance' AS Lastname into #t UNION ALL
SELECT 'David', 'Glance' UNION ALL
SELECT 'David', 'Glance' UNION ALL
SELECT 'John', 'Smith'
;WITH cte AS
(
SELECT ROW_NUMBER() OVER (PARTITION BY Firstname, Lastname ORDER BY (SELECT 0)) - 1 AS ROW,
Firstname,
Lastname
FROM #t
)
SELECT Firstname,
Lastname,
Firstname + Lastname + CASE WHEN ROW = 0 THEN '' ELSE '0' + CONVERT(VARCHAR(10), ROW) END
FROM cte
August 25, 2010 at 2:30 pm
As an UPDATE
IF NOT OBJECT_ID('tempdb.dbo.#t', 'U') IS NULL DROP TABLE #t
CREATE TABLE #t
(Firstname VARCHAR(100),
Lastname VARCHAR(100),
url VARCHAR(200))
INSERT #t (Firstname, Lastname)
SELECT 'David', 'Glance' UNION ALL
SELECT 'David', 'Glance' UNION ALL
SELECT 'David', 'Glance' UNION ALL
SELECT 'John', 'Smith'
;WITH cte AS
(
SELECT ROW_NUMBER() OVER (PARTITION BY Firstname, Lastname ORDER BY (SELECT 0)) - 1 AS ROW,
Firstname,
Lastname,
url
FROM #t
)
UPDATE cte SET url = Firstname + Lastname + CASE WHEN ROW = 0 THEN '' ELSE '0' + CONVERT(VARCHAR(10), ROW) END
SELECT Firstname, Lastname, url
FROM #t
August 25, 2010 at 6:56 pm
Thanks guys. it is working. is there any to automate so that every time new row added it will update by itself
August 25, 2010 at 10:06 pm
Prema sambandam (8/25/2010)
Thanks guys. it is working. is there any to automate so that every time new row added it will update by itself
Put it into an INSERT trigger.
Wayne
Microsoft Certified Master: SQL Server 2008
Author - SQL Server T-SQL Recipes
August 26, 2010 at 7:40 am
Prema sambandam (8/25/2010)
Thanks guys. it is working. is there any to automate so that every time new row added it will update by itself
Yes, the trigger I posted will do this.
August 27, 2010 at 10:06 am
Hi,
Create INSERT Trigger on the table and put previous script.
August 27, 2010 at 8:51 pm
Prema sambandam (8/25/2010)
I have first name and last name in my table. I have to concatenate these two columns and update in other column. But if name appears more than once I have to concatenate 1 to that.eg
Firstname LastName url
David Glance DavidGlance
David Glance Davidglance01
David Glance DavidGlance02
John Smith JohnSmith
How can I achieve this?
Prema
Gosh this is a bad idea. What happens if you have more than 100 instances of the same name?
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 11 posts - 1 through 10 (of 10 total)
You must be logged in to reply to this topic. Login to reply