Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Concatenate fields by specifying the start position of the next filed Expand / Collapse
Author
Message
Posted Friday, April 5, 2013 3:27 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Monday, August 4, 2014 11:24 PM
Points: 176, Visits: 482
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.
Post #1439136
Posted Friday, April 5, 2013 3:41 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 9:54 AM
Points: 6,813, Visits: 14,028
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
Post #1439140
Posted Friday, April 5, 2013 4:13 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Monday, August 4, 2014 11:24 PM
Points: 176, Visits: 482
Thanks chris

that's it
Post #1439148
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse