转载请注明转自:http://www.cnblogs.com/feiyumo/p/8760846.html

一、获取当前时间

1.current_date获取当前日期

2018-04-09

2.current_timestamp/now()获取当前时间

2018-04-09 15:20:49.247

二、从日期时间中提取字段

1.year,month,day/dayofmonth,hour,minute,second

Examples:> SELECT day('2009-07-30'); 30

2.dayofweek (1 = Sunday, 2 = Monday, ..., 7 = Saturday),dayofyear

Examples:> SELECT dayofweek('2009-07-30');   5

Since: 2.3.0

3.weekofyear

weekofyear(date) - Returns the week of the year of the given date. A week is considered to start on a Monday and week 1 is the first week with >3 days.

Examples:> SELECT weekofyear('2008-02-20');   8

4.trunc截取某部分的日期,其他部分默认为01

第二个参数 ["year", "yyyy", "yy", "mon", "month", "mm"]

Examples:

> SELECT trunc('2009-02-12', 'MM');
2009-02-01
> SELECT trunc('2015-10-27', 'YEAR');
2015-01-01

5.date_trunc ["YEAR", "YYYY", "YY", "MON", "MONTH", "MM", "DAY", "DD", "HOUR", "MINUTE", "SECOND", "WEEK", "QUARTER"]

Examples:> SELECT date_trunc('2015-03-05T09:32:05.359', 'HOUR');  2015-03-05T09:00:00

Since: 2.3.0

6.date_format将时间转化为某种格式的字符串

Examples:> SELECT date_format('2016-04-08', 'y');    2016

三、日期时间转换

1.unix_timestamp返回当前时间的unix时间戳

Examples:

> SELECT unix_timestamp();  1476884637
> SELECT unix_timestamp('2016-04-08', 'yyyy-MM-dd');   1460041200

2.from_unixtime将时间戳换算成当前时间,to_unix_timestamp将时间转化为时间戳

Examples:

> SELECT from_unixtime(0, 'yyyy-MM-dd HH:mm:ss');  1970-01-01 00:00:00
>SELECT to_unix_timestamp('2016-04-08', 'yyyy-MM-dd');  1460041200

3.to_date/date将字符串转化为日期格式,to_timestamp(Since: 2.2.0)

> SELECT to_date('2009-07-30 04:17:52');  2009-07-30
> SELECT to_date('2016-12-31', 'yyyy-MM-dd');   2016-12-31
> SELECT to_timestamp('2016-12-31 00:12:00');   2016-12-31 00:12:00

4.quarter 将1年4等分(range 1 to 4)

Examples:> SELECT quarter('2016-08-31');  3

四、日期、时间计算

1.months_between两个日期之间的月数

months_between(timestamp1, timestamp2) - Returns number of months between timestamp1 and timestamp2.

Examples:> SELECT months_between('1997-02-28 10:30:00', '1996-10-30');  3.94959677

2. add_months返回日期后n个月后的日期

Examples:> SELECT add_months('2016-08-31', 1);  2016-09-30

3.last_day(date),next_day(start_date, day_of_week)

Examples:

> SELECT last_day('2009-01-12');  2009-01-31
> SELECT next_day('2015-01-14', 'TU');  2015-01-20

4.date_add,date_sub(减)

date_add(start_date, num_days) - Returns the date that is num_days after start_date.

Examples:

> SELECT date_add('2016-07-30', 1);  2016-07-31

5.datediff(两个日期间的天数)

datediff(endDate, startDate) - Returns the number of days from startDate to endDate.

Examples:> SELECT datediff('2009-07-31', '2009-07-30'); 1

6.关于UTC时间

to_utc_timestamp

to_utc_timestamp(timestamp, timezone) - Given a timestamp like '2017-07-14 02:40:00.0', interprets it as a time in the given time zone, and renders that time as a timestamp in UTC. For example, 'GMT+1' would yield '2017-07-14 01:40:00.0'.

Examples:> SELECT to_utc_timestamp('2016-08-31', 'Asia/Seoul');  2016-08-30 15:00:0

from_utc_timestamp

from_utc_timestamp(timestamp, timezone) - Given a timestamp like '2017-07-14 02:40:00.0', interprets it as a time in UTC, and renders that time as a timestamp in the given time zone. For example, 'GMT+1' would yield '2017-07-14 03:40:00.0'.

Examples:> SELECT from_utc_timestamp('2016-08-31', 'Asia/Seoul');  2016-08-31 09:00:00

