zoukankan      html  css  js  c++  java
  • sqlserver 函数

     sqlserver数据库中字符串分割函数:

    使用语句1查询出来的结果为张三,李四,王五。这个receivername字段里存放了多个人的姓名。

    1.select v.receivername from T_SimpleFlow_MainInfo  where v.id=52;

    使用语句2查询出来的结果是按 ‘,’ 分割之后的结果,显示为3列 分别为 张三   李四   王五 。

    2.select sp.value from T_SimpleFlow_MainInfo v
           cross  apply string_split(v.receivername,',') sp where v.id=52 ;

    使用sql查询为空时指定返回参数

    语法:ISNULL ( check_expression , replacement_value )

    check_expression** :将被检查是否为 NULL的表达式。check_expression 可以是任何类型的。

    replacement_value:在 check_expression 为 NULL时将返回的表达式。replacement_value 必须与check_expresssion 具有相同的类型。

    select ISNULL(null, '456');     查询结果为456

    select ISNULL('123', '456');   查询结果为123 

    SQL LEN() 语法

     SELECT LEN(column_name) FROM table_name

    select len(ISNULL(null, '456'));    查询结果为3

    select len(isnull('1234',''));          查询结果为4

    CONCAT 函数 和 CHARINDEX 的组合使用

    一  concat()函数

    1、功能:将多个字符串连接成一个字符串。

    2、 语法1:concat(str1, str2,...),返回结果为连接参数产生的字符串没有有分隔符

    3.、语法2:concat(str1, seperator,str2,seperator,...),返回结果为连接参数产生的字符串并且有分隔符

    select concat(列名称,',','123') from tableName;

    二 charindex()函数

    一、语法
    CHARINDEX ( char ,string, [  start_location ] )

    char  一个表达式,其中包含要查找的字符的序列
    string  一个表达式,通常是一个为指定序列搜索的列,string  属于字符串数据类别。
    start_location 开始在 string  中搜索 char  时的字符位置 

    select * from tablename  where id=1  and  CHARINDEX('aaa', column)>0;

    查询列中column 是否包含'aaa' 字符串,存在的话>0 可以查询出结果,如果不存在>0 则查不出结果(也可以使用=0)。

    三 concat和charindex配合使用

    update tableName set name=CONCAT(name, ',',#{name}) where id=#{id} and CHARINDEX(#{name},name)=0

    这段sql的作用是:如果name中字段中已存在传过来的用户名(#{name})则不会再进行拼接name字段,如果name中的字段不存在传过来的用户名(#{name})则拼接name字段。

    sql查询语句 如果字段为空,则给定默认值

    如果 数据库中查询出来的  nextActionName、backActionName、wasterActionName 字段为空,则显示 提交、驳回、作废。否则显示从数据库中查询的数据进行显示。

    select
    case when nextActionName is null then '提交' else nextActionName end as nextActionName,
    case when backActionName is null then '驳回' else backActionName end as backActionName,
    case when wasterActionName is null then '作废' else wasterActionName end as wasterActionName
    from Status where id=#{id} and StatusId = #{statusId}
  • 相关阅读:
    WebService 通过POST方式访问时候,因 URL 意外地以“/方法名”结束,请求格式无法识别 解决办法
    SQL Server 触发器
    JS数据类型转换
    .net注册到IIS
    SQL Server 常用sql操作语句
    浅解DLL
    有关注册表API函数
    [原]惜 时
    图解双机共享ADSL上网
    如何在C#中使用全局鼠标、键盘Hook
  • 原文地址:https://www.cnblogs.com/ming-blogs/p/10795525.html
Copyright © 2011-2022 走看看