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

Bogdan Bogdanov
1,7232 gold badges21 silver badges31 bronze badges
asked Nov 9, 2013 at 2:21

2 Answers 2

2

You can use ISNULL or COALESCE.

SELECT ISNULL(column1, column2) AS some_column
FROM SomeTable
answered Nov 9, 2013 at 2:26

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

answered Nov 9, 2013 at 2:26

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.