Solved

Concatenate String in SQL

Posted on 2014-03-26
4
200 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:
ScottPletcher 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Viewers will learn how the fundamental information of how to create a table.

920 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

12 Experts available now in Live!

Get 1:1 Help Now