0

The query below is not my actual query but a minimized version of it, as I cannot post the original one. However this explains what I want to do exactly:

Insert into TableA (BirthdateA, NameA) Select BirthdateB, (case when NameB is not null then NameB else "Desired Value" End) From TableB 

"Desired Value" should be a dummy name in addition to an incremental value, for example "Dummy Name 1", "Dummy Name 2" So the final result would look like

TableB.Name TableA.Name John Smith John Smith Null Dummy Name 1 Adam James Adam James John K. John K. Null Dummy Name 2 

Is that possible ?

2 Answers 2

1

You can try this:

INSERT INTO TableA SELECT BirthDateB, CASE WHEN NameB IS NULL THEN ('Dummy Name ' + CAST(DT.rn As varchar(10))) ELSE NameB END As NameA FROM ( SELECT *, ROW_NUMBER() OVER (PARTITION BY NameB ORDER BY NameB) As rn FROM TableB) DT 

Note :
I use ORDER BY NameB because I don't know what is your PK, you can change NameB to your PK.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for your answer, but I got one question, if i did it this way, this means that the number after Dummy Name will not be incremental (+1) but will be equivalent to that row's number (ie. Dummy Name 3 followed by Dummy Name 5 possibly) is that true ?
@user4612290 When you use PARTITION BY NameB it will not be the row number it will be the rank based on NameB ;).
1

Can you try this

CREATE TABLE #Temp (No INT IDENTITY(1,1), BirthDate varchar(100), Name varchar(250)) INSERT INTO #Temp (Name) SELECT BirthDate, 'Dummy Name' FROM TableB WHERE Name IS NULL SELECT BirthDate, Name FROM FROM TableB WHERE Name IS NOT NULL UNION SELECT BirthDate, Name + CAST(No VARCHAR(100)) FROM FROM #Temp 

2 Comments

Thank you for your answer, but how can I bind each row of the Temp table to their data from TableB ? Each Name in Temp has a Birthdate in TableB
This would work with me too, I chose the other answer however as it will provide it with the least steps. Thanks a lot

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.