Solved

Select using Substring + a Join

Posted on 2008-10-10
1
212 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 143

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

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 article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

809 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