如何从一个月的数据库中获取所有日期 [英] how to get all dates from database of a month
问题描述
我想使用
这样的查询从数据库中选择所有日期
i want to select all dates from db using query like
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
string myDate = System.DateTime.Now.ToString("MM/dd/yyyy");
这适用于上述格式,但是如果我必须选择一个月或上个月或一年,我该怎么做
请提供代码
关于bishnu karki
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
推荐答案
尝试一下
Try this
string sql = "select * from tblVehicle where Month(CAST(floor( CAST( Vehentrytime AS FLOAT ) )AS DATETIME)) = " + Convert.ToDateTime(myDate).Month ;
朋友您好,请使用此查询作为解决方案....
Hello friend use this query for your solution....
SELECT * from tblVehicle WHERE cast(month(CONVERT(datetime,Vehentrytime ,3)) as varchar(4)) = '11'
并从您的代码中传递您的月份,以获取多数民众赞成的月份记录....
在这里,我将11月定为"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...
如果您要搜索特定年份,请执行以下操作:
If you want to search for a particular year do this :
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 :
string myDate = 2 ; // for February for example
string sql = "select * from tblVehicle where DATEPART(mm, CAST(floor( CAST( Vehentrytime AS FLOAT ) )AS DATETIME)) = " + myDate ;
并阅读:
http://msdn.microsoft.com/en-us/library/ms174420.aspx [ ^ ]
希望对您有所帮助.
and read this :
http://msdn.microsoft.com/en-us/library/ms174420.aspx[^]
Hope it helps.
这篇关于如何从一个月的数据库中获取所有日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!