Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 ORDER BY 2 DATE COLUMNNS

Author  Topic 

raviborra
Starting Member

14 Posts

Posted - 2008-08-13 : 09:15:52
Hi,

I have the ORDER BY clause like this

ORDER BY
QT.Created_Date,QT.Modified_Date,QTM.Modified_Date Desc;

Out of these i need to order lattest datetime from the given 3 datetime fields.

Can some one help me here.

THanks,
Ravi.

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-08-13 : 09:21:16
[code]DECLARE @Sample TABLE (i INT, j INT, k INT)

INSERT @Sample
SELECT 1, 2, 3 UNION ALL
SELECT 3, 4, 1 UNION ALL
SELECT 6, 1, 2 UNION ALL
SELECT 2, 3, 4

SELECT *
FROM @Sample
ORDER BY (SELECT MAX(d) FROM (SELECT i AS d UNION ALL SELECT j UNION ALL SELECT k) AS r) DESC[/code]


E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-08-13 : 09:26:16
[code]DECLARE @Sample TABLE (i INT, j INT)

INSERT @Sample
SELECT 1, 2 UNION ALL
SELECT 3, 4 UNION ALL
SELECT 6, 1 UNION ALL
SELECT 2, 3

SELECT *
FROM @Sample
ORDER BY (SELECT MAX(d) FROM (SELECT i AS d UNION ALL SELECT j) AS r) DESC

SELECT *
FROM @Sample
ORDER BY CASE
WHEN i < j THEN j
ELSE i
END DESC[/code]


E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page
   

- Advertisement -