Wednesday, March 21, 2012

order by

Hi everyone,
I have a select statement of the form
SELECT * FROM temp ORDER BY time

I have a scalar function ConvertToMinutes that takes in varchar and returns int, is there any way to do something like this SELECT * FROM temp ORDER BY ConvertToMinutes(time). I tried doing this and it doesn't work (it tells me ConvertToMinutes is not a built-in function). Please guide me as to how I would accomplish this. Thanks in advance.

P.S. Clarification: I am trying to order the table temp by the value returned by the function ConvertToMinutes on the coloumn time.I am not exactly sure if you can use function in the order by clause. try

SELECT * FROM temp ORDER BY dbo.ConvertToMinutes(convert(varchar,time))

|||

I do believe ndinakar's method will work, however, if it does not, you can also do this:

SELECT *
FROM (SELECT *,dbo.converttominutes(time) mytime FROM temp) t1
ORDER BY mytime

or this:

SELECT temp.*
FROM temp
JOIN (SELECT DISTINCT time,dbo.converttominutes(time) mytime FROM temp) t1 ON (temp.time=t1.time)
ORDER BY mytime

|||Hey guys thanks for the answers. I tried the first solution and it worked, so I did not try the second one. But thanks to both of you for answering.sql

No comments:

Post a Comment