Click here to Skip to main content
15,904,153 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have datetime field in sql server

I am select records from sql table between two dates. I tried the following coding but it is not working. pls Please help me for solve this problem
select * from purchase_entry where  pur_date>='" + Convert.ToDateTime(TextBox1.Text).ToString("MM/dd/yyyy") + "' and pur_date<='" + Convert.ToDateTime(TextBox2.Text).ToString("MM/dd/yyyy") + "' order by pur_no

I also this coding too
select * from purchase_entry where pur_to='" + cbobranch.Text + "' and pur_date>='" + DateTime.Parse(TextBox1.Text).ToString("MM/dd/yyyy") + "' and pur_date<='" + DateTime.Parse(TextBox2.Text).ToString("MM/dd/yyyy") + "' order by pur_no

Please help me
Posted
Updated 14-Apr-12 4:46am
v2
Comments
[no name] 14-Apr-12 7:02am    
"it is not working" is not a very good description of your problem. WHAT is not working? The connection is not working? The date conversion is not working? The query is not working? The query works but does not return an records? How are we to know from "it is not working"?
devausha 14-Apr-12 7:04am    
which is not display any records for select date. But the table has records for particular dates. The date conversion in not working. Because it is display records without conditions for dates.

1 solution

If you want records that occur between two dates then use the BETWEEN keyword.
SQL
WHERE pur_date BETWEEN date1 AND date2


[Edit]
See if DateTime Format[^] helps.
 
Share this answer
 
v2
Comments
devausha 14-Apr-12 7:06am    
But the this query is will not display the date1 and date2 records. I can't find the correct report from this one
[no name] 14-Apr-12 7:19am    
See update
devausha 14-Apr-12 7:21am    
ok
devausha 14-Apr-12 7:22am    
Thanks for your help
[no name] 14-Apr-12 7:23am    
You're welcome

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