Click here to Skip to main content
15,900,598 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i want to select all dates from db using query like
C#
string sql = "select * from   tblVehicle where CAST(floor( CAST( Vehentrytime AS FLOAT ) )AS DATETIME) = '" + myDate + "" + "'";

but i want my dates to be selected for a month which means i have to neglect the day value
my string holding datetime is
C#
string myDate = System.DateTime.Now.ToString("MM/dd/yyyy");


this works fine for above format but if i have to do selection of a month or last month or year how can i do this
please provide with the code
with regards bishnu karki
Posted
Updated 3-Nov-11 1:31am
v4

Try this

C#
string sql = "select * from   tblVehicle where Month(CAST(floor( CAST( Vehentrytime AS FLOAT ) )AS DATETIME)) = " + Convert.ToDateTime(myDate).Month ;
 
Share this answer
 
Hello friend use this query for your solution....


SQL
SELECT * from tblVehicle WHERE cast(month(CONVERT(datetime,Vehentrytime ,3)) as varchar(4)) = '11'


and pass your month from your code to get thats month records....

here i have give '11' as a month of november so as per your requirement you can pass your parameter value.....

i hope this will resolve your problem...
 
Share this answer
 
If you want to search for a particular year do this :

C#
string myDate = 2011 ;
string sql = "select * from   tblVehicle where DATEPART(yy, CAST(floor( CAST( Vehentrytime AS FLOAT ) )AS DATETIME)) = " + myDate ;


If you want to search for a particular month do this :
C#
string myDate = 2 ; // for February for example
string sql = "select * from   tblVehicle where DATEPART(mm, CAST(floor( CAST( Vehentrytime AS FLOAT ) )AS DATETIME)) = " + myDate ;


and read this :
http://msdn.microsoft.com/en-us/library/ms174420.aspx[^]

Hope it helps.
 
Share this answer
 

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