0

we have a very old database table that is used for discount codes.

Right now we have columns called discountValue & discountPercent

When a user creates a voucher code, if it is a value amount the value is stored in the value column, and if it is a percentage value this value is stored in the percentage column.

We are using MSSQL 2012 now and have just realised about this problem as it makes the UI look quite awkward.

Is there a way within SQL for MSSQLServer to query the database and alias a column in the output that detects if one of the columns is empty, it will use store the value in 1 column we can use to output to the user ?

I imagine this is kind of like a conditional statement... but im not sure how to write such a thing in MSSQL ?

thanks in advance

2 Answers 2

2

You can use ISNULL or COALESCE.

SELECT ISNULL(column1, column2) AS some_column
FROM SomeTable
Sign up to request clarification or add additional context in comments.

1 Comment

Good information about COALESCE and ISNULL is at MSDN Blogs [here](blogs.msdn.com/b/sqltips/archive/2008/06/26/… ).
1

Use COALESCE() function:

SELECT COALESCE(discountValue, discountPercent) as Discount
FROM Table

See Link here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.