zoukankan      html  css  js  c++  java
  • Sql Server 常用函数

    Sql Server 常用函数

    1,统计函数 avg, count, max, min, sum  

    多数聚会不统计值为null的行。可以与distinct一起使用去掉重复的行。可以与group by 来分组  

    2, 数学函数  

    SQRT  
    ceiling(n) 返回大于或者等于n的最小整数  
    floor(n), 返回小于或者是等于n的最大整数  
    round(m,n), 四舍五入,n是保留小数的位数  
    abs(n)  
    sign(n), 当n>0, 返回1,n=0,返回0,n<0, 返回-1  
    PI(), 3.1415....  
    rand(),rand(n), 返回0-1之间的一个随机数  
    3,字符串函数  

    ascii(), 将字符转换为ASCII码, ASCII('abc') = 97  
    char(), ASCII 码 转换为 字符  
    low(),upper()  
    str(a,b,c)转换数字为字符串。 a,是要转换的字符串。b是转换以后的长度,c是小数位数。str(123.456,8,2) = 123.46  
    ltrim(), rtrim() 去空格  
    left(n), right(n), substring(str, start,length) 截取字符串  
    charindex(子串,母串),查找是否包含。 返回第一次出现的位置,没有返回0  
    patindex('%pattern%', expression) 功能同上,可是使用通配符  
    replicate('char', rep_time), 重复字符串  
    reverse(char),颠倒字符串  
    replace(str, strold, strnew) 替换字符串  
    space(n), 产生n个空行  
    stuff(), SELECT STUFF('abcdef', 2, 3, 'ijklmn') ='aijklmnef', 2是开始位置,3是要从原来串中删除的字符长度,ijlmn是要插入的字符串。  
    3,类型转换函数:  

    cast, cast( expression as data_type), Example:  
    SELECT SUBSTRING(title, 1, 30) AS Title, ytd_sales FROM titles WHERE CAST(ytd_sales AS char(20)) LIKE '3%'  
    convert(data_type, expression)  
    4,日期函数  

    day(), month(), year()  
    dateadd(datepart, number, date), datapart指定对那一部分加,number知道加多少,date指定在谁的基础上加。datepart的取值包括,year,quarter,month,dayofyear,day,week,hour,minute,second,比如明天dateadd(day,1, getdate())  
    datediff(datepart,date1,date2). datapart和上面一样。整个函数结果是date2 - date1  
    datename(datepart, date) 取那一部分,返回字符串。  
    datepart(datepart, date) 取一部分,返回整数。  
    getdate()当前时间  
    5,系统函数  

    col_length('tablename','colname')  
    col_name, SELECT COL_NAME(OBJECT_ID('Employees'), 1) = EmployeeID  
    datalength, example: datalenght('abc') =3, datalength(pub_name) --列名  
    db_id
  • 相关阅读:
    Leetcode Unique Binary Search Trees
    Leetcode Decode Ways
    Leetcode Range Sum Query 2D
    Leetcode Range Sum Query
    Leetcode Swap Nodes in Pairs
    Leetcode Rotate Image
    Leetcode Game of Life
    Leetcode Set Matrix Zeroes
    Leetcode Linked List Cycle II
    CF1321A
  • 原文地址:https://www.cnblogs.com/conquer/p/564400.html
Copyright © 2011-2022 走看看