Uri Dimant
3/26/2007 12:03:00 PM
Richard
Yoy don't need CAST to be used since you have a CASE expression for every
column.
declare @sortby varchar(10)
set @sortby='c1'
select * from #t order by case @sortby when 'c1' then c1 end,
case @sortby when 'c2' then c2 end,
case @sortby when 'c3' then c3 end
<richard@ultraitsolutions.com> wrote in message
news:1174910309.809556.108710@l77g2000hsb.googlegroups.com...
> On 26 Mar, 13:44, "Uri Dimant" <u...@iscar.co.il> wrote:
>> > This doesn't make sense, if I do it this way, wouldn't @sortby always
>> > be c1?
>>
>> Hmm, you have to specify a value for this variable according what theb
>> user
>> pushed. Stored procedure might get a paramer for sorting, that's all
>>
>> To call stored procedure
>>
>> EXEC spbalabala @par1=1,@par2=3,@sortby='c2' --'c1'....
>>
>> <rich...@ultraitsolutions.com> wrote in message
>>
>> news:1174908738.503852.54320@l77g2000hsb.googlegroups.com...
>>
>>
>>
>> > On 26 Mar, 12:58, "Uri Dimant" <u...@iscar.co.il> wrote:
>> >> Hi
>> >> create table #t (c1 int,c2 char(1),c3 datetime)
>>
>> >> insert into #t values (10,'a','20070101')
>> >> insert into #t values (2,'b','20070201')
>> >> insert into #t values (300,'d','20070103')
>> >> insert into #t values (9,'c','20060103')
>>
>> >> declare @sortby varchar(10)
>> >> set @sortby='c1'
>>
>> >> select * from #t order by
>> >> case when @sortby='c1' then c1 end,
>> >> case when @sortby='c2' then c2 end,
>> >> case when @sortby='c3' then c3 end
>>
>> >> <rich...@ultraitsolutions.com> wrote in message
>>
>> >>news:1174905893.930284.8090@y80g2000hsf.googlegroups.com...
>>
>> > This doesn't make sense, if I do it this way, wouldn't @sortby always
>> > be c1?- Hide quoted text -
>>
>> - Show quoted text -
>
> Thanks to yuo both,
>
> I think I have 'sorted' it. I sort of used a combination of both
> answers:
>
> ORDER BY
> CASE WHEN @SortBy = 'ApplicantID' THEN Cast(convert(varchar(4),
> ApplicantID) as Int) end,
> CASE WHEN @SortBy = 'LastName' THEN CONVERT(varchar(50), LastName)
> end,
> CASE WHEN @SortBy = 'MaximumPrice' THEN Cast(Convert(varchar(4),
> MaximumPrice) as Int) end,
> CASE WHEN @SortBy = 'RegistrationDate' THEN Cast(convert(VarChar(8),
> RegistrationDate, 112) as Int) end,
> CASE WHEN @SortBy = 'Status' THEN CONVERT(varchar(50), Status)
> END
>