0 votes
in Rock by (13.1k points)
edited

Originally posted by Unknown

I am writing a SQL query that returns attribute values from an attribute that is a single or multi select. The problem is the displayed text and stored value are different, like this:

enter image description here

So when I try to query the values in SQL it looks like this:

enter image description here

Is there a way to get the text value in SQL?

1 Answer

0 votes
by (13.1k points)
edited
 
Best answer

Originally posted by Unknown

You can use the following sub-query to get the text value:

(   SELECT [Text]
    FROM ( 
        SELECT 
               TRIM(LEFT(Value,CHARINDEX('^',Value)-1)) AS [Value]
           ,TRIM(RIGHT(Value,LEN(Value)-CHARINDEX('^',Value))) AS [Text]
        FROM string_split((
           SELECT AQ.[Value]
           FROM Attribute A
                  INNER JOIN AttributeQualifier AQ ON AQ.AttributeId = A.Id
           WHERE A.Id = AV.[AttributeId]
                      AND AQ.[Key] = 'values'), ',')
              ) T
    WHERE [Value] = AV.[Value] ) AS [TextValue]

If you are using an table alias different than "AV" for your Attribute Value table, you'll need to change that in the two places it appears in the sub query (AV.[AttributeId] and AV.[Value])

enter image description here

by (13.1k points)
(Originally by Unknown) Thanks!
by (13.1k points)
(Originally by Unknown) @MasonKinyon You're welcome
Welcome! Here you can ask questions and receive answers (hopefully) from other members of our team.
...