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)
 select case select

Author  Topic 

jamie
Aged Yak Warrior

542 Posts

Posted - 2008-04-30 : 04:19:49
hi, what am I doing wrong here :

declare @heading varchar
SET @heading = 'Today'

select case when @heading = 'Today' THEN
select getdate() as [date]
end

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-04-30 : 04:24:13
declare @heading varchar
SET @heading = 'Today'

select case when @heading = 'Today' THEN
getdate()
end as [date]



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-04-30 : 04:24:44
declare @heading varchar
SET @heading = 'Today'

select case @heading when 'Today' THEN getdate() end as [date]



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

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-04-30 : 04:25:09
quote:
Originally posted by jamie

hi, what am I doing wrong here :

declare @heading varchar(length)SET @heading = 'Today'

select case when @heading = 'Today' THEN
select getdate() as [date]
end




two things.
1.You need to specify length for varchar variable
2.You dont need select in red
Go to Top of Page

jamie
Aged Yak Warrior

542 Posts

Posted - 2008-04-30 : 04:29:05
thanks guys, I ll try those suggestions.
Go to Top of Page

jamie
Aged Yak Warrior

542 Posts

Posted - 2008-04-30 : 04:31:26
ok, that works.
however, how about :
declare @heading varchar
SET @heading = 'people'

select case when @heading = 'people' THEN
select * from people
end


select case when @heading = 'accounts' THEN
select * from accounts
end
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-04-30 : 04:33:41
[code]declare @heading varchar(20)
SET @heading = 'people'

IF @heading = 'people'
select * from people

IF @heading = 'accounts'
select * from accounts[/code]


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

jamie
Aged Yak Warrior

542 Posts

Posted - 2008-04-30 : 04:35:19
THANKS ! I didn't know I could use IF in T-SQL !
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2008-04-30 : 09:14:24
quote:
Originally posted by jamie

ok, that works.
however, how about :
declare @heading varchar
SET @heading = 'people'

select case when @heading = 'people' THEN
select * from people
end


select case when @heading = 'accounts' THEN
select * from accounts
end



Vishak already pointed out that you should use length for your varchar datatype
Also refer
http://sqlblogcasts.com/blogs/madhivanan/archive/2007/12/04/column-length-and-data-length.aspx

Madhivanan

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

- Advertisement -