Solved

TSQL Combine (Concatenate) Several Rows Into  Single New Row

Posted on 2016-08-30
2
26 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 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

760 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now