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 2000 Forums
 Transact-SQL (2000)
 CASE STMT (Urgent)

Author  Topic 

real_pearl
Posting Yak Master

106 Posts

Posted - 2004-10-30 : 06:04:03
Is it possible to use as variable's value if case stmt instead of a DB column. Like

declare @sql varchar(8000), @log char(1)
set @log = 'N'
set @sql = 'select case '+ @log + ' when ''N'' then T1 else LOG10(ABS(T1)) end as [1]
from Raw_Lexus_FET_DEV_ID_VG_scr_001
where id = 1'
print @sql
exec(@sql)

Arnold Fribble
Yak-finder General

1961 Posts

Posted - 2004-10-30 : 06:15:50
Let's see:

CREATE TABLE Raw_Lexus_FET_DEV_ID_VG_scr_001 (
id int NOT NULL,
N char(1) NOT NULL,
T1 float NOT NULL
)

INSERT INTO Raw_Lexus_FET_DEV_ID_VG_scr_001
SELECT 1, 'N', 3.5
UNION ALL SELECT 1, 'M', 12345.0
UNION ALL SELECT 2, 'X', 0.0

declare @sql varchar(8000), @log char(1)
set @log = 'N'
set @sql = 'select case '+ @log + ' when ''N'' then T1 else LOG10(ABS(T1)) end as [1]
from Raw_Lexus_FET_DEV_ID_VG_scr_001
where id = 1'
print @sql
exec(@sql)

Output:

select case N when 'N' then T1 else LOG10(ABS(T1)) end as [1]
from Raw_Lexus_FET_DEV_ID_VG_scr_001
where id = 1
1
-----------------------------------------------------
3.5
4.0914910942679512

Yup, works fine.
Go to Top of Page
   

- Advertisement -