francis
francis

Reputation: 710

Merge two DataColumns of type DateTime

i have a datatable which contains two Datacolumns (date_start,date_sent) and i wanted to merge them into one single column (date_order) to then apply a sort like this:

DataRow[] dtSorted = dt.Select(null, "date_order DESC",DataViewRowState.CurrentRows);

Since whenever date_start is null, date_sent is not and vice versa, i tried the following expression to the datacolumn:

dt.Columns.Add("date_order", typeof(String), "IIF(date_start=NULL,date_sent,date_start)");

but its not working.Tried something like "ISNULL(date_start,date_sent),date_start" and "date_start + date_sent" but they didn't work either.

Can someone please tell me what expression i should use? Thank you.

Upvotes: 0

Views: 252

Answers (2)

Vishal
Vishal

Reputation: 12369

If you are doing it in C#...?? is the Null Coalescing operator you should be looking for I think...basically equivalent to isnull in tsql.

Upvotes: 0

ozziepeeps
ozziepeeps

Reputation: 108

You should use the SQL COALESCE keyword, which returns the first non-null argument, e.g.:

SELECT COALESCE(date_order, date_sent) ...

That will return the first non-null out of date_order and date_sent

Upvotes: 1

Related Questions