Sample Header Ad - 728x90

Creating an if then clause in SQL

0 votes
2 answers
155 views
I'm fairly new to SQL and I am am used to writing formulas in Crystal Reports. I want to recreate the following formula from Crystal Reports into an SQL statement to create a column:
if (not isnull ({OT.TITLE})) and (not isnull ({OA.LASTNAME}))then {OA.LASTNAME}& ", "& {OT.TITLE} else
if (not isnull ({OT.TITLE})) and (isnull ({OA.LASTNAME}))then {OT.TITLE} else ""
I created the following statement, but it only works if the title and last name fields are both filled out. Otherwise there is an unnecessary ,:
SELECT concat(OA.Lastname, ', ', OT.Title)
FROM OA
LEFT JOIN OT 
    ON OT.ObjectID = OA.ObjectID
Basically I need a column that shows the Last name and title separated by a comma. If there is no Last name, then show the title. If there is no title then show nothing, even if there is a last name. I would really appreciate any help you can give me. I am unfortunately working with SQL 2014.
Asked by Ariane Loersch (1 rep)
Dec 15, 2021, 02:36 PM
Last activity: Dec 16, 2021, 06:07 PM