如何获得今天 - “1 天"sparksql 中的日期? [英] How to get today -"1 day" date in sparksql?

查看:50
本文介绍了如何获得今天 - “1 天"sparksql 中的日期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在sparksql中获取current_date - 1天,与mysql中的cur_date()-1相同.

How to get current_date - 1 day in sparksql, same as cur_date()-1 in mysql.

推荐答案

算术函数允许您对包含日期的列执行算术运算.

The arithmetic functions allow you to perform arithmetic operation on columns containing dates.

例如,您可以计算两个日期之间的差值、向日期添加天数或从日期中减去天数.内置日期算术函数包括datediffdate_adddate_subadd_monthslast_daynext_daymonths_between.

For example, you can calculate the difference between two dates, add days to a date, or subtract days from a date. The built-in date arithmetic functions include datediff, date_add, date_sub, add_months, last_day, next_day, and months_between.

除了上面我们需要的是

date_sub(timestamp startdate, int days),目的:减去指定的天数来自 TIMESTAMP 值.第一个参数可以是一个字符串,即如果使用可识别的格式,则自动转换为 TIMESTAMP,如TIMESTAMP 数据类型中描述.返回类型:返回 > 开始前的天数

date_sub(timestamp startdate, int days), Purpose: Subtracts a specified number of days from a TIMESTAMP value. The first argument can be a string, which is automatically cast to TIMESTAMP if it uses the recognized format, as described in TIMESTAMP Data Type. Return type: Returns the date that is > days days before start

我们有

current_timestamp() 目的: now() 函数的别名.返回类型:时间戳

current_timestamp() Purpose: Alias for the now() function. Return type: timestamp

你可以选择

date_sub(CAST(current_timestamp() as DATE), 1)

参见 https://spark.apache.org/docs/1.6.2/api/java/org/apache/spark/sql/functions.html

这篇关于如何获得今天 - “1 天"sparksql 中的日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