Skip to content
Advertisement

Building complex SQL command

I am using sql for select, update, insert and some other features, but only simple ones until now. Now I need to write a complex command.

I have looked at using case but I do not know how to implement it.

This is what it should look like:

I used ~TACNO~ to point out where I need a conditional value.

How I would write the condition in C# is like this (I will use column names from table as variables):

How can I write the equivalent of this C# condition in SQL query?

Advertisement

Answer

You can use a searched CASE like this:

You could also do it as a single when clause, by joining the conditions using OR, but I think that is less readable:

But as suggested by Arioch’The in the comments: consider normalizing your design, so you don’t have repeating columns in a single table, but instead multiple rows with a discriminator column. It would simplify things like this.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement