Solved

TSQL Combine (Concatenate) Several Rows Into  Single New Row

Posted on 2016-08-30
2
28 Views
Last Modified: 2016-08-30
Hello,

I'm trying to create a Unique ID from several fields in my table by combining several rows into a single row.

Here's a select statement that (I think) is outputting what I want:

SELECT (CAST(BIRTH_DATE as VARCHAR(10))+(CAST(BEGIN_DATE as VARCHAR(10)))+RACE+SEX) 
FROM MyTable_t

Open in new window


I would like to take the output of the above select and insert it into the MyID field in MyTable_t.

Any help will be greatly appreciated - thanks!
0
Comment
Question by:ttist25
2 Comments
 
LVL 42

Accepted Solution

by:
zephyr_hex (Megan) earned 500 total points
ID: 41776715
MS SQL will allow you to update the same table you select from, like so:

UPDATE t SET t.MyID = (CAST(t.BIRTH_DATE as VARCHAR(10))+(CAST(t.BEGIN_DATE as VARCHAR(10)))+t.RACE+t.SEX) 
FROM MyTable_t

Open in new window

1
 
LVL 1

Author Closing Comment

by:ttist25
ID: 41776765
Thanks for this.  Worked great!
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how the fundamental information of how to create a table.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question