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

Retinanet训练自己的数据(2):模型准备   在上一篇博客中,我们已经处理好了模型需要的数据集,下一步就是准备模型去训练数据。这里提醒一句,在制作数据时,最好将训练集和测试集人为分开。像Google tensorflow API,在制作数据时可以按比例自动生成,但是在上一篇文章中产生的数据(annotation.csv )其实都是用来训练的,如果你把自己的数据集全都转换了,那么恭喜你,最后你会发现没有数据集去测试了。   下面开始模型准... 阅读详情

 

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
傻白入门芯片设计,wafer/die/chip/cell(一) 芯片设计的一些基本概念 阅读详情

相关推荐

最优化理论与自动驾驶(三):LQR原理、公式及代码演示

LQR 是基于线性系统和二次型代价函数的最优控制方法,在实际工程应用中,我们一般采用离散时间系统的LQR设计,其目标是通过设计反馈控制律,使系统在离散时间步长内稳定运行,最小化某个二次型代价函数。给定一个离散时间系统,其状态和控制输入通过以下方程表示:是时刻k的系统状态,是时刻k的控制输入,是状态转移矩阵,是输入矩阵。系统描述的物理意义:系统状态通过矩阵A演变到下一时刻,同时控制输入通过矩阵B影响系统的状态。

a8598671的博客 4594

SQLServer游标(Cursor)简介和使用说明 及全局变量说明和功能

游标(Cursor)是处理数据的一种方法,为了查看或者处理结果集中的数据,游标提供了在结果集中一次以行或者多行前进或向后浏览数据的能力。我们可以把游标当作一个指针,它可以指定结果中的任何位置,然后允许用户对指定位置的数据进行处理。       1.游标的组成       游标包含两个部分:一个是游标结果集、一个是游标位置。       游标结果集:定义该游标得SELECT语句返回

lscbfntxgt的博客 2552

IATF16949核心职位全解析

TM / Top MgmtMR / QMRProcess OwnerQMQAQCSQECQEPQEPETEMEIEPM / PJMFMEA ModeratorMSA CoordinatorSPC CoordinatorCM / CCB内审员CP / CPK

weixin_49938552的博客 972

postgre获取后一年的日期怎么写

你可以使用 PostgreSQL 的 date_trunc 函数来获取后一年的日期。 SELECT date_trunc('year', CURRENT_DATE) + INTERVAL '1 year'; 这个查询会将当前日期设为下一年的第一天(即,第一个月的第一天)。 如果你想获取的是从当前日期开始的后一整年的日期,你可以使用下面的查询: SELECT CURRENT_DATE + INTE...

weixin_35750747的博客 677

MyEclipse 2017 CI 10安装与激活

本文介绍MyEclipse 2017 CI 7、CI 8、CI 9和CI 10的安装与激活。此方法理论上应该能激活MyEclipse 2017 CI所有系列,即激活方法是通用的,只是激活文件略有不同。本文提供MyEclipse 2017 CI 7、CI 8、CI 9和CI 10的激活文件。说明:博主所提供的压缩包格式均为RAR5,即WinRAR 5.0以上的版本才能正常解压,别的压缩软件博主不太清...

weixin_42539046的博客 9416

【推荐100个unity插件】将UE的资源导出到unity使用—— Exporter for Unreal to/for Unity插件

Unreal to Unity导出器是一款帮助开发者将Unreal Engine项目转换为Unity的工具。文章详细介绍了使用步骤:1)安装虚幻引擎并创建项目;2)在Plugins文件夹中安装导出插件;3)导入UE资产到Content文件夹;4)配置导出参数并完成转换。文末推荐了多个Unity开发专栏,涵盖C#编程、2D/3D开发、特效制作等内容。作者向宇提供了博客地址,欢迎读者交流学习。该工具能有效提升项目迁移效率,适合需要跨引擎开发的用户使用。

向宇的博客,专注php/web全栈 unity游戏开发,欢迎大家评论纠错 846

PgSQL时间函数

1.3 当前时间 - 时间段。1.1 当前时间日期函数。1.2 当前时间+时间段。1.4 时间函数进阶。

m0_70271240的博客 2282

PLC集群管理:C#上位机实现10台以上分布式PLC的集中监控与指令下发

本文介绍了一套基于C# WPF开发的PLC集群管理系统,用于解决现代工业自动化产线中分布式PLC监控难题。系统采用三层解耦架构,支持100台以内不同品牌PLC的集中管理,具备批量数据采集、指令下发、实时报警等功能。核心创新点包括:1)基于异步IO的连接池管理;2)统一通信接口设计;3)寄存器合并优化算法;4)三级优先级指令队列。实际应用表明,系统数据采集延迟低于100ms,指令下发成功率99.9%,有效解决了传统单PLC监控模式存在的效率低下问题。

这里只有干货:从 Modbus 通信到 MES 对接,从 YOLO 训练到工控机部署,带你搞定工业软件开发全流程。 506

PostgreSql时间和日期字符串转换函数

标准的PostgreSQL语法进行开发

m0_56062333的博客 1万+

LoveSQL

LoveSQL(SQL注入详解) 1、根据题目,显然想到的是sql注入漏洞,直接尝试万能密码 2、此时url构造,查看字段数 /check.php?username=admin' order by 3%23&password=1 # 存在 /check.php?username=admin' order by 4%23&password=1 # 报错 因此,得到总共有三个字段数 注意:url中不能用#,应该用url编码%23 3、用union来查询测试注入点,查看回显点

lunan的博客 1561

fatal: unable to access ‘https://github.com/Stability-AI/stablediffusion.git/‘解决方案

本文主要介绍在安装 stable-diffusion-webui 时出现的 fatal: unable to access 'https://github.com/Stability-AI/stablediffusion.git/': Failed to connect to github.com port 443 after 21071 ms: Couldn't connect to server 问题的解决方案,希望能对你有所帮助。

水滴的博客 5684

pg_sql关于时间的函数

作用:提取时间中某些内容(可以实现和date_part类似功能)时间戳转日期(时间戳为数值类型,若为字符型需进行转换)作用:获取日期对应的参数是当前年的第几(周、月、季)格式:日期 +/- interval '加减值'格式:date_trunc(('参数',日期))格式:extract(提取值 from 时间)格式:date_part('参数',日期)作用:获取日期对应参数的起始。作用:实现时间日期的加减。

weixin_57876654的博客 3358

创建 Fiori Space 时看不到传输请求的根因与排查路径

摘要: 在创建Fiori Space时,若Transport字段下拉列表为空,通常是因为系统未找到符合要求的传输请求。主要原因是: 请求类型不匹配:需使用Customizing request而非Workbench request。 用户权限问题:当前用户需在请求下有可用的task。 客户端或系统不一致:请求需在当前客户端和正确系统(如后端)中创建。 解决方法: 在当前客户端和系统中新建Customizing request,确保包含用户task。 检查是否为前后端分离架构,确保请求创建在正确的系统。 验证

2007 年 ~ 2025 年,深耕 SAP 技术 18 年 106

选读SQL经典实例笔记23_读后总结与感想兼导读

读后总结与感想兼导读

lyingSeven的博客 252

Statistics and Machine Learning Toolbox

Statistics and Machine Learning Toolbox(带书签),MATLAB统计学习工具

ssm框架搭建

这个是一个在idea上成功跑起来的ssm框架项目,保存在这里方便以后使用直接下载

sql内置函数(五)

SQL内置函数(五) • TAN TAN函数返回一个角度的正切值。 其具体的语法格式如下: TAN(angle) 其中: angle 用弧度表示。可参考函数COS了解从弧度到 角度的转换 下面是该函数的使用情况: TAN(225*3.1415926535/180)=1 TAN(0)=0 SQL内置函数 • TANH TANH函数返回一个角度的十六进制正切值。 其具体的语法格式如下: TANH(angle) 其中: angle 用弧度表示。可参考函数COS了解从弧度到

ljl_5085的博客 514

文档:亲测可用解决vnc灰屏,黑屏,鼠标大黑叉情况

主要解决vnc-viewer灰屏,黑屏,鼠标大黑叉等现象,详细见文档。

Presto 时间、日期及计算相关日期

由于工作中在数据迁移,大数据平台数据查询引擎使用Presto,和传统的数据库时间函数有区别,整理一版,供大家参考,一起学习,有错误欢迎指正。1、查询当前日期 select current_date; 2、查询当前时间 select current_timestamp; _col0 --------------------------------------- 2022-01-02 20:45:58.551

8610

win10 跳过U8环境检测的IE WebControls.rar

win10 安装u8的时候 提示IE WebControls 不通过,这个工具可以欺骗u8的检查。 跳过U8环境检测的IE WebControls。提示导入失败后,重新检测通过。

PostgreSQL获得去、今、明年份、今年的第一天、去年的第一天转换时区、最后一天等

pgsql获得去、今、明年份、今年的第一天、去年的第一天获取今年的第一天获取去年的第一天获得今年的年份获得去年与明年的年份 获取今年的第一天 SELECT date_trunc('year',CURRENT_DATE) 获取去年的第一天 select date_trunc('year',now() + '-1 year') 获得今年的年份 select to_char(current_date,'yyyy') select extract(year from now()) select to_c

