SQL Query for multi-select field and custom_captions

Hi,

I'm trying to query the database and one of the fields is a multi-select field. Consequently it has values in it like ',val1,val2,val3,' in the report view. What I'd really like is to replace those with the values in the custom_captions table. Does anyone know the appropriate SQL query to do that?

Many thanks,

D.

  • 0

    I cannot think of a nice way to do this, be interested to see if someone else can point me in a better direction, but, this may get you started...

    I have a multi select named oppo_dcl_multi, it has values of 1,2,3,4 and it uses a caption family (just to be confusing) with name lead_dcl_multi, which has the translations of 'First','Second','Third','Fourth'

    The SQL is:

    SELECT o.oppo_opportunityId,p.capt_uk,q.capt_uk,r.capt_uk,t.capt_uk
    FROM Opportunity o
    LEFT JOIN
    (
    SELECT oppo_opportunityId,capt_uk
    FROM
    (
    SELECT oppo_opportunityId,A.[oppo_dcl_multi], Split.a.value('.', 'VARCHAR(100)') AS String
    FROM
    (
    SELECT oppo_opportunityId,[oppo_dcl_multi],CAST ('' + REPLACE([oppo_dcl_multi], ',', '') + '' AS XML) AS String
    FROM Opportunity
    WHERE oppo_dcl_multi IS NOT NULL
    ) as A
    CROSS APPLY String.nodes ('/M') AS Split(a)
    ) as Stuf
    INNER JOIN Custom_Captions ON LTRIM(RTRIM(String)) = LTRIM(RTRIM(capt_code)) AND capt_family = 'lead_dcl_multi'
    WHERE COALESCE(String,'') <> ''
    ) as p on p.Oppo_OpportunityId = o.oppo_opportunityId AND p.capt_uk = 'First'
    LEFT JOIN
    (
    SELECT oppo_opportunityId,capt_uk
    FROM
    (
    SELECT oppo_opportunityId,A.[oppo_dcl_multi], Split.a.value('.', 'VARCHAR(100)') AS String
    FROM
    (
    SELECT oppo_opportunityId,[oppo_dcl_multi],CAST ('' + REPLACE([oppo_dcl_multi], ',', '') + '' AS XML) AS String
    FROM Opportunity
    WHERE oppo_dcl_multi IS NOT NULL
    ) as A
    CROSS APPLY String.nodes ('/M') AS Split(a)
    ) as Stuf
    INNER JOIN Custom_Captions ON LTRIM(RTRIM(String)) = LTRIM(RTRIM(capt_code)) AND capt_family = 'lead_dcl_multi'
    WHERE COALESCE(String,'') <> ''
    ) as q on q.Oppo_OpportunityId = o.oppo_opportunityId AND q.capt_uk = 'Second'
    LEFT JOIN
    (
    SELECT oppo_opportunityId,capt_uk
    FROM
    (
    SELECT oppo_opportunityId,A.[oppo_dcl_multi], Split.a.value('.', 'VARCHAR(100)') AS String
    FROM
    (
    SELECT oppo_opportunityId,[oppo_dcl_multi],CAST ('' + REPLACE([oppo_dcl_multi], ',', '') + '' AS XML) AS String
    FROM Opportunity
    WHERE oppo_dcl_multi IS NOT NULL
    ) as A
    CROSS APPLY String.nodes ('/M') AS Split(a)
    ) as Stuf
    INNER JOIN Custom_Captions ON LTRIM(RTRIM(String)) = LTRIM(RTRIM(capt_code)) AND capt_family = 'lead_dcl_multi'
    WHERE COALESCE(String,'') <> ''
    ) as r on r.Oppo_OpportunityId = o.oppo_opportunityId AND r.capt_uk = 'Third'
    LEFT JOIN
    (
    SELECT oppo_opportunityId,capt_uk
    FROM
    (
    SELECT oppo_opportunityId,A.[oppo_dcl_multi], Split.a.value('.', 'VARCHAR(100)') AS String
    FROM
    (
    SELECT oppo_opportunityId,[oppo_dcl_multi],CAST ('' + REPLACE([oppo_dcl_multi], ',', '') + '' AS XML) AS String
    FROM Opportunity
    WHERE oppo_dcl_multi IS NOT NULL
    ) as A
    CROSS APPLY String.nodes ('/M') AS Split(a)
    ) as Stuf
    INNER JOIN Custom_Captions ON LTRIM(RTRIM(String)) = LTRIM(RTRIM(capt_code)) AND capt_family = 'lead_dcl_multi'
    WHERE COALESCE(String,'') <> ''
    ) as t on t.Oppo_OpportunityId = o.oppo_opportunityId AND t.capt_uk = 'Fourth'
    WHERE o.Oppo_deleted IS NULL AND oppo_dcl_multi IS NOT NULL
    ORDER BY o.Oppo_OpportunityId


    Which gives an output of:

    You could then write a case statement or concat the captions into one comma separated field.

    I could not find a simple way to split the comma separated values, then when it is split, they go onto their own row, so the multiple joins gets them back on the same row in separate columns, you can then concatenate.

    Hope that helps, if at least a little.