Solved

SQL Syntax

Posted on 2009-05-19
7
246 Views
Last Modified: 2012-05-07
I have a table with mulitple columns of doc types that either has a 0 or 1. I have created another table and I want to put all the 1s concatenated into the docname field of the new table. Example

1st table
acctID   doc1   doc2   doc3
123         0        1           1

I need to pull it like this into my 2nd table
acctID      docs
123        doc2, doc3


I know this is easy but I have spent too much time on it already.....Any help would be greatly appreciated
0
Comment
Question by:healthcheckinc
7 Comments
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 24423322
what is the database/version you are using?
0
 
LVL 92

Expert Comment

by:Patrick Matthews
ID: 24423327
Hello healthcheckinc,

With respect, both of these are reall, really suboptimal designs :)

What db product are you using, as it makes a difference in syntax...

Regards,

Patrick
0
 
LVL 92

Accepted Solution

by:
Patrick Matthews earned 500 total points
ID: 24423358
SQL Server...

SELECT acctID, SUBSTRING(
      CASE WHEN doc1 = 0 THEN '' ELSE ', doc1' END +
      CASE WHEN doc2 = 0 THEN '' ELSE ', doc2' END +
      CASE WHEN doc3 = 0 THEN '' ELSE ', doc3' END, 3, 100) AS Docs
FROM SomeTable

To create a new table...

SELECT acctID, SUBSTRING(
      CASE WHEN doc1 = 0 THEN '' ELSE ', doc1' END +
      CASE WHEN doc2 = 0 THEN '' ELSE ', doc2' END +
      CASE WHEN doc3 = 0 THEN '' ELSE ', doc3' END, 3, 100) AS Docs
INTO NewTableName
FROM SomeTable
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 11

Expert Comment

by:bmatumbura
ID: 24423359
Try this
SELECT AcctID, 

CASE WHEN doc1 AND doc2 AND doc3 THEN 'doc1, doc2, doc3'

CASE WHEN doc1 AND doc2 THEN 'doc1, doc2'

CASE WHEN doc1 and doc3 THEN 'doc1, doc3'

CASE WHEN doc2 AND doc3 THEN 'doc2, doc3'

CASE WHEN doc1 THEN 'doc1'

CASE WHEN doc2 THEN 'doc2'

ELSE 'doc3' END AS docs

INTO Table2 from Table1

Open in new window

0
 
LVL 92

Expert Comment

by:Patrick Matthews
ID: 24423374
Access...

SELECT acctID, Mid(IIf(doc1 = 0, "", ", doc1") & IIf(doc2 = 0, "", ", doc2") & IIf(doc3 = 0, "", ", doc3"), 3) AS docs
FROM SomeTable

creating new table...

SELECT acctID, Mid(IIf(doc1 = 0, "", ", doc1") & IIf(doc2 = 0, "", ", doc2") & IIf(doc3 = 0, "", ", doc3"), 3) AS docs
INTO NewTable
FROM SomeTable
0
 
LVL 11

Expert Comment

by:bmatumbura
ID: 24423378
correction
SELECT AcctID, 

CASE WHEN doc1 AND doc2 AND doc3 THEN 'doc1, doc2, doc3'

CASE WHEN doc1 AND doc2 THEN 'doc1, doc2'

CASE WHEN doc1 and doc3 THEN 'doc1, doc3'

CASE WHEN doc2 AND doc3 THEN 'doc2, doc3'

CASE WHEN doc1 THEN 'doc1'

CASE WHEN doc2 THEN 'doc2'

CASE WHEN doc3 THEN 'doc3'

ELSE '' END AS docs

INTO Table2 from Table1

Open in new window

0
 

Author Closing Comment

by:healthcheckinc
ID: 31583062
its needed for a report..I didnt design the system
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Join & Write a Comment

The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

707 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

14 Experts available now in Live!

Get 1:1 Help Now