Hive SQL 基礎語法(1)

2021-08-14 02:21:39 字數 2533 閱讀 5653

字串擷取函式:substr,substring

語法: substr(string a, int start, int len),substring(string a, int start, int len)

返回值: string

說明:返回字串 a 從 start 位置開始,長度為 len 的字串

舉例:

hive> select substr(『abcde』,3,2) from dual;  

cd

hive> select substring(『abcde』,3,2) from dual;

cd

hive>select substring(『abcde』,-2,2) from dual;

de

日期時間轉日期函式: to_date

語法: to_date(string timestamp)

返回值: string

說明: 返回日期時間欄位中的日期部分。

舉例:

hive>   select to_date('2011-12-08 10:03:01') from dual;

2011-12-08

帶分隔符字串連線函式:concat_ws

語法: concat_ws(string sep, string a, string b…)

返回值: string

說明:返回輸入字串連線後的結果,sep 表示各個字串間的分隔符

舉例:

hive> select concat_ws(『,』,'abc』,'def』,'gh』) from dual;  

abc,def,gh

日期比較函式: datediff

語法: datediff(string enddate, string startdate)

返回值: int

說明: 返回結束日期減去開始日期的天數。

舉例:

hive>   select datediff(』2012-12-08′,』2012-05-09′) from dual;

213

正取餘函式 : pmod

語法: pmod(int a, int b),pmod(double a, double b)

返回值: int double

說明: 返回正的 a 除以 b 的餘數

hive> select pmod(9,4) from lxw_dual;

1hive> select pmod(-9,4) from lxw_dual;

3

兩個函式配合可以計算當前日期對應為星期幾

pmod(datediff(#date#, '1920-01-01') - 3, 7)
日期轉周函式: weekofyear

語法: weekofyear (string date)

返回值: int

說明: 返回日期在當前的週數。

舉例:

hive> select weekofyear('2011-12-08 10:03:01') from lxw_dual;

49

concat() 函式可以連線乙個或者多個字串

concat(str1,str2,…) 返回結果為連線引數產生的字串。如有任何乙個引數為null ,則返回值為 null。

select concat('11','22','33'); 

112233

the if condition evaluates the 「test condition」 and if the 「test condition」 is true, then it returns the 「true value」. otherwise, it returns the false value.

example:

if(1=1, 'working', 'not working') returns 'working'
a in (val1, val2, …)

boolean

true if a is equal to any of the values.

in 操作符允許我們在 where 子句中規定多個值。

select column_name(s)

from table_name

where column_name in (value1,value2,...)

非空查詢函式: coalesce

語法: coalesce(t v1, t v2,…)

返回值: t

說明: 返回引數中的第乙個非空值;如果所有值都為null,那麼返回null

舉例:

hive>

select coalesce(null,'100','50′) from lxw_dual;

100

1 基礎語法

1.python原始檔編碼 預設 utf 8 修改 在.py檔案第一行新增 coding cp 1252 2.識別符號 大小寫敏感 首位 字母,下劃線 只能是 其他位組成 字母,下劃線,數字 3.保留字 檢視保留字 import keyword print keyword.kwlist 4.注釋 單...

CSS語法基礎CSS語法基礎1 基本語法

css語法基礎css語法基礎1.基本語法 css的定義是由三個部分構成 選擇符 selector 屬性 properties 和屬性的取值 value 基本格式如下 selector 選擇符 選擇符是可以是多種形式,一般是你要定義樣式的html標記,例如body p table 你可以通過此方法定義...

Python基礎 1(語法基礎)

1 資料交換 c語言中 temp x x y y temp python中 x,y y,x 2 典型的python檔案結構 1 usr bin env python 1 起始行 23 this is a test module 2 模組文件45 import sys 3 模組匯入 6importos...