Search code examples
sqlsql-serversql-server-2012coalesce

SQL: Nesting coalesce and convert


The version is SQL Server 2012 with SQL Server Management Studio

My assignment requires that I create a table with the columns PeopleID, HireDate and TermDate. TermDate is to be renamed to Current and have Null values coalesced into 'Current Employee'. The problem here being that TermDate is a Null column and needs to be converted into a text column in order to be coalesced...

I have tried numerous adjustments, but I cant seem to figure it out. This is my current iteration. Right now I get the error

Msg 102, Level 15, State 1, Line 5
Incorrect syntax near 'TermDate'.

Code:

select 
   PeopleID
   ,HireDate
   ,coalesce(convert(varchar(10), TermDate, 1) TermDate, 'Current Employee') as [Current]
from 
   WORKERS

Help is appreciated


Solution

  • select  PeopleID
            ,HireDate
            ,case 
                when TermDate is NULL then 'Current Employee')
                else convert(varchar(10), TermDate, 1)
             end as [Current]
        from WORKERS