-2

我使用的是 SQL Server 数据库,但我的案例陈述很难。我想要做的是在 id 字段(state_issue_teacher_id)等于''(空字段)时将属性串联插入。问题是我有一些空的 id 字段,我需要用通过 case 语句连接属性创建的唯一 id 来填充这些字段。

在此处输入图像描述

上面的空行我需要用 last_name+first_name+gender+race_ethnicity_code+high_degree_code+position_code+assignment_code 的串联来填充。

这是我到目前为止所拥有的:

SELECT    
        state_issue_teacher_id,
        region_code
        + county_code
        + district_code AS district_code ,
        last_name ,
        first_name ,
        assigned_fte = CASE assign_fte
                  WHEN '' THEN 0
                  ELSE CAST(assign_fte AS NUMERIC(18,
                  2))
                  END ,
        CASE WHEN state_issue_teacher_id = '' THEN 
        RTRIM([last_name]) + '_' + RTRIM([first_name]) + '_' + RTRIM([gender])
        + '_' + RTRIM([race_ethnicity_code]) + '_' + high_degree_code + '_'
        + position_code  + '_' + assignment_code
        ELSE state_issue_teacher_id
        END,
        year_time

FROM      dbo.example
4

4 回答 4

2

When concatenating like that you need to account for two possible problems.

First are all of the fields you are concatenating together varchar (or nvarchar) or are some of them int or numeric? If your datatypes are not varchar or nvarchar, then you need to convert them to use them in a concatentation.

The next possible problem is if one or more of the values can be null. You will need to coalesce any value that could be null with a varchar value (usually a space or empty string for a concatenations although sometimes the value "unknown" works too)) or the concatenation will return null as null concatenated with anything else is null.

于 2013-04-12T21:28:11.263 回答
1
SELECT    
    state_issue_teacher_id,
    region_code
    + county_code
    + district_code AS district_code ,
    last_name ,
    first_name ,
    assigned_fte = CASE assign_fte
              WHEN '' THEN 0
              ELSE CAST(assign_fte AS NUMERIC(18,
              2))
              END ,
    CASE WHEN state_issue_teacher_id = '' THEN 
    RTRIM(coalesce([last_name],'ValueYouUseInsteadOfNull')) + '_' + 
    RTRIM(coalesce([first_name],'ValueYouUseInsteadOfNull')) + '_' + 
    RTRIM(coalesce([gender],'ValueYouUseInsteadOfNull')) + '_' + 
    RTRIM(coalesce([race_ethnicity_code],'ValueYouUseInsteadOfNull')) + '_' + 
    coalesce(high_degree_code,'ValueYouUseInsteadOfNull') + '_' + 
    coalesce(position_code,'ValueYouUseInsteadOfNull')  + '_' + 
    coalesce(assignment_code,'ValueYouUseInsteadOfNull')
    ELSE state_issue_teacher_id
    END,
    year_time
FROM      dbo.example

奇怪的是,您在其中一些字段中有 null 。试试这个查询。

于 2013-04-12T21:31:56.367 回答
0

感谢所有回答问题的人。我让它与下面的查询一起工作。

SELECT    
    state_issue_teacher_id,
    region_code
    + county_code
    + district_code AS district_code ,
    last_name ,
    first_name ,
    gender,
    race_ethnicity_code,
     high_degree_code, 
    position_code,
    assignment_code,  
    assigned_fte = CASE assign_fte
              WHEN '' THEN 0
              ELSE CAST(assign_fte AS NUMERIC(18,
              2))
              END ,
    state_issue_teacher_id =
    CASE state_issue_teacher_id WHEN  '' THEN
    RTRIM([last_name]) + '_' + RTRIM([first_name]) + '_' + RTRIM([gender])
      + '_' + RTRIM([race_ethnicity_code]) + '_' + high_degree_code + '_'
      + position_code  + '_' + assignment_code
      ELSE state_issue_teacher_id
    END,
    year_time
FROM dbo.example
于 2013-04-12T21:53:26.943 回答
-1

怎么样?:

update example
set state_issue_teacher_id = 
RTRIM([last_name]) + '_' + RTRIM([first_name]) + '_' + RTRIM([gender])
        + '_' + RTRIM([race_ethnicity_code]) + '_' + high_degree_code + '_'
        + position_code  + '_' + assignment_code
WHERE isnull(state_issue_teacher_id, '') = ''
于 2013-04-12T21:33:55.603 回答