选读SQL经典实例笔记06_日期处理(上)

这篇具有很好参考价值的文章主要介绍了选读SQL经典实例笔记06_日期处理(上)。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

选读SQL经典实例笔记06_日期处理(上)文章来源地址https://www.toymoban.com/news/detail-553113.html

1. 计算一年有多少天

1.1. 方案

1.1.1. 找到当前年份的第一天

1.1.2. 加上1年以得到下一年的第一天

1.1.3. 得到的结果减去第一步得到的结果

1.2. DB2数据库

1.2.1.  sql语句

select days((curr_year + 1 year)) - days(curr_year)
   from (
 select (current_date -
         dayofyear(current_date) day +
          1 day) curr_year
   from t1
        ) x

1.3. Oracle数据库

1.3.1.  sql语句

select add_months(trunc(sysdate,'y'),12) - trunc(sysdate,'y')
   from dual

1.4. PostgreSQL数据库

1.4.1.  sql语句

select cast((curr_year + interval '1 year') as date) - curr_year
   from (
 select cast(date_trunc('year',current_date) as date) as curr_year
   from t1
        ) x

1.5. MySQL数据库

1.5.1.  sql语句

select datediff((curr_year + interval 1 year),curr_year)
   from (
 select adddate(current_date,-dayofyear(current_date)+1) curr_year
   from t1
        ) x

1.6. SQL Server数据库

1.6.1.  sql语句

select datediff(d,curr_year,dateadd(yy,1,curr_year))
   from (
 select dateadd(d,-datepart(dy,getdate())+1,getdate()) curr_year
   from t1
        ) x

2. 日期值里提取年月日时分秒

2.1. DB2数据库

2.1.1.  sql语句

select   hour( current_timestamp ) hr,
        minute( current_timestamp ) min,
        second( current_timestamp ) sec,
           day( current_timestamp ) dy,
         month( current_timestamp ) mth,
          year( current_timestamp ) yr
   from t1

2.2. Oracle数据库

2.2.1.  sql语句

select to_number(to_char(sysdate,'hh24')) hour,
        to_number(to_char(sysdate,'mi')) min,
        to_number(to_char(sysdate,'ss')) sec,
        to_number(to_char(sysdate,'dd')) day,
        to_number(to_char(sysdate,'mm')) mth,
        to_number(to_char(sysdate,'yyyy')) year
    from dual

2.3. PostgreSQL数据库

2.3.1.  sql语句

select to_number(to_char(current_timestamp,'hh24'),'99') as hr,
        to_number(to_char(current_timestamp,'mi'),'99') as min,
        to_number(to_char(current_timestamp,'ss'),'99') as sec,
        to_number(to_char(current_timestamp,'dd'),'99') as day,
        to_number(to_char(current_timestamp,'mm'),'99') as mth,
        to_number(to_char(current_timestamp,'yyyy'),'9999') as yr
   from t1

2.4. MySQL数据库

2.4.1.  sql语句

select date_format(current_timestamp,'%k') hr,
        date_format(current_timestamp,'%i') min,
        date_format(current_timestamp,'%s') sec,
        date_format(current_timestamp,'%d') dy,
        date_format(current_timestamp,'%m') mon,
        date_format(current_timestamp,'%Y') yr
   from t1

2.5. SQL Server数据库

2.5.1.  sql语句

select datepart( hour, getdate()) hr,
        datepart( minute,getdate()) min,
        datepart( second,getdate()) sec,
        datepart( day,   getdate()) dy,
        datepart( month, getdate()) mon,
        datepart( year, getdate()) yr
   from t1

3. 一个月的第一天和最后一天

3.1. DB2数据库

3.1.1.  sql语句

select (current_date - day(current_date) day +1 day) firstday,
        (current_date +1 month -day(current_date) day) lastday
   from t1

3.2. Oracle数据库

3.2.1.  sql语句

select trunc(sysdate,'mm') firstday,
        last_day(sysdate) lastday
   from dual

3.3. PostgreSQL数据库

3.3.1.  sql语句

select firstday,
        cast(firstday + interval '1 month'
                      - interval '1 day' as date) as lastday
   from (
 select cast(date_trunc('month',current_date) as date) as firstday
   from t1
        ) x

3.4. MySQL数据库

3.4.1.  sql语句

select date_add(current_date,
                 interval -day(current_date)+1 day) firstday,
        last_day(current_date) lastday
   from t1

3.5. SQL Server数据库

3.5.1.  sql语句

select dateadd(day,-day(getdate())+1,getdate()) firstday,
       dateadd(day,
               -day(getdate( )),
               dateadd(month,1,getdate())) lastday
   from t1

4. 当前月份的第一个和最后一个星期一

4.1. DB2数据库

4.1.1.     sql语句

