Wednesday, February 3, 2010

Fetch Data from one table except data in other table

I have two tables

T1 Columns A,B
------------------------
Data 1,2
1,3
3,5
4,6
7,6

T2 Columns A,B
------------------------
Data 1,2
1,5
4,6

Expected Result : Select data from T1, which is not preset in T2
Result Data: A,B
--------------------------
1,3
3,5
7,6


To work out such problem we have 'Minus' operator in oracle, but in MS Sql, we can solve in the following ways......



Solution 1
***********************
Select a,b from T1
Where Not Exists
(Select 1 from T2 Where T1.A = T2.A and T1.B = T2.B)



Solution 2
***********************
select a,b from t1
except
select a,b from t2

Thursday, January 7, 2010

How to check before conversion from one data type to another to avoid runt time error ? (C#)

For example conversion from string to integer
string s="123";
int i = Convert.toInt32(s.ToString()); // no error
but if s="abc";
and int i = Convert.ToInt32(s.ToString()); // run time error
so its always better to check before conversion as
int res;
bool NoError = Int32.TryParse(s.ToString(), out res)
if(!NoError ){ Console.WriteLine("Conversion is not possible"); }
else{ i = Convert.ToInt32(s.ToString());}