字段(field) 基本上与列(column)的意思相同,经常互换使用,不过数据库列一般称为列,而术语字段通常与计算字段一起使用。
    拼接字段
    输入:
    SELECT
    concat(province_name,’(‘,city_name’)’)
    from dw.dim_city

    输出
    河北省(张家口)

    keys:
    1、concat(a,b)→ab,a或b可以是表里的字段,也可以是自己设的字符(concat(a,’哈哈哈’)→a哈哈哈)或||
    使用别名

    从前面的输出可以看到,SELECT语句可以很好地拼接地址字段。但是,这个新计算列的名字是什么呢?实际上它没有名字,它只是一个值。一个未命名的列不能用于客户端应用中,因为客户端没有办法引用它。 为了解决这个问题,SQL支持列别名。别名(alias)是一个字段或值的替换名。别名用AS关键字赋予。别名也称为导出列。
    输入:
    SELECT
    concat(province_name,city_name) as province_city
    from dw.dim_city

    keys:
    1、as不强制使用,也能跑,但是最好命名,方便结果表或者其他引用的时候方便
    2、如果要引用,必须命名(理解:没有列名的表无法拉透视表)
    3、命名也可以规范不合法的字符(如空格),以及原来名字容易混淆或者错误的时候纠正它
    算数计算

    算数操作符 含义
    +
    -
    *
    ÷

    输入:
    SELECT
    2*3

    keys:
    1、+-/与数学含义一致,计算优先级也一致
    2、用圆括号可以优先顺序(和数学公式也一样)
    *文本处理函数

    函数名 返回值 用法
    chr(n) string 返回第 n 个字符
    concat(string1, string2) string 连接两个字符串
    length(string) bigint 返回字符串的长度
    lower(string)
    string 将字符串转换为小写
    upper(string) 将字符串转换为大写
    ltrim(string)
    rtrim(string)
    trim(string)
    string 删除字符串左/右边的空白符
    replace(string, search) string 删除所有 string 中出现的 search
    replace(string, search, replace) string 替换
    split_part(string, delimiter, index) string 用 delimiter 分割字符串,并返回第 index 部分
    strpos(string, substring) bigint 返回 substring 第一次出现的 index. 计数从 1 开始,如果没有出现则返回0
    substr(string, start)
    substr(string, start, length)
    string
    reverse(string) string 反转
    LTRIM() 去掉字符串左边的空格
    left() 返回字符串左边的字符
    RIGHT() 返回字符串右边的字符
    RTRIM() 去掉字符串右边的空格
    LTRIM() 去掉字符串左边的空格
    TRIM() 去掉字符串左右两边的空格
    SOUNDEX() 返回字符串的SOUNDEX值



    日期和时间处理函数

    函数名 示例 结果
    current_date select current_date; (date 类型,非 string)
    current_time select current_time; (time 类型)
    current_timestamp select current_timestamp; (timestamp 类型)
    current_timezone() select current_timezone();
    to_unixtime(timestamp) select to_unixtime(current_timestamp);
    select to_unixtime(cast(‘2015-03-02 12:00:00’ as timestamp));
    (注意这里需要 cast 做类型转换,Presto 在类型方面确实没 hive 顺手…)
    now() select now();
    from_unixtime(unixtime) select from_unixtime(to_unixtime(current_timestamp));
    unix_timestamp(string) select unix_timestamp(‘2015-02-27 12:00:00’);
    select unix_timestamp(‘2015-02-27’);
    1425009600
    1424966400
    date2datekey(string) select date2datekey(‘2012-02-01’);
    select date2datekey(‘2012-02-01 12:12:12’);
    ‘20120201’
    ‘20120201’
    datekey2date(string) select datekey2date(‘20120201’); ‘2012-02-01’
    weekofyear(string) select weekofyear(‘2015-02-26 03:09:51’);
    select weekofyear(‘2015-02-26’);
    9
    9
    date_sub(string) select date_sub(‘2014-01-30 12:00:00’, 1);
    select date_sub(‘2014-01-30’, 1);
    ‘2014-01-29’
    ‘2014-01-29’
    to_date(string) select to_date(‘2014-01-30 12:00:00’); ‘2014-01-30’
    datediff(string DATE1, string DATE2) select datediff(‘2009-03-01’, ‘2009-02-27’); 2
    datepart() 返回日期的某一部分

    数值处理函数

    函 数 说 明
    SIN() 返回一个角度的正弦
    COS() 返回一个角度的余弦
    TAN() 返回一个角度的正切
    ABS() 返回一个数的绝对值
    EXP() 返回一个数的指数值
    PI() 返回圆周率
    SQRT() 返回一个数的平方根

    函数的差异

    函数 语法
    提取字符串的组成部分 Access使用MID();DB2、Oracle、PostgreSQL和SQLite使用SUBSTR();MySQL和SQL

    Server使用SUBSTRING()
    | | 数据类型转换
    | Access和Oracle使用多个函数,每种类型的转换有一个函数;DB2和PostgreSQL使用CAST();MariaDB、MySQL和SQL Server使用CONVERT()
    | | 取当前日期
    | Access使用NOW();DB2和PostgreS使用CURRENT_DATE;MariaD和MySQL使用CURDATE();Oracle使用SYSDATE;SQL Server使用GETDATE();SQLite使用DATE()
    |

    upper()函数:将文本装换为大写
    select vender_name,upper(verder_name) as vender_name_upcase
    from Vendors
    order by vend_name;

    SOUNDEX()函数:
    SOUNDEX是一个将任何文本串转换为描述其语音表示的字母数字模式的算法。SOUNDEX考虑了类似的发音字符和音节,使得能对字符串进行发音比较而不是字母比较。虽然SOUNDEX不是SQL概念,但多数DBMS都提供对SOUNDEX的支持。
    下面给出一个使用SOUNDEX()函数的例子。Customers表中有一个顾客Kids Place,其联系名为Michelle Green。但如果这是错误的输入,此联系名实际上应该是Michael Green,该怎么办呢?显然,按正确的联系名搜索不会返回数据,如下所示:
    输入▼
    SELECT cust_name, cust_contact
    FROM Customers
    WHERE SOUNDEX(cust_contact) = SOUNDEX(‘Michael Green’);

    输出▼
    custname cust_contact
    ————————————— ——————————————
    Kids Place Michelle Green
    分析▼
    在这个例子中,WHERE子句使用SOUNDEX()函数把cust_contact列值和搜索字符串转换为它们的SOUNDEX值。因为Michael Green和Michelle Green发音相似,所以它们的SOUNDEX值匹配,因此WHERE子句正确地过滤出了所需的数据。
    RTRIM()函数
    输入▼
    _SELECT RTRIM(vend_name) || ‘ (‘ || RTRIM(vend_country) || ‘)’

    FROM Vendors
    ORDER BY vend_name;
    输出▼
    —————————————————————————————-
    Bear Emporium (USA)
    Bears R Us (USA)
    Doll House Inc. (USA)
    Fun and Games (England)
    Furball Inc. (USA)
    Jouets et ours (France)
    没有RTRIM()函数时:
    输入▼
    SELECT vend_name || ‘ (‘ || vend_country || ‘)’
    FROM Vendors
    ORDER BY vend_name;
    输出▼
    —————————————————————————————-
    Bear Emporium (USA )
    Bears R Us (USA )
    Doll House Inc.
    (USA )

    Fun and Games (England )
    Furball Inc. (USA )
    Jouets et ours
    (France )

    _
    substring_index(str,delim,count)
    str:要处理的字符串
    delim:分隔符
    count:计数
    例子:str=www.baidu.com
    substring_index(str,’.’,1)
    结果是:www
    substring_index(str,’.’,2)
    结果是:www.baidu
    也就是说,如果count是正数,那么就是从左往右数,第N个分隔符的左边的全部内容
    相反,如果是负数,那么就是从右边开始数,第N个分隔符右边的所有内容,
    如:
    substring_index(str,’.’,-2)
    结果为:baidu.com
    有人会为,如果我要中间的的baidu怎么办?
    很简单的,两个方向:
    从右数第二个分隔符的右边全部,再从左数的第一个分隔符的左边:  substring_index(substring_index(str,’.’,-2),‘.’,1);