Solved

Select using Substring + a Join

Posted on 2008-10-10
1
197 Views
Last Modified: 2012-05-05
I have the following:

select Top(5) substring(ParentId, 0, charindex('-',ParentId,1)) as Parent from assets

1. :
2. : 72
3. : 51
4. : 168
5. : 111

and I have:

select Top(5) parentid from assets

which gives:

1. : 56
2. : 72-10672
3. : 51-10610
4. : 168-10658
5. : 111-10624


How would I go about doing a join where I include ALL parentID's which do not have a - ? and where it uses the first select statement to extract the first part of the ParentID?
0
Comment
Question by:directxBOB
1 Comment
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 22685656
what about this:

select Top(5) case when parentid like '%-%' then substring(ParentId, 0, charindex('-',ParentId,1)) else parentid end as Parent from assets 

Open in new window

0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

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

17 Experts available now in Live!

Get 1:1 Help Now