with x (dy,mth,is_monday)
       as (
  select dy,month(dy),
          case when dayname(dy)='Monday'
               then 1 else 0
          end
     from (
   select (current_date-day(current_date) day +1 day) dy
     from t1
          ) tmp1
   union all
  select (dy +1 day), mth,
         case when dayname(dy +1 day)='Monday'
               then 1 else 0
         end
    from x
   where month(dy +1 day) = mth
  )
  select min(dy) first_monday, max(dy) last_monday
    from x
   where is_monday = 1

4.2. Oracle数据库

4.2.1. sql语句

select next_day(trunc(sysdate,'mm')-1,'MONDAY') first_monday,
       next_day(last_day(trunc(sysdate,'mm'))-7,'MONDAY') last_monday
  from dual

4.3. PostgreSQL数据库

4.3.1.   sql语句

select first_monday,
          case to_char(first_monday+28,'mm')
               when mth then first_monday+28
                        else first_monday+21
          end as last_monday
     from (
   select case sign(cast(to_char(dy,'d') as integer)-2)
               when  0
               then dy
              when -1
              then dy+abs(cast(to_char(dy,'d') as integer)-2)
              when 1
              then (7-(cast(to_char(dy,'d') as integer)-2))+dy
         end as first_monday,
         mth
    from (
  select cast(date_trunc('month',current_date) as date) as dy,
         to_char(current_date,'mm') as mth
    from t1
         ) x
         ) y

4.4. MySQL数据库

4.4.1.   sql语句

select first_monday,
          case month(adddate(first_monday,28))
               when mth then adddate(first_monday,28)
                        else adddate(first_monday,21)
          end last_monday
     from (
   select case sign(dayofweek(dy)-2)
               when 0 then dy
               when -1 then adddate(dy,abs(dayofweek(dy)-2))
              when 1 then adddate(dy,(7-(dayofweek(dy)-2)))
         end first_monday,
         mth
    from (
  select adddate(adddate(current_date,-day(current_date)),1) dy,
         month(current_date) mth
    from t1
         ) x
         ) y

4.5. SQL Server数据库

4.5.1.     sql语句

with x (dy,mth,is_monday)
       as (
   select dy,mth,
          case when datepart(dw,dy) = 2
               then 1 else 0
          end
     from (
   select dateadd(day,1,dateadd(day,-day(getdate()),getdate())) dy,
          month(getdate()) mth
    from t1
         ) tmp1
   union all
  select dateadd(day,1,dy),
         mth,
         case when datepart(dw,dateadd(day,1,dy)) = 2
              then 1 else 0
         end
    from x
   where month(dateadd(day,1,dy)) = mth
  )
  select min(dy) first_monday,
         max(dy) last_monday
    from x
   where is_monday = 1

5. 一年中所有的星期五

5.1. DB2数据库

5.1.1.     sql语句

with x (dy,yr)
       as (
   select dy, year(dy) yr
     from (
   select (current_date -
            dayofyear(current_date) days +1 days) as dy
     from t1
           ) tmp1
    union all
  select dy+1 days, yr
    from x
   where year(dy +1 day) = yr
  )
  select dy
    from x
   where dayname(dy) = 'Friday'

5.2. Oracle数据库

5.2.1.     sql语句

with x
       as (
   select trunc(sysdate,'y')+level-1 dy
     from t1
     connect by level <=
        add_months(trunc(sysdate,'y'),12)-trunc(sysdate,'y')
   )
   select *
     from x
   where to_char( dy, 'dy') = 'fri'

5.3. PostgreSQL数据库

5.3.1.  sql语句

select cast(date_trunc('year',current_date) as date)
          + x.id as dy
    from generate_series (
          0,
          ( select cast(
                   cast(
             date_trunc('year',current_date) as date)
                        + interval '1 years' as date)
                        - cast(
                   date_trunc('year',current_date) as date) )-1
         ) x(id)
  where to_char(
           cast(
     date_trunc('year',current_date)
                as date)+x.id,'dy') = 'fri'

5.4. MySQL数据库

5.4.1.   sql语句

select dy
     from (
   select adddate(x.dy,interval t500.id-1 day) dy
     from (
   select dy, year(dy) yr
     from (
   select adddate(
          adddate(current_date,
                  interval -dayofyear(current_date) day),
                 interval 1 day ) dy
    from t1
         ) tmp1
         ) x,
         t500
   where year(adddate(x.dy,interval t500.id-1 day)) = x.yr
         ) tmp2
   where dayname(dy) = 'Friday'

5.5. SQL Server数据库

5.5.1.     sql语句

with x (dy,yr)
       as (
   select dy, year(dy) yr
     from (
   select getdate()-datepart(dy,getdate())+1 dy
      from t1
           ) tmp1
    union all
   select dateadd(dd,1,dy), yr
   from x
   where year(dateadd(dd,1,dy)) = yr
  )
  select x.dy
    from x
   where datename(dw,x.dy) = 'Friday'
  option (maxrecursion 400)

