查询日期sql语句

tjm1 2002-04-09 07:18:44
怎么查询某个月份的数据
比如select * from table where :只要4月份的
...全文
68 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
liu7537 2002-04-09
  • 打赏
  • 举报
回复
不同意:
lgsdaly()的
“select * from tablename where convert(char(7),colname,111)=:mon;
如4月份:mon='2002/04'”方案


china_hfz(孔雀为什么东南飞)的
“select * from tablename where mid(convert(varchar(10,colname,201),6,2)=:mon;”方案!

我不同意上面两个方案,因为上面两个方案只是就事论事,如果在数据量比较小的时候可能看不出问题来,如果数据量达到每年500万条记录以上的时候,问题可就大发了!

因为他们都对数据库的字段增加了函数处理,在这种情况下SQL Server数据库是无法利用索引来加快搜索的,只能是全表扫描(full table scan)!

因此我建议使用 liangshi001(ls) 的方法。




china_hfz 2002-04-09
  • 打赏
  • 举报
回复
不对,应是:
select * from qf_bt where substring(convert(varchar(10),date,111),6,2)='04'
samuria 2002-04-09
  • 打赏
  • 举报
回复
要看你用什么数据库,不同的数据库的SQL语法不尽相同
如果是oracle
select * from tab where substr(to_char(colname,'yyyymmdd'),5,2)=:month;
如果是anywhere
select * from tab where month(colname)=:month;
china_hfz 2002-04-09
  • 打赏
  • 举报
回复
select * from tablename where mid(convert(varchar(10,colname,201),6,2)=:mon;
tjm1 2002-04-09
  • 打赏
  • 举报
回复
查了一下帮助,111是japan格式的
tjm1 2002-04-09
  • 打赏
  • 举报
回复
to: lgsdaly()
convert(char(7), colname, 111)
这个111有什么用?
rock_csdn 2002-04-09
  • 打赏
  • 举报
回复
提醒:不同的DBMS的类型转换不同。
sybase/MSS convert
oracle: to_char(adatetime, 'mm')
lgsdaly 2002-04-09
  • 打赏
  • 举报
回复
select * from tablename where convert(char(7),colname,111)=:mon;

如4月份:mon='2002/04'
tjm1 2002-04-09
  • 打赏
  • 举报
回复
to: supsuccess(口气不小~修炼中...)
你的语法不对,我是说在pb中用嵌入式sql语句。
tjm1 2002-04-09
  • 打赏
  • 举报
回复
to: supsuccess(口气不小~修炼中...)
你的语法不对
supsuccess 2002-04-09
  • 打赏
  • 举报
回复
int mon
mon=4
select * from tablename where month(colname)=:mon;
liangshi001 2002-04-09
  • 打赏
  • 举报
回复
sorry ,resend


data ld_stardate,ld_enddate
ld_stardate=data(string(year(today()))+'/'+string(month(today()))+'/1')
choose month(today())
case 1,3,5,7,8,10,12
ld_enddate=data(string(year(today()))+'/'+string(month(today()))+'/31')
case 4,6,9,11
ld_enddate=data(string(year(today()))+'/'+string(month(today()))+'/30')
case 2
if mid(year(today()))=0 then
ld_enddate=data(string(year(today()))+'/'+string(month(today()))+'/29')
else
ld_enddate=data(string(year(today()))+'/'+string(month(today()))+'/28')
end if
end choose
select * from table where col_date between :ld_stardate and ld_enddate using sqlca;



liangshi001 2002-04-09
  • 打赏
  • 举报
回复
data ld_stardate,ld_enddate
ld_stardate=data(string(year(today()))+string(month(today()))+'/1')
choose month(today())
case 1,3,5,7,8,10,12
ld_enddate=data(string(year(today()))+string(month(today()))+'/31')
case 4,6,9,11
ld_enddate=data(string(year(today()))+string(month(today()))+'/30')
case 2
if mid(year(today()))=0 then
ld_enddate=data(string(year(today()))+string(month(today()))+'/29')
else
ld_enddate=data(string(year(today()))+string(month(today()))+'/28')
end if
end choose
select * from table where col_date between :ld_stardate and ld_enddate using sqlca;
tjm1 2002-04-09
  • 打赏
  • 举报
回复
月份是动态的
wwxc18 2002-04-09
  • 打赏
  • 举报
回复
select *
from table
where between table.日期=4-1-xxxx to table.日期=4-30-xxxx

1,079

社区成员

发帖
与我相关
我的任务
社区描述
PowerBuilder 相关问题讨论
社区管理员
  • 基础类社区
  • WorldMobile
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