MPetrovic
MPetrovic

Reputation: 171

How to create column in SQL query with custom text

Is it possible to create custom column with sql query, and in that query to add some custom text, to say e.g what type of error is in that query, or i need to create table, where i will insert types of errors, and after with joins to add that column ?

E.g query would look like this (without adding that extra column that will say what is type of error, because, i don't know is it possible to do that)

SELECT id,aa,bb,NULL AS cc, NULL AS dd
FROM test
WHERE aa=SomeRequirement AND 
      bb=SomeRequirement

UNION ALL

SELECT id,NULL,NULL,cc,dd
FROM test2
WHERE cc=SomeRequirement AND
      dd=SomeRequirement

and wanted result is

id| TextForError               |  aa      |   bb    |    cc    |    dd
1 | thisRowIsNotGoodBecause..  |  someData| someData| someData | someData

And one more question, if i need to create table with errors that i wanted to display, how is possible to add that error , if i don't have keys that will reference typical error for that row?

Upvotes: 0

Views: 19130

Answers (3)

ArifMustafa
ArifMustafa

Reputation: 4965

Condition:

If the Columns ColCompletedBy and ColCompletedAt are not null, so I have to show 'ARRIVED' and else if not meet the condition, 'NOT ARRIVED' will show in custom Column ColCustomStatus.

(SELECT VanColId,
    CASE
        WHEN ColCompletedBy IS NOT NULL AND ColCompletedAt IS NOT NULL THEN 'ARRIVED'
        ELSE 'NOT ARRIVED'
    END AS ColCustomStatus
FROM VanTableEvents) vanEvt

Note: vanEvt is the alias name for table VanTableEvents and this query is tested with MS SQL Server 2017

Hope it will hope many one.

Upvotes: 0

MPetrovic
MPetrovic

Reputation: 171

@Strawberry wrote what is needed, it's only need to put

SELECT id,'my text error' as error,aa,bb,NULL AS cc, NULL AS dd
FROM test
WHERE aa=SomeRequirement AND 
      bb=SomeRequirement

And this will work job that i want.

Upvotes: 2

NCRANKSHAW
NCRANKSHAW

Reputation: 1

Do you mean something like this?

    SELECT
    id,
    CASE
    WHEN condition1 THEN 'Error Type 1'
    WHEN condition2 THEN 'Error Type 2'
    ELSE 'Unknown Error Type'
    END as Error,
    aa,
    bb,
    cc,
    dd

Upvotes: 0

Related Questions