how do I code the query for getting data from table where datadate = valueofdatepicker or < 15 days


something like

SELECT * FROM EMPLOYEE WHERE Hireddate BETWEEN ( @date and @date-15days) <---how do I subtract the 15 days?

Recommended Answers

All 3 Replies

You can use Timespan to represent the number of days to subtract from a DateTime

DateTime dt15DaysAgo = DateTime.Now.Subtract(TimeSpan.FromDays(15));

Seems like in MSSQL that if you just subract an integer from a datetime it defaults to subtracting days. Not sure about MySQL though. Let me know if you have any success with just using an integer. Also, apperantly if you use a decimal it defaults to represent a fraction of a day (ie 0.5 would be 12 hours).

how do I code the query for getting data from table where datadate = valueofdatepicker or < 15 days
something like
SELECT * FROM EMPLOYEE WHERE Hireddate BETWEEN ( @date and @date-15days) <---how do I subtract the 15 days?

If you mean to get some data based on two conditions, two dates actually (between those two dates), then you can do it:

string query = @SELECT * FROM EMPLOYEE WHERE Hireddate >= @date1 and AND Hiredate <= @date1)
SqlConnection sqlConn = new SqlConnection("connString");
SqlCommand cmd = new  SqlComamnd(query, sqlConn);
cmd.Parameters.Add("@date1", SqlDbType.DateTime).Value = dateTimePicker1.Value;
cmd.Parameters.Add("@date1", SqlDbType.DateTime).Value = dateTimePicker1.Value.AddDays(15); //if you want to add 15 days on dtp selection!
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.