Solved

Concatenate String in SQL

Posted on 2014-03-26
4
202 Views
Last Modified: 2014-05-13
In my code, I'm trying to get get the result to be 0001.DESCRIPTION
Problem is I can't figure out how to properly concatenate.
Can  someone help.

Thankss

I currently have the following code:

SELECT RIGHT('0000' + CAST((glm.COMPANY) AS VARCHAR(4)),4) AS COMPANY,
       
       glm.ACCT_UNIT,
       glm.[DESCRIPTION],
      (CASE
        WHEN glm.ACCT_UNIT = '15200' THEN (COMPANY) + glm.[DESCRIPTION])
        --+glm.[DESCRIPTION])
        ELSE NULL
      END) "Report Group"
FROM LSPROD.dbo.GLNAMES glm  
WHERE glm.ACCT_UNIT in ('15200') and COMPANY = 5
0
Comment
Question by:metalteck
4 Comments
 
LVL 39

Expert Comment

by:lcohan
ID: 39957414
SELECT '0001.'+CAST(glm.COMPANY AS VARCHAR(4)) AS COMPANY,
       glm.ACCT_UNIT,
       glm.[DESCRIPTION],
      CASE
        WHEN glm.ACCT_UNIT = '15200' THEN COMPANY + glm.[DESCRIPTION]
        --+glm.[DESCRIPTION])
        ELSE NULL
      END as [Report Group]
FROM LSPROD.dbo.GLNAMES glm  
WHERE glm.ACCT_UNIT in ('15200') and COMPANY = 5
0
 

Author Comment

by:metalteck
ID: 39957428
I'm sorry, the company can by dynamic, thats why I use Right('0000'....
Regardless, when I tried your code, I got an error that says :
Conversion failed when converting the varchar value 'CPM (Company 5) 15200         ' to data type smallint.

CPM (Company 5) 15200 is the company name. So to revise my previous example, the result I would like to see is 0005.CPM (Company 5) 15200

Thanks
0
 
LVL 11

Expert Comment

by:John_Vidmar
ID: 39957479
Assumes you have less than 10,000 companies:
SELECT	RIGHT('0000' + CAST(COMPANY as varchar), 4) + '.' + [DESCRIPTION]
FROM	LSPROD.dbo.GLNAMES
WHERE	ACCT_UNIT = '15200'
AND	COMPANY = 5

Open in new window

0
 
LVL 69

Accepted Solution

by:
Scott Pletcher earned 500 total points
ID: 39957590
SELECT ca1.COMPANY_4 AS COMPANY,
       glm.ACCT_UNIT,
       glm.[DESCRIPTION],
       ca1.COMPANY_4 + '.' + glm.[DESCRIPTION] AS [Report Group]
FROM LSPROD.dbo.GLNAMES glm  
CROSS APPLY (
    SELECT RIGHT('0000' + CAST((glm.COMPANY) AS VARCHAR(4)),4) AS COMPANY_4
) AS ca1
WHERE
    glm.ACCT_UNIT in ('15200') and
    COMPANY = 5
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
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 documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

815 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

7 Experts available now in Live!

Get 1:1 Help Now