查询语句:

select <列名> 

from <表名>

[where <查询条件表达式>]

[ order by <排序的列名>[asc或desc] ]

  asc升序(缺省)  

  desc降序

使用as来命名列(取别名)

select Scode as 学员编号, Sname as 学员姓名, Saddress as 学员地址

from students

where Saddress<>‘河南新乡’

 

select FirstName + '.' + LastName AS '姓名' from Employees

 

 

查询空行

select SName from students where Semail IS NULL

 

查询非空行

select sname from students where not semail is null

select sname from students where semail is  not   null

 

限制固定行数

select top 5 sname, saddress 

from students where ssex = 0

 

返回百分之多少行

select top 20 percent sname, saddress 

from students where ssex = 0 

 


 

sql server 中函数

  • 字符串函数
    • charindex 用来寻找一个指定的字符串在另一个字符串中的起始位置
      •   select charindex('accp', 'my accp course', 1)
      •      返回:4     
      •         返回:0  表示未找到。  
    • len    返回传递给它的字符串长度
      •   select len('sql server课程')
      •        返回:12 
    • lower    转成小写
      •   select lower('SQL SERVER课程'
      •        返回:sql server课
    • UPPER   转成大写 
      •   select upper('sql server课程')
      •        返回:SQL SERVER课程   
    • ltrim   清除字符左边的空格
    • rtrim   清除字符右边的空格   
      • 清除左边右边的空格
      • select rtim(ltrim('       abc      '))   
    • right  从字符串右边返回指定数目的字符 
      • select right('买买提,土尔松',3)
      • 返回:土尔松
    • left  从字符串左边返回指定数目的字符  
      • select left('买买提,土尔松',3)
      • 返回:买买提
    • replace 替换一个字符串中的字符
      •   select replace('莫勒克且,杨克','克','兰')  
      •        返回:莫勒兰且,杨兰  
    • stuff    在一个字符串中删除指定长度的字符,并在该位置插入一个新的字符串
      •   select stuff('abcdefg',2,3,'我的音乐我的世界')
      •        返回:a我的音乐我是世界efg                              
  • 日期函数
    • getdate  取得当前的系统日期    
      •   select getdate()
      •        返回:今天的日期
    • dateadd  将指定的数值添加到指定的日期部分后的日期
      •   select dateadd(mm,4,'01/01/99')
      •        返回:以当前的日期格式返回05/01/99 
    • datediff   两个日期之间的指定日期部分的区别
      •   select datediff(dd,'01/01/1999','01/01/2000')
      •        返回:365 
    • datename   日期中指定日期部分的字符串形式
      •   select datename(dw,'01/2/2020')
      •        返回:星期四  
    • datepart  日期中指定日期部分的整数形式
      •   select datepart(day,'01/15/2020')
      •        返回:15  
  • 数学函数
    • abs    取数值表达式的绝对值
      •   select abs(-9)
      •        返回:9
    • ceiling  返回大于或等于所给数字表达式的最小整数
      •   select ceiling(3.14926)
      •        return: 4
    • floor  返回小于或等于指定表达式的最大整数
      •   select floor(9.9999)
      •        return: 9
    • power    取数值表达式的幂值
      •   print power(2,10)
      •        return: 1024
    • round    将数值表达式四舍五入为指定精度
      •   select round(3.1415926,3)
      •        return: 3.142   
    • sign   对于正数返回+1,对于负数返回-1,对于0返回0
      •   select sign(-1.000001)
      •        return: -1 
    • sqrt   取浮点表达式的平方根
      •   select sqrt(9)
      •        return: 3     
    • rand   返回 0 到 1 之间的随机float值
      •   select rand()     
      •   返回:0.xxxxx   
  • 系统函数
    • convert  用来转变数据类型
      •   select convert(varchar(5),12345)
      •       返回:字符串‘12345’
    • current_user  返回当前用户的名字
      •   select current_user 
    • datalength   返回用于指定表达式的字节数
      •   select datalength('中国A盟')   
      •        返回:7
    • host_name   返回当前用户登陆的电脑名
      •   select host_name()
    •   system_user    返回当前所登陆的用户名
      •   select system_user
    •   user_name     从给定的用户id返回用户名
      •   select user_name(1
      •       返回:从任意数据库中返回 ‘dbo’

案例分析1:

某公司印了一批充值卡,卡的密码是随机生产的,现在出现这个问题:

卡里面的 “o" 和 0 "i" 和 1,用户反映看不清,公司决定,把存储在数据库中的密码中所有的”哦“都改成”零“,所有的”哎“都改成1;

请编写sql语句实现以上要求

答案:

   两条sql语句:

           update card set passwd = replace(password,'o','0')

   update card set passwd = replace(password,'i','1')

           一条sql语句

   update card set passwd = replace(replace(password,'o','0'),'i','1')

 

案列分析2:

在数据库表中有以下字符数据,如:

13-1、13-2、13-3、13-10、13-100、13-108、13-18、13-11、13-15、14-1、14-2

通过sql语句进行排序

 

答案:

方法1:

select listNumber from SellRecord 

   order by  convert(int, left('门牌号',charindex('-','门牌号')-1)) ,
   convert(int,  right('门牌号,len('门牌号')-charindex('-','门牌号'))) 
 

方法2:

select listNumber from SellRecord

order by  convert(int,left ('门牌号',charindex('-','门牌号')-1))  ,
    convert(int,stuff('门牌号',1,charindex('-','门牌号'),'' ))  

 

方法3:


select listNumber from SellRecord

order by convert(int , substring('门牌号',1,charindex('-','门牌号')-1)),
       convert(int, substring('门牌号',charindex('-','门牌号')+1,len('门牌号')))
       

posted on 2020-01-01 17:41  风天雨乐  阅读(323)  评论(0)    收藏  举报