SPARK-SQL内置函数之时间日期类的更多相关文章

  1. Spark SQL内置函数

    Spark SQL内置函数官网API:http://spark.apache.org/docs/latest/api/scala/index.html#org.apache.spark.sql.fun ...

  2. [转] Spark sql 内置配置(V2.2)

    [From] https://blog.csdn.net/u010990043/article/details/82842995 最近整理了一下spark SQL内置配.加粗配置项是对sparkSQL ...

  3. sql内置函数pivot强大的行转列功能

    原文:sql内置函数pivot强大的行转列功能 语法: PIVOT用于将列值旋转为列名(即行转列),在SQL Server 2000可以用聚合函数配合CASE语句实现 PIVOT的一般语法是:PIVO ...

  4. SQL入门(2): Oracle内置函数-字符/数值/日期/转换/NVL/分析函数与窗口函数/case_decode

    本文介绍Oracle 的内置函数. 常用!  一. 字符函数 ASCII 码与字符的转化函数 chr(n)   例如 select chr(65) || chr(66) || chr(67) , ch ...

  5. 【翻译】Flink Table Api & SQL — 内置函数

    本文翻译自官网:Built-In Functions  https://ci.apache.org/projects/flink/flink-docs-release-1.9/dev/table/fu ...

  6. Oracle内置函数:时间函数,转换函数,字符串函数,数值函数,替换函数

    dual单行单列的隐藏表,看不见 但是可以用,经常用来调内置函数.不用新建表 时间函数 sysdate 系统当前时间 add_months 作用:对日期的月份进行加减 写法:add_months(日期 ...

  7. Oracle SQL 内置函数大全

    SQL中的单记录函数 1.ASCII 返回与指定的字符对应的十进制数;SQL> select ascii('A') A,ascii('a') a,ascii('0') zero,ascii(' ...

  8. Oracle SQL 内置函数大全(转)

    SQL中的单记录函数 1.ASCII 返回与指定的字符对应的十进制数;SQL> select ascii('A') A,ascii('a') a,ascii('0') zero,ascii(' ...

  9. CSIC_716_20191213【内置函数exec、元类】

    In memory of the more than 300 thousand Chinese murdered exec( 字符类型的代码,全局变量,局部变量 ).其中,全局变量和局部变量可以写成字 ...

随机推荐

  1. Leetcode438.Find All Anagrams in a String找到字符串中所有字母异位词

    给定一个字符串 s 和一个非空字符串 p,找到 s 中所有是 p 的字母异位词的子串,返回这些子串的起始索引. 字符串只包含小写英文字母,并且字符串 s 和 p 的长度都不超过 20100. 说明: ...

  2. SSM7-nginx的反向代理和负载均衡

    1. 反向代理 1.1. 什么是反向代理 正向代理 反向代理: 反向代理服务器决定哪台服务器提供服务. 返回代理服务器不提供服务器.也是请求的转发. 1.2. Nginx实现反向代理 两个域名指向同一 ...

  3. R语言中如何使用最小二乘法

    R语言中如何使用最小二乘法 这里只是介绍下R语言中如何使用最小二乘法解决一次函数的线性回归问题.         代码如下: > x<-c(6.19,2.51,7.29,7.01,5.7, ...

  4. jQuery控制导航条样式

    原理:点击当前元素时,当前元素添加(样式类),父辈的兄弟姐妹的孩子('a')去掉此样式类. 代码如下: /*次要导航*/ $(".subnav li a").click(funct ...

  5. 2019-7-3-WPF-使用-Composition-API-做高性能渲染

    title author date CreateTime categories WPF 使用 Composition API 做高性能渲染 lindexi 2019-07-03 10:30:57 +0 ...

  6. I Love Palindrome String

    I Love Palindrome String 时间限制: 2 Sec  内存限制: 128 MB 题目描述 You are given a string S=s1s2..s|S| containi ...

  7. 【CodeVS】1083 Cantor表

    1083 Cantor表 1999年NOIP全国联赛普及组 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 白银 Silver 题目描述 Description 现代数学的著名证明之 ...

  8. ubuntu16.04上在使用搜狗输入法时,按shift不能正常切换中英文

    问题描述: ubuntu16.04上在使用搜狗输入法时,不知道把什么组合键给错按了,导致了按shift不能正常切换中英文,这是一件很烦恼的事儿! 解决步骤: 1,终端输入打开: fcitx-confi ...

  9. 模拟21 题解(waiting)

    留坑待填 效率!!! 题还没改Oh,NO!!!

  10. 当spark遇见hbase

    一.使用sbt引入hbase依赖包 "org.apache.hbase" % "hbase-server" % "2.1.0", " ...