关于SQL和HQL时间类型的模糊查询

今天用time  Like   ' 2008-06-01% ' 语句来查询该天的所有数据,被提示语句错误。查了一下才发现该模糊查询只能用于String类型的字段。


自己也查阅了一些资料。关于时间的模糊查询有以下三种方法:


 

1 .Convert转成String,在用Like查询。

select   *   from  table1    where   convert ( varchar ,date, 120 like     ' 2006-04-01% '   

 

2 . Between

select   *   from  table1  where  time  between   ' 2006-4-1 0:00:00 '   and   ' 2006-4-1 24:59:59 ' ";

 

3   datediff ()函数

select * from table1   where datediff(day,time,'2006-4-1')=0



 

第一种方法应该适用与任何数据类型;

第二种方法适用String外的类型;

第三种方法则是为date类型定制的比较实用快捷的方法


二.对应在hibernate里的HQL方法为:

//根据日期和姓名返回CustomerInfo集合   convert(varchar,created_time,120) like   '2017-05-05%'   c.createdTime
	@SuppressWarnings("unchecked")
	@Override
	public List<CustomerInfo> getCustomerInfoByDate(String date,String name) {
		String hql="from CustomerInfo c where convert(varchar,c.createdTime,120) like '"+date+"%' and c.saleman='"+name+"'";
			return this.getHibernateTemplate().find(hql);
		}



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值