sql中关于日期范围查询

1、查询startTime-endTime时间范围

(例:传过来的startTime=2020-12-30;endTime=2022-6-13)

(数据库中create_time=2021-12-13 15:14:49)

注:1、CONCAT()函数:是拼接日期+时间;

        2、CAST()函数:将AS前面的数据转为datetime类型;

<where>
            <if test="startTime != null  and startTime != '' " >
               and a.create_time <![CDATA[ >= ]]> CAST(CONCAT(#{startTime},' 00:00:00') AS datetime)
            </if>
            <if test="endTime != null  and endTime != '' " >
               and a.create_time <![CDATA[ <= ]]> CAST(CONCAT(#{endTime},' 23:59:59') AS datetime)
            </if>
        </where>

2、查询近一个月、近三个月、近一年时间内数据

(range=(1代表近一个月;2代表近3个月;3代表近一年))

注:1、&gt:是大于号的意思;

        2、GETDATE():返回当前本地日期和时间;

 <where>
            <if test="range != null  and range != '' " >
                <choose>
                    <when test="range==1">
                        and a.create_time <![CDATA[ > ]]>; dateadd(mm, -1, GETDATE())
                    </when>
                    <when test="range==2">
                        and a.create_time &gt; dateadd(mm, -3, GETDATE())
                    </when>
                    <when test="range==3">
                        and a.create_time &gt; dateadd(mm, -12, GETDATE())
                    </when>
                </choose>
            </if>
        </where>

  • 5
    点赞
  • 38
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值