使用sqlserver convert()函数
合集下载
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
e_format
//查询2010年2月创建的学生 List students = session.createQuery("select s.id, from Student s where date_format(s.createTime, '%Y-%m')=?") .setParameter(0, "2010-02") .list();
使用sqlserver convert()函数
由于在hql查询中使用 strToDate()函数,出现字符串转化为时间错误的问题,同事建议是sqlserver中自带的convert()函数。不过在存入 的时候 strToDate()有没有发生异常,也没有明白原因。这里是excel的导入书库的功能。 strToDate()函数: /** * 时间转换器 * 将字符时间装换成date类型 * @param strDate * @return */ private Date strToDate(String strDate) { SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); ParsePosition pos = new ParsePosition(0); Date strtodate = formatter.parse(strDate,pos); return strtodate; } hql查询语句并返回list集合: private List<Cost> checkCost(String typeName,String amount,String orderNo,String costTime,String applicant) throws Exception{ List<Cost> costList=(List<Cost>)costDao.find("from Cost as cost where cost.isdeleted="+PropertyEnum.normal+" and ='"+typeName+"'" +"and cost.amount='"+amount+"'"+" and cost.orderNo='"+orderNo+"'"+" and CONVERT(VARCHAR(10),cost.costDate,120)='"+costTime+"'"+" and ='"+applicant+"'" ); return costList; } 说明:当时在costDate的查询是使用这样的"and cost.costDate='"+strToDate(costTime)"' 现在想来应该是没有多了一个""的问题,将转化得到的日期类型又转化为字符串类型,回头测试一下,waiting...... 不是多了""的问题,删了直接就变成了字符串而非参数,因为这是使用的字符串拼接来传的参数。So,我有去看了hql传递参数的几种写 法 按照我的想法应该是这样来写:
这是别人总结的网址:/s/blog_44a059590100qmn8.html 为了试验是否能匹配到想要的yyyy-mm-dd的日期,我在我的sqlserver 中做了一个测试表test中只有一个date字段,存入了一个数据 2015-08-09 12:00:00.000 新建查询:select test.date from test where CONVERT(VARCHAR(10),test.date,120)='2015-08-09' 因为要匹配是yyyy-mm-dd的日期,所以varchar的取值为10,。 得到结果:
convert()的第一个参数是查询出来的要去的长度 第二个参数是要转换的值或者字段 第三个是日期的格式
格式:
//查询2010年2月创建的学生 List students = session.createQuery("select s.id, from Student s where date_format(s.createTime, '%Y-%m')=?") .setParameter(0, "2010-02") .list();
使用sqlserver convert()函数
由于在hql查询中使用 strToDate()函数,出现字符串转化为时间错误的问题,同事建议是sqlserver中自带的convert()函数。不过在存入 的时候 strToDate()有没有发生异常,也没有明白原因。这里是excel的导入书库的功能。 strToDate()函数: /** * 时间转换器 * 将字符时间装换成date类型 * @param strDate * @return */ private Date strToDate(String strDate) { SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); ParsePosition pos = new ParsePosition(0); Date strtodate = formatter.parse(strDate,pos); return strtodate; } hql查询语句并返回list集合: private List<Cost> checkCost(String typeName,String amount,String orderNo,String costTime,String applicant) throws Exception{ List<Cost> costList=(List<Cost>)costDao.find("from Cost as cost where cost.isdeleted="+PropertyEnum.normal+" and ='"+typeName+"'" +"and cost.amount='"+amount+"'"+" and cost.orderNo='"+orderNo+"'"+" and CONVERT(VARCHAR(10),cost.costDate,120)='"+costTime+"'"+" and ='"+applicant+"'" ); return costList; } 说明:当时在costDate的查询是使用这样的"and cost.costDate='"+strToDate(costTime)"' 现在想来应该是没有多了一个""的问题,将转化得到的日期类型又转化为字符串类型,回头测试一下,waiting...... 不是多了""的问题,删了直接就变成了字符串而非参数,因为这是使用的字符串拼接来传的参数。So,我有去看了hql传递参数的几种写 法 按照我的想法应该是这样来写:
这是别人总结的网址:/s/blog_44a059590100qmn8.html 为了试验是否能匹配到想要的yyyy-mm-dd的日期,我在我的sqlserver 中做了一个测试表test中只有一个date字段,存入了一个数据 2015-08-09 12:00:00.000 新建查询:select test.date from test where CONVERT(VARCHAR(10),test.date,120)='2015-08-09' 因为要匹配是yyyy-mm-dd的日期,所以varchar的取值为10,。 得到结果:
convert()的第一个参数是查询出来的要去的长度 第二个参数是要转换的值或者字段 第三个是日期的格式
格式: