0
SELECT 
    'https://xxxxxx/' + CONVERT(VARCHAR(10), [AnnouncementID]) AS 'URL', 
    [Status] AS 'Code', 
    [Subject] AS 'Text', 
    (CASE 
        WHEN LEN([Subject]) > 500 
           THEN SUBSTRING([Subject], 0, 500) + '...' 
           ELSE [Subject] END) 
    AS 'ShortText'
FROM 
    [Announcement] 
WHERE 
    [Public] = 1 
    AND [Enable] = 1 
    AND GETDATE() BETWEEN [ActiveFrom] AND [ActiveTo] 
ORDER BY 
    [Status] ASC, [ActiveFrom] ASC

This query returns something like:

Url                 Code        Text    ShortText
-------------------------------------------------
https://xxxxxx/2    Enable      Text    Text
https://xxxxxx/1    Critical    Text    Text

I would like to Automatic change the text in [Code].

  • If Critical -> RED
  • If Enabled -> YELLOW

How can I rewrite my query string above?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459

3 Answers3

1

use a CASE:

CASE [Status]
    WHEN 'Enable' then 'YELLOW'
    WHEN 'Critical'  then 'RED'
END as 'Code'

or use a JOINED table with its values.

McNets
  • 10,352
  • 3
  • 32
  • 61
0

If I understand correctly; you want to change the words Enable and Critical to YELLOW and RED respectively?

You can do this with a case statement, as you haven't tagged what DBMS you are using I can't really help with syntax but a rough idea would be;

SELECT 'https://xxxxxx/' + CONVERT(VARCHAR(10), [AnnouncementID]) AS 'URL', 
Case [Status]
  when 'Critical' then 'RED'
  when 'Enable' then 'YELLOW'
  end AS 'Code' , 
[Subject] AS 'Text', 
(CASE WHEN LEN([Subject]) > 500 THEN SUBSTRING([Subject], 0, 500) + '...' ELSE [Subject] END) AS 'ShortText' 

FROM [Announcement] 

WHERE [Public] = 1 
AND [Enable] = 1 
AND GETDATE() BETWEEN [ActiveFrom] AND [ActiveTo] 

ORDER BY [Status] ASC, [ActiveFrom] ASC

It's effectively saying if [Status] has the value 'Critical' display 'RED' or if has the value 'Enable' then display 'YELLOW'

0

Check out this answer. It references this article. With some minor modifications, adding a few CASE statements based on the value of [Code] you could add a <td style="color: red;"> or a <td style="color: yellow;">.

I hope that helps!

Community
  • 1
  • 1
grom
  • 71
  • 5