要有梦想,即使遥远 1万+

postgreSQL格式化时间的函数详解

数据类型格式化函数:     PostgreSQL格式化函数提供一套有效的工具用于把各种数据类型(日期/时间、integer、floating point和numeric)转换成格式化的字符串以及反过来从格式化的字符串转换成指定的数据类型。下面列出了这些函数,它们都遵循一个公共的调用习惯:第一个参数是待格式化的值,而第二个是定义输出或输出格式的模板。 函数 返回类型 描述

snn1410的专栏 27万+

SQL Cookbook 系列 - 日期操作

确定一年是否是闰年 确定一年内的天数 从日期中提取时间的各部分 确定某个月的第一天和最后一天 确定一年内属于周内某天的所有日期 确定某月内第一个和最后一个周内某天的日期 创建日历 列出一年中每个季度的开始日期和结束日期 确定某个给定季度的开始日期和结束日期 填充丢失的日期 按照给定的时间单位进行查找 使用日期的特殊部分比较记录 识别重叠的日期范围   1.确定一年是否是闰...

seacean2000的专栏 426

postgresql日期格式转换

--上月天数 select EXTRACT(DAY from date_trunc('month', CURRENT_DATE)- interval '0 month'- interval '1 day');  --当前时间 select now(); --当前年月日 select current_date 当前年当前月 select extract(year fro

o0wufan0o的专栏 8万+

PostgreSQL date_trunc() 截断日期函数,完成定时时间语法.

摘要:Oracle有大量的日期函数可供使用,trunc, new_time,months_between,next_day,last_day,add_months,round等函数.当然PostgreSQl 也有大量的时间函数,详情请移步postgresql时间日期函数总结. 所以在此只说明Postgresql 中的TRUNC取断函数.  1.首先介绍一下Oracle的trunc函数: selec...

liguangxianbin的博客 10万+

[presto(trino)]多种日期写法

显示当前日期 select current_date 精确到毫秒,并显示时区 select now() 显示前一天 select current_date - interval '1' day 4.字符串转时间 select date_parse('20210512','%Y%m%d') 时间转字符串 select date_format(current_date -interval '1' day,'%Y-%m-%d %T') 计算时间的函数 date_add(uni.

数据打工人的博客 1万+

Psql 日期处理

Psql 日期处理 1.获取最近n天的数据 //包括当天 where date_part('day',current_date - onedate) >= 0 and date_part('day',current_date - onedate) < n; //不包括当天 where date_part('day',current_date - onedate) > 0 a...

伍文君的博客 1096

Flink SQL 连接器实战:MySQL Source、Flink CDC 与 JDBC Sink 全解析

提供有界流读取,适合批处理和维表场景,简单直接但无法感知变更。MySQL CDC提供无界流读取,通过「全量快照 + binlog 增量」的方式实现实时捕获,是实时数仓的核心入口,但需要 MySQL 侧正确配置 binlog 和权限。JDBC Sink负责结果回写,关键在于区分仅插入流和更新流,更新流必须依赖主键完成 upsert。掌握这三者的差异,本质上就是掌握了 Flink 中「有界与无界」「追加与更新」这两组最基本的语义。

2401_83857914的博客 419

数据库乐观锁深度解析:MySQL、PostgreSQL 实战 + Spring Boot 集成指南

Oracle 提供了ORA_ROWSCN伪列(System Change Number),记录最后修改行的 SCN,类似 PostgreSQL 的xmin。实践中通常仍使用 version 字段方案,逻辑与 MySQL 完全相同。-- Oracle:通过 ORA_ROWSCN 实现乐观锁-- 更新时比对 SCN// 自定义注解// AOP 切面@Aspect@Component@Slf4jtry {

yjc_1111的专栏 392

XSS 过滤器 vs SQL 注入防护:自动化测试平台中的

通过分析 AutoTest Platform 的源码可以看出,在实际开发中合理运用字符转义、参数化查询和白名单策略能够有效减少 Web 安全风险。对于非科班出身或刚刚入门的技术人员来说,理解这些安全措施的具体实现方式是非常重要的一步。建议读者可以尝试以下几个方向深入学习:1. 学习 Jsoup 库及 Whitelist API 的用法,并尝试自定义一套适用于自身项目的 HTML 清理规则;

后台模板 224
上一篇: 选读SQL经典实例笔记05_日期运算(下)
下一篇: 选读SQL经典实例笔记07_日期处理(下)
躺柒
博客等级 码龄4年 7004粉丝 1317原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值