当前位置:编程学习 > VB >>

用sql语句如何表达,急!

文本框填入月份(1-12)数,然后通过sql语句查询数据库中在相应月份期间的记录,即从月头到月尾的记录,要如何表达,谢谢!! --------------------编程问答-------------------- select * from 表名 where month(时间字段)=月份 --------------------编程问答-------------------- strsql="select * from tablename where month(日期字段)="& text1.text &""
--------------------编程问答-------------------- 日期字段的数据类型为:日期/时间即YYYY-MM-DD --------------------编程问答-------------------- 如你的日期字段为date 格式为 2007-10-08 选择10月份的记录
select * from table where substring(date,6,2) = 10 
--------------------编程问答-------------------- 以上语法针对日期字段为char类型的
如果你的日期字段为 date类型
可以直接用
select  xsdate from sale  wheres DATEPART(mm,xsdate) = 10 --------------------编程问答-------------------- 你们都不管数据是那一年的吗? --------------------编程问答-------------------- 如你的日期字段为date 格式为 2007-10-08 选择10月份的记录
select * from table where substring(date,6,2) = 10  
按这个方法发现错误:substring子程序或函数未定义,这是什么原因 --------------------编程问答-------------------- substing 是sql server 2000的语法,不提交给sql执行的。不知道你的程序是怎么写的,用的是什么数据库? --------------------编程问答-------------------- CHR(35) 是 # 的Asc值, bwmain 是表名, inday 是字段名 

'指定某一天 
reco = "select * from bwmain where inday =" & Chr(35) & Text1.Text & Chr(35) 

'指定两日期之间 

aa=cstr(year(now)) & "/" & text1 & "/1"
bb=cstr(year(now)) & "/" & text1 & "/31"
'text1.text是你输入的月份
reco = "select * from bwmain where inday between " & Chr(35) & aa & "# and #" & bb & Chr(35) 

--------------------编程问答-------------------- 不能用substring估计是Access数据库:

dim sql as string
sql="select * from tb where format(日期字段,'yyyy-mm')='" & year(date) & "-" & text1 & "'"
--------------------编程问答-------------------- 呵呵,这样:
Dim begindate As Date, enddate As Date
Dim strSQL As String

begindate = DateSerial(Year(Date), Val(Text1), 1)           '当月初
enddate = DateSerial(Year(Date), Val(Text1) + 1, 1) - 1     '当月末


strSQL = "SELECT * FROM tablename WHERE datefield BETWEEN #" & Format(begindate, "yyyy-mm-dd") & "# AND #" & Format(enddate, "yyyy-mm-dd") & "#" 
--------------------编程问答-------------------- 哈哈!谢谢,各位大师,问题已解决! --------------------编程问答-------------------- 还有个问题如何取当月的最迟日期,用SQL如何表达 --------------------编程问答-------------------- 排序了
补充:VB ,  数据库(包含打印,安装,报表)
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,