zoukankan      html  css  js  c++  java
  • SQL变量、Substring、charindex、case函数、去除重复

     

    isnull(aa,0)
    删除表数据:

    truncate table aaa
     
    添加字段:

    ALTER TABLE table1 ADD col1 varchar(200) DEFAULT '2008-05-22'

    修改字段名:

    alter table table1 rename column col1 to col2;

    修改字段属性:

    alter table table1 alter column col1 varchar(200) not null;

    修改默认值

    alter table table1 add constraint df default('嘿嘿') for col1;
     

    insert into table1(col1,col2)

      select col1,col2

      from table2 where table2.id=1

    update t
       set t.col1 = o.col1
      from table1 AS t
             inner join
           table1 AS o 
             on t.id = o.id

    //第一个参数是要截取的对象,第二个参数是截取的起始位置(从1开始),第三个参数是截取的长度

    select Substring('1234567890',-1,3)

    //第一个参数是要查找的字符,第二个参数是查找的对象,字符串的索引是从1开始的

    select charindex('.','132.12.3')

    //获取字符串的长度,参数为要查找的对象

    len('123456')

    //将一个字符串反转

    select reverse('hello,world') 

    将得到如下的输出:dlrow,olleh 

    //获取最后一次”-“出现的位置

    charindex('-',reverse(@str)) 

    //获取最后一个”-“后面的字符 

    reverse(substring(reverse(@str),1,charindex('-',reverse(@str))-1))

    //类型转换

    CAST(@XX AS char(20))

    CONVERT(char(20), @XX)

    cast(0 as bit)

    --简单case函数

    case sex

                  when   '1'   then  '男'

                  when   '2'   then  '女'

                  else     '其他'   end

    --case搜索函数

    case      when  sex = '1'   then  '男'

                  when  sex = '2'   then  '女'

                  else    '其他'   end

    //申明及使用变量

    declare @OldItemPath nvarchar(500);

    select @OldItemPath=‘123’;

    //申明及使用表变量

    declare @table1 table (Id int);

    insert into @table1

    select Id from table1;

    select * from @table1;

    //关键字distinct,去除重复项

    select distinct ip,city from table2//关键字distinct,去除重复项

  • 相关阅读:
    Dubbo 节点telnet测试
    node.js 文件下载
    node.js获取ip及mac
    excel中根据A列筛选B列填充C列
    django在读取数据库时未筛选到符合条件的记录会报错
    django分页功能
    django中命令行调试程序
    Python中if-else的多种写法
    python自定义函数的参数之四种表现形式
    python类变量和实例变量的区别
  • 原文地址:https://www.cnblogs.com/deep-blue/p/5110039.html
Copyright © 2011-2022 走看看