SELECT * FROM salesdetail where date<'"+DateTime.Now.Subtract(365).ToString()

Q)How to compare whether current date and date stored in salesdetail database table has difference of 365 days.
Please reply urgently.

Recommended Answers

All 4 Replies

use this

DateTime date2day = DateTime.Now;
            DateTime  dateFind = DateTime.Now.Subtract(TimeSpan.FromDays (365) )  ;
            MessageBox.Show(dateFind .ToString ());

Or if you want to do it in SQL:

SELECT * FROM salesdetail WHERE DATEDIFF(YEAR, Date, CURRENT_TIMESTAMP) = 1
-- OR
SELECT * FROM salesdetail WHERE DATEDIFF(DAY, Date, CURRENT_TIMESTAMP) = 365
String obj = "INSERT INTO OPENROWSET ('Microsoft.Jet.OLEDB.4.0', 'Excel 8.0;Database=d:\\contact.xls;','SELECT * FROM [Sheet1$]')SELECT * FROM salesdetail where date < '"+DateTime.Now.Subtract(TimeSpan.FromDays(365))+"'";
            try
            {
                conn = new SqlConnection(Properties.Settings.Default.conn.ToString());
                conn.Open();
                SqlCommand scom = new SqlCommand(obj, conn);
                scom.ExecuteNonQuery();
            }
            catch (Exception e1)
            {
                MessageBox.Show(e1.Message.ToString());
            }
            finally
            {
                conn.Close();
            }

I am trying to insert data from salesdetail table to excel sheet but the code reports following error: Insert Error: Column name or number of supplied values does not match table
definition.
Please help.

Simple way

DateTime date2day = DateTime.Now;
            DateTime  dateFind = DateTime.Now.Subtract(TimeSpan.FromDays (365) )  ;
            MessageBox.Show(dateFind .ToString ());

Use this dateFind in your query.

SELECT * FROM salesdetail where date<'"+dateFind .ToString ()

or convert it to datetime
Hope it will help

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.