6. 判断闰年

6.1. DB2数据库

6.1.1.  sql语句

 with x (dy,mth)
     as (
   select dy, month(dy)
     from (
   select (current_date -
            dayofyear(current_date) days +1 days)
             +1 months as dy
     from t1
           ) tmp1
   union all
  select dy+1 days, mth
    from x
   where month(dy+1 day) = mth
  )
  select max(day(dy))
    from x

6.2. Oracle数据库

6.2.1.  sql语句

select to_char(
          last_day(add_months(trunc(sysdate,'y'),1)),
         'DD')
   from t1

6.3. PostgreSQL数据库

6.3.1.   sql语句

select max(to_char(tmp2.dy+x.id,'DD')) as dy
     from (
   select dy, to_char(dy,'MM') as mth
     from (
   select cast(cast(
               date_trunc('year',current_date) as date)
                          + interval '1 month' as date) as dy
     from t1
           ) tmp1
          ) tmp2, generate_series (0,29) x(id)
    where to_char(tmp2.dy+x.id,'MM') = tmp2.mth

6.4. MySQL数据库

6.4.1.  sql语句

select day(
         last_day(
         date_add(
         date_add(
         date_add(current_date,
                  interval -dayofyear(current_date) day),
                  interval 1 day),
                  interval 1 month))) dy
   from t1

6.5. SQL Server数据库

6.5.1.     sql语句

with x (dy,mth)
       as (
   select dy, month(dy)
     from (
   select dateadd(mm,1,(getdate()-datepart(dy,getdate()))+1) dy
     from t1
          ) tmp1
    union all
   select dateadd(dd,1,dy), mth
    from x
   where month(dateadd(dd,1,dy)) = mth
  )
  select max(day(dy))
    from x

到了这里,关于选读SQL经典实例笔记06_日期处理(上)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 选读SQL经典实例笔记21_字符串处理

    2024年02月13日
    浏览(87)
  • 选读SQL经典实例笔记08_区间查询

    1.6.3.1. 即使同一天入职的员工不止一个人,也只会返回一个值 1.6.4.1. 使用MIN函数来确保只返回一个值 2.2.2.1. PROJ_START和PROJ_END的值决定哪些行属于同一个区间 2.2.2.2. 如果某一行的PROJ_START值等于上一行的PROJ_END值,那么该行就是“连续”的,或者说它属于某个组 3.4.1.1. ora

    2024年02月16日
    浏览(97)
  • 选读SQL经典实例笔记18_Exactly

    2024年02月14日
    浏览(72)
  • 选读SQL经典实例笔记16_逻辑否定

    2024年02月14日
    浏览(182)
  • 选读SQL经典实例笔记10_高级查询

    2024年02月17日
    浏览(96)
  • 选读SQL经典实例笔记14_层次查询

    2.6.1.1. sql 2.7.1.1. sql 3.5.1.1.  sql 4.5.1.1.  sql 5.5.1.1.  sql 6.7.1.1. Oracle Database 10g新增的CONNECT_BY_ROOT和CONNECT_BY_ISLEAF

    2024年02月15日
    浏览(104)
  • 选读SQL经典实例笔记15_窗口函数

    2.2.1.1. 分组不为空 2.2.1.1.1. 一个分组至少要拥有一个成员(行 2.2.1.1.2. 无法从一个空表中生成任何分组 2.2.1.2. 分组具有唯一性 2.2.1.2.1. 如果查询语句使用了GROUP BY子句,那么通常而言SELECT列表里就不再需要使用DISTINCT了 2.2.2.1. COUNT永远大于0 2.2.2.1.1. 无法从一个空表

    2024年02月14日
    浏览(39)
  • 选读SQL经典实例笔记01_检索和排序

    SMITH    800                           0 ALLEN   1600          300          1 WARD    1250         500          1 JONES   2975                          0 MARTIN  1250       1400         1 BLAKE   2850                         

    2024年02月11日
    浏览(116)
  • 选读SQL经典实例笔记22_2版增补

    4.1.3.1. 查找拼写不同但发音相同的字符串 6.1.1.1. 记录值的仪表存在误差 6.1.3.1. 意味着数据点是正确的,但应谨慎根据数据得出任何结论 9.1.2.1. 不使用CASE表达式或额外的连接操作

    2024年02月13日
    浏览(219)
  • 选读SQL经典实例笔记11_结果集变换

    3.6.1.1. 为了剔除掉Null,需要调用聚合函数MAX,并基于RN执行GROUP BY 3.9.1.1. 使用标量子查询基于EMPNO为每个员工排序 3.9.1.2. 针对标量子查询的返回值执行GROUP BY 3.9.1.3. 使用CASE表达式和聚合函数MAX实现结果集变换

    2024年02月16日
    浏览(80)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包