Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 617613
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T18:27:18+00:00 2026-05-13T18:27:18+00:00

I’m working on integrating some data from a 3rd-Party system into one of my

  • 0

I’m working on integrating some data from a 3rd-Party system into one of my applications (legacy ASP Classic-based web application/SQL 2000) – they have made some poor decisions (IMHO) when it comes to their approach and data structure, though perhaps we might get a chance to refactor at some point… but until then, I have to work with what is in front of me.

The main table holds inspection data, with one of the fields being used to record if certain characteristics were observed. The characteristics are stored in a Table called Categories, but unfortunately, the main inspection table (Test) links to the categories by concatenating relevant CategoryIDs into a single field (SelectedCategories). So, for example, if characteristics 01 and 02 were observed, the SelectedCategories column for that row in Test would have a value of ’01C02C’.

Trimmed DDL:

CREATE TABLE [dbo].[Test](
[ItemID] [varchar](255) NOT NULL,
[Result] [varchar](255) NULL,
[Comments] [varchar](255) NULL,
[ResultReason] [varchar](255) NULL,
[ImageLocation] [varchar](255) NULL,
[TestDateTime] [smalldatetime] NOT NULL,
[SelectedCategories] [varchar](255) NULL)

The question is, given the situation, how best can I appropriately extract the data from Test with a breakdown of the characteristics observed?

The output on the client I want is a table with the following columns:
Test.PK, Test.Field2… Test.Fieldn, Categories.ID1, Categories.ID2, Categories.IDn

That’s probably not quite clear enough – the first fields would be the usual suspects from Test, followed by a tick or cross (or some other visual indicator) for each of the categories in Categories.

Obviously, if this can be achieved in one query, so much the better in terms of efficiency and performance. However, I’m not sure how this would be achieved – how would you join the Categories table via SelectedCategories?

I could obviously simply report the SelectedCategories value and have the application parse the value. This could be hard-coded or more likely we would re-query Categories for each row in Test – though this would have performance implications. TBH, performance probably isn’t an issue in this case, but just because you can get away with something, doesn’t mean you should make a habit of it.

Equally, if I have an opportunity to refactor the 3rd party application, would I remove SelectedCategories column and add in a TestCategories table? Or would I hard-code each category as a series Bit columns. In all likelihood, the Categories won’t change throughout the life of the system, but if they did, it means changes (albeit very minor) to both the DB and the application.

I hope I’ve explained it sufficiently clearly. In essence, I’m saying what is the best approach if I stick with the current system? And if I were to refactor, what different approach could I take?

Progress Update:

Largely thanks to Lieven, I’ve got thus far:

DML:

SELECT  c.ID, c.Category, t.FilterID, t.OperatorResult, t.SelectedCategories
FROM    dbo.Categories c
        inner JOIN dbo.Test t ON CHARINDEX(Cast(c.ID as varchar), t.SelectedCategories, 1) <> 0
order by FilterID, ID

Output:

ID   Category             FilterID   OperatorResult   SelectedCategories
4    Cracked Ceramic      137667     FAILED           04C
4    Cracked Ceramic      284821     FAILED           04C
4    Cracked Ceramic      287617     FAILED           04C05C
5    Damaged Case         287617     FAILED           04C05C
4    Cracked Ceramic      310112     FAILED           04C05C
5    Damaged Case         310112     FAILED           04C05C

This would suffice, except that in order to reach my desired screen output…

Filter ID  Operator Result    Cat Matl   Crack    Damage   High Soot   
137667     FAILED             X          X        
178643     FAILED           
284821     FAILED                        X        
287617     FAILED                                 X        X     
310112     FAILED                        X        X    

… I either need further work on the SQL (so that I can achieve the desired output in just one query) or I need to do some extra work in the application itself.

Conclusion:

If we look at Lieven’s latest example (below), we can see that the the problem can be tackled in TSQL but that the Categories are hard-coded.

