Use if else statements to retrieve a certain column based on criteria and give it for display
-
Hey Guys, Here is my issue - I have a table with 3 date fields. (Entry Date, Pending Date, Accounting Date) -> in my GridView I need to represent this value in 1 column called (Report Date). My rules are if Accounting Date is null use Finalized Date, if Pending date is null use Entry Date. This rule has to work on a row by row basis as I am returning 100's of rows. Now if this is better done in the code - I can do that also. Any help would greatly be appreciated. Thank you.
-
Hey Guys, Here is my issue - I have a table with 3 date fields. (Entry Date, Pending Date, Accounting Date) -> in my GridView I need to represent this value in 1 column called (Report Date). My rules are if Accounting Date is null use Finalized Date, if Pending date is null use Entry Date. This rule has to work on a row by row basis as I am returning 100's of rows. Now if this is better done in the code - I can do that also. Any help would greatly be appreciated. Thank you.
Well, depending on whether or not your DBMS supports it, you could use the CASE statement to accomplish this, or you could use a COALESCE method,e.g.
SELECT COALESCE(AccountingDate, PendingDate, EntryDate) AS RuleDate ...
This assumes that one of these columns has a date value in it. Basically, coalesce falls through from one entry to the next until it finds a none-null value. BTW - I'm assuming that you got your logic a bit wrong in your statement and you mean Pending Date and not Finalized Date.
"WPF has many lovers. It's a veritable porn star!" - Josh Smith
-
Hey Guys, Here is my issue - I have a table with 3 date fields. (Entry Date, Pending Date, Accounting Date) -> in my GridView I need to represent this value in 1 column called (Report Date). My rules are if Accounting Date is null use Finalized Date, if Pending date is null use Entry Date. This rule has to work on a row by row basis as I am returning 100's of rows. Now if this is better done in the code - I can do that also. Any help would greatly be appreciated. Thank you.
CASE WHEN Accounting Date IS NULL THEN Finalized Date WHEN Pending date IS NULL THEN Entry Date END etc.