| Author |
Topic |
|
cwtriguns2002
Constraint Violating Yak Guru
272 Posts |
Posted - 2007-06-17 : 23:33:50
|
| hi all...DECLARE @paytype varchar(50)SET @paytype = 'W'select t.*, c.descriptionfrom HrSetTaxTable as tleft join hrsettaxcodes as c on t.taxcode = c.taxcodeWHERE t.taxpaytype = @paytype OR ....my problem is if @paytype is null, it returns all the data in HrSetTaxTable.'W' = weekly'M' = Monthly'D' = Daily'' = retrieves all the data from the tableThanks.-Ron-"If you can only access one site on the Internet, make it SQLTeam!" |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-06-17 : 23:39:59
|
quote: my problem is if @paytype is null, it returns all the data in HrSetTaxTable.'W' = weekly'M' = Monthly'D' = Daily'' = retrieves all the data from the table
You want to return all data when @paytype IS NULL or @paytype = '' ? KH |
 |
|
|
cwtriguns2002
Constraint Violating Yak Guru
272 Posts |
Posted - 2007-06-17 : 23:42:20
|
| '' --> is it not a NULL ?ok. both '' and null would retrieve all the data.Thanks.-Ron-"If you can only access one site on the Internet, make it SQLTeam!" |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-06-17 : 23:44:40
|
[code]WHERE ( @paytype is nullor @paytype = ''or (@paytype = 'D' and ... )or (@paytype = 'W' and ... )or (@paytype = 'M' and ... ))[/code] KH |
 |
|
|
cwtriguns2002
Constraint Violating Yak Guru
272 Posts |
Posted - 2007-06-17 : 23:48:38
|
| Doesn't work properly. it retrieves all the data even the paytype is 'W'.-Ron-"If you can only access one site on the Internet, make it SQLTeam!" |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-06-17 : 23:50:47
|
can you post your full query, table DDL, sample data and expected result KH |
 |
|
|
Koji Matsumura
Posting Yak Master
141 Posts |
Posted - 2007-06-17 : 23:53:36
|
| WHERE t.taxpaytype = @paytype OR IsNull(@paytype, '') = '' |
 |
|
|
cwtriguns2002
Constraint Violating Yak Guru
272 Posts |
Posted - 2007-06-18 : 00:03:47
|
| i think its complicated to use null/''. Ill change it to 'N'table - HrSetTaxTable taxcode paytype111 W222 W333 M444 M555 D666 D--expected result: if @paytype = 'W'taxcode paytype111 W222 W--if @paytype = 'N'taxcode paytype111 W222 W333 M444 M555 D666 Dthanks-Ron-"If you can only access one site on the Internet, make it SQLTeam!" |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-06-18 : 00:09:58
|
try thisdeclare @HrSetTaxTable table( taxcode int, paytype varchar(1))insert into @HrSetTaxTableselect 111, 'W' union allselect 222, 'W' union allselect 333, 'M' union allselect 444, 'M' union allselect 555, 'D' union allselect 666, 'D'declare @paytype varchar(1)-- select @paytype = NULL-- select @paytype = ''select @paytype = 'W'select *from @HrSetTaxTablewhere ( @paytype is nullor @paytype = ''or paytype = @paytype) KH |
 |
|
|
cwtriguns2002
Constraint Violating Yak Guru
272 Posts |
Posted - 2007-06-18 : 00:13:28
|
| thanks to both of u. KH and Koji.-Ron-"If you can only access one site on the Internet, make it SQLTeam!" |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-06-18 : 00:16:17
|
OR you can use these if you are not expecting @paytype = '' and your paytype column does not contain NULLselect *from @HrSetTaxTablewhere paytype = coalesce(@paytype, paytype) If you do want to handle @paytype = ''select *from @HrSetTaxTablewhere paytype = coalesce(nullif(@paytype, ''), paytype) KH |
 |
|
|
|