Help for a stored Procedure

Hi,

My Customer table stores the age or the birsthday of the customer but

one of them is set. I want to get the customer's name and age. So have

to compute the customer's age from his birthday if age is set to null

or get directly his age if birthday is set to null . I resolved my

problem by writing this C# code that modify a dataset wgich contains

the name, age and birthday columns.

Code :

// res is my dataset

DataColumn dc = new DataColumn("FinalAge", System.Type.GetType("System.Int32"));

//add my FinalAge column

res.Tables[0].Columns.Add(dc);

//and compute its value
foreach (DataRow row in res.Tables[0].Rows)
{
if (row["Age"].ToString()==string.Empty)
{
DateTime birth = Convert.ToDateTime(row["Birthday"]);
int years = DateTime.Now.Year - birth.Year;
if (DateTime.Now.Month < birth.Month || (DateTime.Now.Month == birth.Month && DateTime.Now.Day < birth.Day))
years--;

row["FinalAge"] = years;
}
else row["FinalAge"] = row["Age"];

}

Now I want to transform this code to a stored procedure or even a sql query if it is possible.

Any suggestion ?

Thanks in Advance

[1478 byte] By [FekihMehdi] at [2007-12-22]
# 1

select isnull(Age,datediff(year,Birthday,getdate())) [Age],.. from TblCustomer where...

this will get Age from column 'Birthday' if the column 'Age' is null

regards

mobin

mobin.p at 2007-8-30 > top of Msdn Tech,SQL Server,Transact-SQL...
# 2

Thanks mobin for the isnull Function

Code :

SELECT [Name],ISNULL([Age],DateDIFF(yy,[Birthday],getdate())-
CASE WHEN getdate()>=DateAdd(yy,DateDIFF(yy,[Birthday],getdate()),
[Birthday]) THEN 0 ELSE 1 END) AS Age FROM [TblCustomer]

FekihMehdi at 2007-8-30 > top of Msdn Tech,SQL Server,Transact-SQL...

SQL Server

Site Classified