Wednesday, March 21, 2012

Order by 2 column ?

i have i tabletab1 and 2 columnsdate1<b> and <b>date2

i want order this cloumns but i have one thing

* if date1 < date2 then order by date2

* if date2 < date1 then order by date1

how i do it .?

thanxYour question does not make much sense. Can you provide some sample data, and how you want it organised/sorted?

It's easy, using Case statement, to work out which of the two conditions is true, but that doesn't really help us sort the data.

Do you mean that you want to sort the records by "date", and that the "date" of each record should be the larger of the two possible values?

Cheers
Ken|||ex.

my table name is:tab1
columns in table are:Date1,Date2
the data type ofDate1 andDate2 is DateTime

i want to disply this table in datagrid...it OK
but i want to:
If the ColumnDate1 Bigget thanDate2 then
i want order my data by Data1
otherwise,
If the ColumnDate2 Bigget thanDate1 then
i want order my data by Data2

i want but this in storedprocedure|||You can do this:


SELECT
some columns
FROM
some table
WHERE
some condition
ORDER BY
CASE WHEN DATEDIFF(d,Date1,Date2) > 0 THEN Date2 ELSE Date1 END

No comments:

Post a Comment