梧桐數(shù)據(jù)庫與GBase日期函數(shù)比較代碼示例
一、前言
在日常開發(fā)中,我們經(jīng)常會(huì)使用到日期函數(shù),對日期或時(shí)間戳進(jìn)行計(jì)算,以下簡單介紹梧桐數(shù)據(jù)庫與Gbase數(shù)據(jù)庫日期函數(shù)的使用。
二、日期加減
1、GBase數(shù)據(jù)庫
select cast('2024-10-30'as date) ± interval '1' day; --日期加減一天
select cast('2024-10-30'as date) ± interval '1' year; --日期加減一年
select cast('2024-10-30'as date) ± interval '1' month; --日期加減一月
select addDate(cast('2024-10-30'as date), interval '1' day/month/year); --日期加一天/月/年
select subDate(cast('2024-10-30'as date), interval '1' day/month/year); --日期減一天/月/年
select timestampdiff(month, cast('2024-01-01' as date), cast('2024-10-30'as date)); --計(jì)算兩個(gè)日期之間月份差
select add_months(cast('2024-01-01' as date), 10); --指定日期上加月數(shù)2、梧桐數(shù)據(jù)庫
select date'2024-10-30' ± 1 ; --日期加減一天 select date'2024-10-30' ± interval '1 day'; --日期加減一天 select date'2024-10-30' ± interval '1 year'; --日期加減一年 select date'2024-10-30' ± interval '1 month'; --日期加減一月 select age(date'2023-10-30' , date'2024-10-30') --計(jì)算兩個(gè)日期的差值
三、時(shí)間戳加減
1、GBase數(shù)據(jù)庫
select current_time; --獲取當(dāng)前時(shí)間 select current_time ± interval '1' day; --日期加減一天 select current_time ± interval '1' year; --日期加減一年 select current_time ± interval '1' month; --日期加減一月 select current_time ± interval '1' hour; --當(dāng)前時(shí)間加減一小時(shí) select current_time ± interval '1' minute; --當(dāng)前時(shí)間加減一分鐘 select current_time ± interval '1' second; --當(dāng)前時(shí)間加減一秒
2、梧桐數(shù)據(jù)庫
elect now(); --獲取當(dāng)前時(shí)間 select now() ± interval '1 day'; --時(shí)間加減一天 select now() ± interval '1 year'; --時(shí)間加減一年 select now() ± interval '1 month'; --時(shí)間加減一月 select now() ± interval '1 hour'; --時(shí)間加減一小時(shí) select now() ± interval '1 minute'; --時(shí)間加減一分鐘 select now() ± interval '1 second'; --時(shí)間加減一秒 select extract(epoch from (end_time - start_time)) ; --計(jì)算時(shí)間戳相差秒數(shù)
四、其他日期函數(shù)
- 時(shí)間戳轉(zhuǎn)換
--GBase數(shù)據(jù)庫:
select from_unixtime('2024-10-30 15:29:24')
--梧桐數(shù)據(jù)庫:
select to_timestamp('2024-10-30 15:29:24')
- 時(shí)間截取
--梧桐數(shù)據(jù)庫
select extract(year from now()); -- 獲取年
select extract(month from now()); -- 獲取月
select extract(day from now()); -- 獲取日
--Gbase數(shù)據(jù)庫
select date_part('year', current_time) ; --獲取年
select date_part('month', current_time) ; -- 獲取月
select date_part('day', current_time) ; -- 獲取日
四、總結(jié)
日期函數(shù)對于我們?nèi)粘i_發(fā)很重要,時(shí)常會(huì)使用到。梧桐數(shù)據(jù)庫與GBase數(shù)據(jù)庫都有豐富的日期函數(shù),為我們的開發(fā)提供了強(qiáng)大的支持。兩個(gè)庫的日期函數(shù)名上有所不同,但其實(shí)現(xiàn)功能上基本相同。
到此這篇關(guān)于梧桐數(shù)據(jù)庫與GBase日期函數(shù)比較的文章就介紹到這了,更多相關(guān)梧桐數(shù)據(jù)庫與GBase日期函數(shù)比較內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
關(guān)于關(guān)系數(shù)據(jù)庫如何快速查詢表的記錄數(shù)詳解
這篇文章主要給大家介紹了關(guān)于關(guān)系數(shù)據(jù)庫如何快速查詢表的記錄數(shù)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用關(guān)系數(shù)據(jù)庫具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04
通過DBeaver連接GaussDB數(shù)據(jù)庫的實(shí)戰(zhàn)案例
DBeaver是一個(gè)通用的數(shù)據(jù)庫客戶端,可以通過配置不同驅(qū)動(dòng)連接各種不同的數(shù)據(jù)庫,這篇文章主要介紹了通過DBeaver連接GaussDB數(shù)據(jù)庫的相關(guān)資料,包括前置條件、連接步驟、常見問題及解決方案、高級操作,需要的朋友可以參考下2025-11-11
關(guān)系型數(shù)據(jù)庫和非關(guān)系型數(shù)據(jù)庫概述與優(yōu)缺點(diǎn)對比
這篇文章介紹了關(guān)系型數(shù)據(jù)庫和非關(guān)系型數(shù)據(jù)庫概述與優(yōu)缺點(diǎn)對比,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-03-03
8種主流NoSQL數(shù)據(jù)庫系統(tǒng)特性對比和最佳應(yīng)用場景
這篇文章主要介紹了8種主流NoSQL數(shù)據(jù)庫系統(tǒng)特性對比和最佳應(yīng)用場景,對選擇一個(gè)NoSQL數(shù)據(jù)庫來說是一個(gè)不錯(cuò)的參考文章,需要的朋友可以參考下2014-06-06
dapper使用Insert或update時(shí)部分字段不映射到數(shù)據(jù)庫
我們在使用dapper的insert或update方法時(shí)可能會(huì)遇見一些實(shí)體中存在的字段但是,數(shù)據(jù)庫中不存在的字段,這樣在使用insert時(shí)就是拋出異常提示字段不存在,這個(gè)時(shí)候該怎么解決呢,下面給大家分享示例實(shí)體代碼,感興趣的朋友一起看看吧2023-12-12
DBeaver連接GBase數(shù)據(jù)庫的簡單步驟記錄
DBeaver數(shù)據(jù)庫連接工具,是我用了這么久最好用的一個(gè)數(shù)據(jù)庫連接工具,擁有的優(yōu)點(diǎn),支持的數(shù)據(jù)庫多、快捷鍵很贊、導(dǎo)入導(dǎo)出數(shù)據(jù)非常方便,下面這篇文章主要給大家介紹了關(guān)于DBeaver連接GBase數(shù)據(jù)庫的簡單步驟,需要的朋友可以參考下2024-03-03

