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)
 which value is selected

Author  Topic 

esthera
Master Smack Fu Yak Hacker

1410 Posts

Posted - 2010-02-24 : 07:26:02
i am doing

select @startingtime=convert(VARCHAR(20),@mydate,102) + ' '+ convert(VARCHAR(20),starttime,108) from parkingtimes where site=@site order by starttime

I thought in this case the @startingtime would be = to the first entry shows ordered by starttime but it seems to not be doing

how do i get the smallest starttime?

pk_bohra
Master Smack Fu Yak Hacker

1182 Posts

Posted - 2010-02-24 : 07:41:28
quote:
Originally posted by esthera

i am doing

select @startingtime=convert(VARCHAR(20),@mydate,102) + ' '+ convert(VARCHAR(20),starttime,108) from parkingtimes where site=@site order by starttime

I thought in this case the @startingtime would be = to the first entry shows ordered by starttime but it seems to not be doing

how do i get the smallest starttime?




If you want to sort on first output column then Try using "Order by 1"
Go to Top of Page

esthera
Master Smack Fu Yak Hacker

1410 Posts

Posted - 2010-02-24 : 07:49:48
but i want the first one order by starttime - but only the first collumn returned
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2010-02-24 : 07:50:47
select @startingtime=convert(VARCHAR(20),@mydate,102) + ' '+ convert(VARCHAR(20),min(starttime),108) from parkingtimes where site=@site

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-02-24 : 09:53:03
quote:
Originally posted by esthera

i am doing

select @startingtime=convert(VARCHAR(20),@mydate,102) + ' '+ convert(VARCHAR(20),starttime,108) from parkingtimes where site=@site order by starttime

I thought in this case the @startingtime would be = to the first entry shows ordered by starttime but it seems to not be doing

how do i get the smallest starttime?



if you've multiple records satisfying this condition you'll get only a single value stored in your variable. if you specifically want first value use top 1

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

esthera
Master Smack Fu Yak Hacker

1410 Posts

Posted - 2010-02-24 : 10:04:50
what's the correct syntax for top 1 in this case?
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-02-24 : 10:08:31
[code]select top 1 @startingtime=convert(VARCHAR(20),@mydate,102) + ' '+ convert(VARCHAR(20),starttime,108) from parkingtimes where site=@site order by starttime[/code]

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2010-02-25 : 01:39:03
quote:
Originally posted by esthera

what's the correct syntax for top 1 in this case?


Why didn't you use my solution?

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

esthera
Master Smack Fu Yak Hacker

1410 Posts

Posted - 2010-02-25 : 01:46:29
Why didn't you use my solution?

sorry i missed that - i did top now - is that better to use then top? is any better?
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2010-02-25 : 02:57:04
quote:
Originally posted by esthera

Why didn't you use my solution?

sorry i missed that - i did top now - is that better to use then top? is any better?


If you use top you need order by too where in min you dont need

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

esthera
Master Smack Fu Yak Hacker

1410 Posts

Posted - 2010-02-25 : 03:05:37
thanks:)
Go to Top of Page
   

- Advertisement -