Access
Access 数据库的Date格式字段值必须由一对 # 符号标识,即当Access与日期常量比较的时候,日期常量应由##包围。
举例一:
查找2001-01-01至2001-01-31(即该年一月份)生日的客户资料:
1.Select * From Customer Where BirthDay
Between #2002-01-01# and #2002-01-31#
2.SELECT * FROM Customer WHERE Year(Birthday)=2001 and
month(Birthday)=1 AND
Day(Birthday)>=1 AND Day(Birthday)<=31
3.SELECT * FROM Customer WHERE FORMAT(Birthday,"yymmdd")
BETWEEN "20010101" AND "20010131"
如果放入字符串内,注意双引号要变成单引号,应改为:
string strSql = "SELECT * FROM Customer WHERE FORMAT(Birthday,'yymmdd')
BETWEEN "20010101" AND "20010131"";
举例二:
往表Customer表插入日期数据:
1.常量插入:
insert into Customer(Name, Birthday) values('Tom' ,#2002-01-32#);
2.变量插入:
insert into Customer(Name, Birthday) values('Tom' ,'" + DateTime.Now.ToString("d") + "');
注意下变量参数化,插入日期时,要转成字符串,否则报错。(附录日期转字符串方法:http://blog.csdn.net/kkfdsa132/archive/2010/03/20/5399168.aspx)
同理,变量参数化更新,也一样。
Update Customer set Birthday='"+ DateTime.Now.ToString("d") +"' where Name='Tom';
举例三:
Access日期变量比较:从表Customer中查找生日日期为“85-3-18”的顾客姓名
string strTime = "85-3-18";
string strSql= "select Name from Customer where FORMAT(Birthday,'yy-mm-dd')='"+ strTime +"' ";
Access日期变量的处理,要先转化,后比较,如上。
其他例子,如下:
FORMAT(Birthday,'yymmdd')
Year(Birthday)
Day(Birthday)
month(Birthday)
再说说一个注意事项:
1.如果你的列名,是关键字。比如Time ,那么应该加上转义字符[],如[Time],否则会失败。
下面附录Access日期函数(用法看举例一):
ACCESS字段中设置默认日期值函数大全
年Year(Now())
季度DatePart("q",Now())
月Month(Now())
周数 DatePart("ww",Now())
日Day(Now())
星期几DatePart("w",Now(),2)
时Hour(Now())
分Minute(Now())
秒Second(Now())
当前时间Now()
当前日期Date()