| Author |
Topic |
|
lols
Posting Yak Master
174 Posts |
Posted - 2007-11-01 : 08:39:55
|
| Hi,I have made a join between 2 tables TableA and TableBI want to check for the following :Insert into TableAselect isnull(TableA.fname,TableB.fname) || isempty(TableA.fname,TableB.fname), isnull(TableA.lname,TableB.lname)||isempty(TableA.fname,TableB.fname), isnull(TableA.age,TableB.age)||iszero(TableA.fname,TableB.fname)from TableA right join TableBOn TableA.id = TableB.idNow there isnt a function called isempty or iszero. How to implement this query properly. I want to basically check null and then empty or zero, for columns fname and lname, agethanks |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2007-11-01 : 08:42:40
|
| Replaceisempty(TableA.fname,TableB.fname), withcase when TableA.fname='' then TableB.fname else TableA.fname endMadhivananFailing to plan is Planning to fail |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2007-11-01 : 08:43:36
|
| Wait. Are you using SQL Server?MadhivananFailing to plan is Planning to fail |
 |
|
|
lols
Posting Yak Master
174 Posts |
Posted - 2007-11-01 : 09:52:57
|
| thanks madhivanan,yes, i use sql server 2005. Can I do something like :select isnull(TableA.fname,TableB.fname) || case when TableA.fname='' then TableB.fname else TableA.fname endthat is checking either of these 2 conditions |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2007-11-01 : 09:55:13
|
| what do you mean by ||?concatenation?MadhivananFailing to plan is Planning to fail |
 |
|
|
elancaster
A very urgent SQL Yakette
1208 Posts |
Posted - 2007-11-01 : 09:55:57
|
| isn't that oracle?Em |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2007-11-01 : 10:07:25
|
quote: Originally posted by elancaster isn't that oracle?Em
Thats why I asked OP MadhivananFailing to plan is Planning to fail |
 |
|
|
joldham
Wiseass Yak Posting Master
300 Posts |
Posted - 2007-11-01 : 11:18:23
|
| I think the statement below will work in SQL Server, assuming when you say "iszero" you are talking about the length:select case when TableA.fname='' then TableB.fname when Len(TableA.fname) = 0 then TableB.fname else isnull(TableA.fname,TableB.fname) endJeremy W. Oldham |
 |
|
|
Kristen
Test
22859 Posts |
Posted - 2007-11-01 : 13:00:00
|
| I'll use COALESCE instead of ISNULL for my example. You can use either, but there are "issues" with ISNULL.COALESCE(TableA.fname, TableB.fname)this will get you TableA.fname if it is not null, otherwise TableB.fname (whether it is NULL of not)COALESCE(NullIf(TableA.fname, ''), NullIf(TableB.fname, ''))This will convert TableA.fname and TableB.fname to NULL if they are empty. Then as above.COALESCE(NullIf(NullIf(TableA.fname, ''), '0'), NullIf(NullIf(TableB.fname, ''), '0'))Ditto, but also converts the string "0" to NULL.HOWEVER, this is only going to work if TableA.fname is a string datatype. And the "zero" match will only work if it contains exactly "0". If it contains " 0" that won't match.Kristen |
 |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
joldham
Wiseass Yak Posting Master
300 Posts |
Posted - 2007-11-01 : 14:06:44
|
| Thanks for the info Kristen. I have been writing CASE statements for a while. Looks like I need to become familiar with the COALESCE and NullIf functions. ;)Jeremy W. Oldham |
 |
|
|
|