Solved

Concatenate String in SQL

Posted on 2014-03-26
4
197 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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

757 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

18 Experts available now in Live!

Get 1:1 Help Now