Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/24.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/lua/3.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 server MSSQL将名称解析为两个新列并添加到同一个表中_Sql Server - Fatal编程技术网

Sql server MSSQL将名称解析为两个新列并添加到同一个表中

Sql server MSSQL将名称解析为两个新列并添加到同一个表中,sql-server,Sql Server,我正在使用sql server,我正在尝试找出如何选择vp_timesheetpunch.personfullname[Assoc Name],这是一个视图,然后对其进行解析,因为结果显示为“姓氏、名、中间名”。我试图在vp_timesheetpunch.personfullname[Assoc Name]之后再添加三列,在一列中显示名字,然后显示姓氏,并在Assoc Name之后的另一列中添加中间名列(如果有中间名) select vp_timesheetpu

我正在使用sql server,我正在尝试找出如何选择
vp_timesheetpunch.personfullname[Assoc Name]
,这是一个视图,然后对其进行解析,因为结果显示为“姓氏、名、中间名”。我试图在
vp_timesheetpunch.personfullname[Assoc Name]
之后再添加三列,在一列中显示名字,然后显示姓氏,并在
Assoc Name
之后的另一列中添加中间名列(如果有中间名)

     select
            vp_timesheetpunch.personnum [Assoc ID],
            vp_timesheetpunch.personfullname [Assoc Name],
            vp_timesheetpunch.laborlevelname1 [Department],
            vp_timesheetpunch.laborlevelname4 [Salary Code],
            vp_timesheetpunch.eventdate [Shift Date],
            shiftassignmnt.shiftstartdate [Scheduled Start],
            vp_timesheetpunch.startdtm [Rounded Start],
            vp_timesheetpunch.inpunchdtm [Actual Start],
            vp_timesheetpunch.enddtm [Rounded End],
            vp_timesheetpunch.outpunchdtm [Actual End],
            vp_timesheetpunch.TIMEINSECONDS [Time in seconds],
            convert(decimal(7,2),vp_timesheetpunch.TIMEINSECONDS/3600.0) [Time in hours]
        from
            vp_timesheetpunch
        left outer join
            vp_punchexceptions
        on
            vp_timesheetpunch.timesheetitemid = vp_punchexceptions.timesheetitemid
        inner join
            timesheetitem
        on
            vp_timesheetpunch.timesheetitemid = timesheetitem.timesheetitemid
        inner join
            workedshift
        on
            timesheetitem.workedshiftid = workedshift.workedshiftid
        inner join
            shfasgnwshfmm
        on
            workedshift.workedshiftid = shfasgnwshfmm.workedshiftid
        inner join
            shiftassignmnt
        on
            shfasgnwshfmm.shiftassignid = shiftassignmnt.shiftassignid
        where
            --limit rows to the specified pay period
            vp_timesheetpunch.eventdate = '1/22/2019'
            --exclude rows that are missing data
            and vp_timesheetpunch.inpunchdtm is not null
            and vp_timesheetpunch.outpunchdtm is not null
            --limit rows to shifts with exceptions
        order by
            vp_timesheetpunch.personnum,
            vp_timesheetpunch.eventdate
编辑:我尝试使用case进行解析,但没有成功

在此方面的任何帮助都将不胜感激。

您需要在(后面加一个逗号)作为姓氏

 ) AS LAST_NAME,
更新:下面是您只包含一个对象的代码。你能确认它有效吗

Select CASE WHEN 0 = CHARINDEX(' ',vp_timesheetpunch.personfullame)
     THEN NULL  --no more spaces?  assume rest is the first name
     ELSE SUBSTRING(
                      vp_timesheetpunch.personfullame,1
                     ,CHARINDEX(' ',vp_timesheetpunch.personfullame)-1
                   )  END AS MIDDLE_NAME
        ,SUBSTRING(
                      vp_timesheetpunch.personfullame,1 + CHARINDEX(' ',vp_timesheetpunch.personfullame)
                      ,LEN(vp_timesheetpunch.personfullame)
                  ) AS LAST_NAME
From vp_timesheetpunch

解析名称可能有点难看。假设你真的有这两种模式,像这样的东西应该会起作用

declare @FullName varchar(50) = 'Brierton, David H'

select LastName = case when parsename(replace(replace(@FullName, ',', ''), ' ', '.'), 3) is null
        then parsename(replace(replace(@FullName, ',', ''), ' ', '.'), 2)
        else parsename(replace(replace(@FullName, ',', ''), ' ', '.'), 3)
    end
    , FirstName = case when parsename(replace(replace(@FullName, ',', ''), ' ', '.'), 3) is null
        then parsename(replace(replace(@FullName, ',', ''), ' ', '.'), 1)
        else parsename(replace(replace(@FullName, ',', ''), ' ', '.'), 2)
    end
    , case when parsename(replace(replace(@FullName, ',', ''), ' ', '.'), 3) is not null
        then parsename(replace(replace(@FullName, ',', ''), ' ', '.'), 1)
    end

但是每当有关于数据库中名字的问题时,我觉得我必须分享这个链接

在我看来,添加这个必需的逗号只会对其他问题有所帮助。缺少的逗号对于此查询甚至解析都至关重要。您是否建议您的问题中新编辑的信息(未尝试解析名称)工作正常?我这样问是因为在我看来你以前的解析代码是有效的,而你只是漏掉了逗号。。。然后您会说查找vp_timesheetpunch对象时出错?您能否简化确认较小部件是否正常工作的工作?查看我的更新以测试您的无效对象…选择的答案如何解决无效对象?我同意…不知道丢失的对象是怎么回事。当然,正如最初发布的那样,丢失的逗号会使这个问题变得支离破碎。我感谢你没有让我感到困惑。你对实际问题的回答显然是最准确的。。。。干杯