sql server 的T-SQL 学习笔记(四)

关于T-sql的学习笔记
主要是内置函数 时间函数 还有一些简单的查询语句

/******************* 2017-7-19 13:39:55 数据查询 *******************

--学习目标
  -- 熟练掌握 top n, top n percent ,order by查询
  -- 熟练使用查询语句
  -- 删除重复信息 distinct(效率相当低)
*/

-- ************* select 语句 ********************
-- select column1..... from table_name where <表达式> order by   
  -- 查看所有 select * ....(以一般不建议这样)
  -- 除去重复信息 distinct
  use studentSys
  -- 查询所有列(一般不建议这样,需要查询什么字段就写什么字段)
    select * from Student 
    select * from Course 
  -- 字段取别名
    select stuSex as '性别',stuName as '姓名' from Student
    select 性别=stuSex , 姓名=stuName from Student   
  -- 去重 distinct 
    select distinct stuName from Student 
    select distinct stuName stuSex from Student  

  -- 排序(降序desc 或者 升序asc) 默认为升序asc
    select stuId from Student order by stuId 
    select stuId from Student order by stuId desc
    -- 多列异排序
      select * from Student order by classId desc
      select * from Student order by classId desc,stuId desc
      -- 在SQL server中可以用数字代替列名
        select * from Student order by stuId desc
        select * from Student order by  1 desc
    -- 统计性别人数(男 多少人,女多少人)
      select stuSex as '性别',  COUNT(1) as '人数' from Student group by stuSex 

  -- top n 指定返回结果集的前n行
    select * from Student 
    select top 3 * from Student

  -- 返回 top num percent 前百分num的数据
    select * from Student 
    select top 50 percent * from Student 

  -- ************* 条件查询 *************
    select * from Student 
    -- 查询stuAddress 为 null 的数据
      select * from Student where stuAddress is null
    -- 查询stuAddress 不为 null 的数据
      select * from Student where stuAddress is not null  
    -- 单例比较查询
      select * from Student where classId > 1

    -- 多列组合比较查询
      select * from Student where stuName = '张三' or stuSex = '女'
        -- select * from Student where stuName = '张三' or stuSex = '男' and classId = 2
        -- 这样写是不正确的 运算符的优先级
        -- 订正 (增加括号)
        select * from Student where ( stuName = '张三' or stuSex = '男') and classId = 2

-- ************ SQL server 内置函数  ***************************
  -- 输出字符串长度(select 和 print 都为输出)
    select LEN('chencong') as '字符串chencong长度'
    select DATALENGTH('chencong') as '字符串chencong的长度'
    select DATALENGTH ('chen陈') as '字符串chen陈的长度,中文两个字节'
    select CHARINDEX ('c','chencong') as 'c在字符串chencong中位置'
    select UPPER ('chencong') as '将chencong转换成大写'
    select LTRIM (' chencong ') as '去掉 chencong左边空格'
    select rtrim (' chencong ') as '去掉chencong 右边空格'
    select REPLACE ('chencong','c','s') as '将chencong中的c替换成s'
    select SUBSTRING('chencong',3,4) as '从第3个位置开始截取指定长度4的字符串'

    select 'chencong' + CONVERT(varchar(10),123) as '将字符 chencong 和数字 123 拼接'
    select 'chencong' + CAST(123 as varchar(10)) as '将字符 chencong 和数字 123 拼接'

  -- 在查询语句中使用字符串函数(只有字符串函数才能够使用字符串)
    select * from Student where LEN(stuName) > 3
    select SUBSTRING(CAST(stuBornDay as varchar(20)),1,10) as '转换后的日期'  from Student 

  -- 日期函数
    select GETDATE() as '当前系统时间'
    -- 指定时间 1996-03-21 减去月份 -5
      select DATEADD(MM,-5,'1996-03-21') as '指定时间减去 指定的日期'
    select DATEDIFF(MM,'2017-05-19','2017-07-19') as '指定日期相隔天数'
    select DATEDIFF (DD,'1996-03-21',GETDATE()) as '指定日期距离今天的天数'
    select DATENAME (DD ,GETDATE()) as '获得今天是几号'
    select DATENAME (QQ,GETDATE()) as '当前时间是季度'
    select DATENAME(DW,GETDATE()) as '今天星期'
    select DAY(GETDATE()) as '今天的日期'
    select MONTH(GETDATE()) as '今天的月份'
    select YEAR(GETDATE()) as '今天的年份'
    select DATENAME (HOUR,GETDATE()) as '现在是当天的小时'
    select DATENAME(MINUTE,GETDATE()) as '现在是当天的分钟'
    select datename (second,GETDATE()) as '现在是当天的秒数'

    -- 随机数
      select RAND() as '随机数'
      select RAND(1) as '固定的种子随机数'
    -- 

你可能感兴趣的:(SQL-Server)