SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Concatenate fields by specifying the start position of the next filed


Concatenate fields by specifying the start position of the next filed

Author
Message
Trybbe
Trybbe
SSC Veteran
SSC Veteran (287 reputation)SSC Veteran (287 reputation)SSC Veteran (287 reputation)SSC Veteran (287 reputation)SSC Veteran (287 reputation)SSC Veteran (287 reputation)SSC Veteran (287 reputation)SSC Veteran (287 reputation)

Group: General Forum Members
Points: 287 Visits: 493
Hi,

how do I Concatenate fields by specifying the start position of the next filed.

e.g. I have table_x with three columns

Name
LastName
Empno


I would like to concatenate the three so that I only get one line. but I would also like to specify the length of the column and where the next column would start.
ChrisM@Work
ChrisM@Work
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16410 Visits: 19554
Why don't you simply concatenate into fixed-width, like this?
DROP TABLE #table_x
CREATE TABLE #table_x (Empno INT IDENTITY(3141593,31415), FirstName VARCHAR(25), LastName VARCHAR(25))

INSERT INTO #table_x (FirstName, LastName) VALUES
(('Francis'),('Bacon')),
(('Jean'),('Baptiste')),
(('Clarence'),('Birdseye')),
(('Charles'),('Babbage')),
(('John'),('Logie Baird'))

SELECT *,
Concatenated_FixedWidth = CAST(FirstName AS CHAR(25)) + CAST(LastName AS CHAR(25)) + CAST(Empno AS CHAR(25))
FROM #table_x




“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
Trybbe
Trybbe
SSC Veteran
SSC Veteran (287 reputation)SSC Veteran (287 reputation)SSC Veteran (287 reputation)SSC Veteran (287 reputation)SSC Veteran (287 reputation)SSC Veteran (287 reputation)SSC Veteran (287 reputation)SSC Veteran (287 reputation)

Group: General Forum Members
Points: 287 Visits: 493
Thanks chris

that's it
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search