Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 如何将字段结果从两个单词更改为两个字母_Sql_Sql Server - Fatal编程技术网

Sql 如何将字段结果从两个单词更改为两个字母

Sql 如何将字段结果从两个单词更改为两个字母,sql,sql-server,Sql,Sql Server,我正在运行下面的脚本,areaoflaw字段的结果是“家庭法”。但是,读取输出的应用程序正在寻找两个字符的结果。《家庭法》应该是FL。我怎样才能修改剧本来做到这一点 declare @s varchar(100)='(FILE'; select clientid ,ClientSort as ClientName ,'Y' as Enabled ,'N' as HIPPA ,matternum ,case when

我正在运行下面的脚本,areaoflaw字段的结果是“家庭法”。但是,读取输出的应用程序正在寻找两个字符的结果。《家庭法》应该是FL。我怎样才能修改剧本来做到这一点

declare @s varchar(100)='(FILE';
select clientid
      ,ClientSort as ClientName
      ,'Y' as Enabled
      ,'N' as HIPPA
      ,matternum
      ,case
          when charindex(@s,[description])>0
          then left(cast([description] as varchar(max)),charindex(@s,[description])-1)
          else [description]
       end as MatterName
      ,'Y' as Enable
      ,'N' as HIPPA
      ,areaoflaw
  from matters
 order by Clientid
         ,matterid
; 

添加一个
案例
语句。比如说,

SELECT ....
CASE
WHEN areaoflaw = 'Family Law' then 'FL'
WHEN areaoflaw = 'Admiralty Law' then 'AL'
WHEN areaoflaw = 'Criminal Law' then 'CL'
WHEN areaoflaw = 'Patent Law' then 'PL'
END as areaoflaw....

添加一个
案例
语句。比如说,

SELECT ....
CASE
WHEN areaoflaw = 'Family Law' then 'FL'
WHEN areaoflaw = 'Admiralty Law' then 'AL'
WHEN areaoflaw = 'Criminal Law' then 'CL'
WHEN areaoflaw = 'Patent Law' then 'PL'
END as areaoflaw....

假设所有值都是两个单词,并且您总是需要这些单词的首字母,您可以使用
子字符串
charindex
的简单组合:

select 
    ...,
    (
        substring(areaoflaw,1,1) + 
        substring(areaoflaw,charindex(' ', areaoflaw)+1, 1) 
    ) as areaoflaw

假设所有值都是两个单词,并且您总是需要这些单词的首字母,您可以使用
子字符串
charindex
的简单组合:

select 
    ...,
    (
        substring(areaoflaw,1,1) + 
        substring(areaoflaw,charindex(' ', areaoflaw)+1, 1) 
    ) as areaoflaw

其他可能的值是什么?总是两个单词之间用空格隔开吗?你总是想要这两个单词的第一个字母吗?其他可能的值是什么?总是两个单词之间用空格隔开吗?你总是想要这两个单词的第一个字母吗?非常感谢。如何为以下单字设置此项?工人公司、WC、Y刑事、CR、Y家庭法、FL、Y公司业务、CB、Y人身伤害、PI、Y诉讼、LI、Y房地产事务、EM、Y杂项、MI、Y上诉、AP、Y房地产、RE?看起来两个字母代码的推导方式不同,因此,您将使用@Robert的答案中的
case
语句。当然,如果您将这些值存储在SQL表中,那么您只需要一个
join
。非常感谢。如何为以下单字设置此项?工人公司、WC、Y刑事、CR、Y家庭法、FL、Y公司业务、CB、Y人身伤害、PI、Y诉讼、LI、Y房地产事务、EM、Y杂项、MI、Y上诉、AP、Y房地产、RE?看起来两个字母代码的推导方式不同,因此,您将使用@Robert的答案中的
case
语句。当然,如果您将这些值存储在SQL表中,那么您只需要一个
join
。如何添加连接语法来修复此问题?谢谢,我添加了上述内容,但出现布尔错误。如何添加联接语法来修复此问题?