The alternative is stick to raw data and make IIS/ASP do more work. It will complicate the source code for certain, but will remove the potential overhead of updating the TSQL if a category is added or removed. I could certainly live with this very occasional need to update the TSQL, but I’m thinking ahead to different problems where the the Categories table would be actively changing on a regular basis.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T18:27:18+00:00Added an answer on May 13, 2026 at 6:27 pm

    To join the Categories table via SelectedCategories could work like this

    Edit

    Some things to think about

    • While the crosstab functionality is possible with the group by and all, perhaps it would be better to handle this in a client application.
    • The inputs you’ve provided in your question do not match the outputs you’ve provided. I have used your inputs and assumed some things about the requested output. I assume Cat Matl and High Soot are just other possible values for categories.

    Let us know if it worked for you.

    BEGIN TRAN
    
    CREATE TABLE [dbo].[Categories](
    [CategorieID] INTEGER NOT NULL)
    
    CREATE TABLE [dbo].[Test](
    [ItemID] [varchar](255) NOT NULL,
    [Result] [varchar](255) NULL,
    [Comments] [varchar](255) NULL,
    [ResultReason] [varchar](255) NULL,
    [ImageLocation] [varchar](255) NULL,
    [TestDateTime] [smalldatetime] NOT NULL,
    [SelectedCategories] [varchar](255) NULL)
    
    INSERT INTO dbo.Categories VALUES (4)
    INSERT INTO dbo.Categories VALUES (5)
    
    
    INSERT INTO dbo.Test VALUES (137667, 'FAILED', NULL, 'Cracked Ceramic', NULL, GetDate(), '04C')
    INSERT INTO dbo.Test VALUES (284821, 'FAILED', NULL, 'Cracked Ceramic', NULL, GetDate(), '04C')
    INSERT INTO dbo.Test VALUES (287617, 'FAILED', NULL, 'Cracked Ceramic', NULL, GetDate(), '04C05C')
    INSERT INTO dbo.Test VALUES (287617, 'FAILED', NULL, 'Damaged Case'   , NULL, GetDate(), '04C05C')
    INSERT INTO dbo.Test VALUES (310112, 'FAILED', NULL, 'Cracked Ceramic', NULL, GetDate(), '04C05C')
    INSERT INTO dbo.Test VALUES (310112, 'FAILED', NULL, 'Damaged Case'   , NULL, GetDate(), '04C05C')
    
    SELECT  [Filter ID] = t.ItemID
            , [Operator Result] = t.Result
            , [Reason] = t.ResultReason
    INTO    #Output
    FROM    dbo.Categories c                  
            LEFT OUTER JOIN dbo.Test t ON 
              /* Search for "C<{00}CategorieID>C" */
              CHARINDEX('C'                                                       -- Prefix CategorieID & SelectedCategories with 'C'
                          + REPLICATE('0', 2 - LEN(CAST(CategorieID AS VARCHAR))) -- Left Pad CategorieID with '0'
                          + CAST(CategorieID AS VARCHAR)                          -- Add CategorieID itself
                          + 'C'                                                   -- Suffix search string with 'C'.
                        , 'C' + t.SelectedCategories                              -- Prefix CategorieID & SelectedCategories with 'C'
                        , 1) <> 0
    
    SELECT    [Filter ID]
              , [Operator Result]
              , [Cat Matl] = CASE WHEN [Cat Matl] = 1 THEN 'X' ELSE '' END
              , [Crack] = CASE WHEN [Crack] = 1 THEN 'X' ELSE '' END
              , [Damage] = CASE WHEN [Damage] = 1 THEN 'X' ELSE '' END
              , [High Soot] = CASE WHEN [High Soot] = 1 THEN 'X' ELSE '' END
    FROM      (
                SELECT    [Filter ID]
                          , [Operator Result]
                          , [Cat Matl] = MAX(CASE WHEN Reason = 'Cat Matl' THEN 1 ELSE 0 END) 
                          , [Crack] = MAX(CASE WHEN Reason = 'Cracked Ceramic' THEN 1 ELSE 0 END) 
                          , [Damage] = MAX(CASE WHEN Reason = 'Damaged Case' THEN 1 ELSE 0 END) 
                          , [High Soot] = MAX(CASE WHEN Reason = 'High Soot' THEN 1 ELSE 0 END) 
                FROM      #Output
                GROUP BY  [Filter ID]
                          , [Operator Result]
              ) o
    
    DROP TABLE #Output
    
    ROLLBACK TRAN
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I am currently running into a problem where an element is coming back from
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I am reading a book about Javascript and jQuery and using one of the
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.