Click here to Skip to main content
15,890,690 members
Please Sign up or sign in to vote.
4.20/5 (2 votes)
See more:
Hi guys,

I'm trying to implement an application for my web site , where I want to discount products 20% in their price which they are older than a year.

So I have a datatable products with columns [id,name,price,date]
with the following code i retrieve the current date and then I try to retrieve the date from my
sql table products and compare with the today date , but i don't now how to compare them and decide if that is an old product?
is this the correct way where i'm trying to do this?
what i shoud change in order to work this?

Thnx in advance!!!

C#
string todaydate = DateTime.Now.ToString("dd/MM/yyyy");

 SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ToString());

        string aSql = "select date from products";
         SqlCommand sqlCommand = new SqlCommand(aSql, con);
     
      
            con.Open();
            using (SqlDataReader read = sqlCommand.ExecuteReader())
            {
                while (read.Read())
                {
                    string date2 = read["date"].ToString();
                    string price = read["price"].ToString();

                    if(todaydate - date2 > )//12months i must writte something here)
                    {
                        int price2 = Convert.ToInt32(price) - ((20/100)*Convert.ToInt32(price));
                    }
                }
                GridView1.DataSource = read;
                GridView1.DataBind();
                read.Close();
                
            }
            con.Close();
Posted
Updated 26-Nov-13 8:02am
v3

You can add to the query the DateDiff function.
so:
SQL
select date from products
where DateDiff(m, date, GetDate()) < 13


In this case the differences in monthes between the DATE field and the current date ->GetDate() function is returnen in the result from the query. You have solved your problem in the select. No postprocessing with TimeSpan object is needed.
 
Share this answer
 
Comments
Maciej Los 26-Nov-13 14:00pm    
Good answer ;)
+5!
JasonTsoum77 26-Nov-13 14:23pm    
Great it works I have to change '<' compare to '>' because i want the old products but with your solution it works,
now the only think that i have is to add a discount for the products in column price.

Thnx again for your help my friend
Solution 1 by digimanus is very good, but i would suggest you to use stored procedure. For further information, please see: SQL SELECT statement with text field C# .NET[^]
SQL
SELECT [date], price, price * 20/100 AS NewPrice
FROM products
WHERE DateDiff(yy, [date], GETDATE()) = 1


More: DATEDIFF[^]
 
Share this answer
 
Comments
JasonTsoum77 26-Nov-13 14:25pm    
Thnx for your help, Now I will try it!
Herman<T>.Instance 26-Nov-13 14:31pm    
Good help given! Maciej
Maciej Los 26-Nov-13 14:33pm    
Thank you ;)
You can use DATEDIFF() function for that.

E.g.

SELECT DATEDIFF(day,'2008-06-05','2008-08-05') AS DiffDate


Result : DiffDate =61

For more info : SQL Server DATEDIFF() Function
 
Share this answer
 
Comments
JasonTsoum77 26-Nov-13 14:26pm    
Thnx for your help
Sampath Lokuge 26-Nov-13 15:50pm    
No problem :)
Herman<T>.Instance 26-Nov-13 14:32pm    
This is a fixed date solution, so not so usable
Sampath Lokuge 26-Nov-13 15:49pm    
I just showed the way where how to use the 'DATEDIFF()' function.So can be easily replaced it with the dynamic dates.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900