将访问mid转换为sql substring

时间:2016-10-13 20:42:11

标签: sql ms-access

我有代码我想从访问语言转换为SQL。我已经将instr转换为charindex,只需转换中间片。该代码基本上采用一个包含始终格式为john.doe@gmail.com的电子邮件地址的字段,并使两个列一个具有名字,另一个具有姓氏。这是代码:

SELECT sap.Description, Sum(main.Hours) AS SumOfHours, 
    substring(supervisor1email, 1, charindex('.', supervisor1email) - 1) AS SupervisorFirstName, 
substring(supervisor1email, charindex('.', supervisor1email) + 1, charindex('@', supervisor1email) - charindex('.', supervisor1email) - 1) AS SupervisorLastName, 

(SELECT SUM(sub.hours) 

FROM v_MES_TcActivities sub

WHERE sub.costctr='106330' 
AND sub.AttCode Not Like 'MEAL'
AND sub.clockin Between dateadd(day, 1-datepart(dw, getdate()), CONVERT(date,getdate())) and dateadd(day, 8-datepart(dw, getdate()), CONVERT(date,getdate()))

AND substring(sub.supervisor1email, 1, charindex('.', sub.supervisor1email) - 1) = substring(main.supervisor1email, 1, charindex('.', main.supervisor1email) - 1) 

AND substring(sub.supervisor1email, charindex('.', sub.supervisor1email) + 1, charindex('@', sub.supervisor1email) - charindex('.', sub.supervisor1email) - 1) = substring(main.supervisor1email, charindex('.', main.supervisor1email) + 1, charindex('@', main.supervisor1email) - charindex('.', main.supervisor1email) - 1)

) AS TotalHours

FROM v_MES_TcActivities AS main
LEFT JOIN t_SAP_AttCodes AS sap 
ON main.AttCode = sap.Code

WHERE main.AttCode Not Like 'MEAL' 
AND main.CostCtr Like '106330' 
AND main.ClockIn Between dateadd(day, 1-datepart(dw, getdate()), CONVERT(date,getdate())) and dateadd(day, 8-datepart(dw, getdate()), CONVERT(date,getdate()))

GROUP BY sap.Description, substring(main.supervisor1email, 1, charindex('.', main.supervisor1email) - 1), substring(main.supervisor1email, charindex('.', main.supervisor1email) + 1, charindex('@', main.supervisor1email) - charindex('.', main.supervisor1email) - 1)

ORDER BY substring(main.supervisor1email, charindex('.', main.supervisor1email) + 1, charindex('@', main.supervisor1email) - charindex('.', main.supervisor1email) - 1)

编辑:这是整个代码

1 个答案:

答案 0 :(得分:1)

只要为substring指定3个参数(就像您已经为MID所做的那样),您应该能够简单地切换函数名称并运行查询。哦,如果这是SQL Server,那么LEFT也必须转换为substring

SELECT 
    sap.Description, Sum(main.Hours) AS SumOfHours,     
    substring(supervisor1email, 1, charindex('.', supervisor1email) - 1) AS SupervisorFirstName,
    substring(supervisor1email, charindex('.', supervisor1email) + 1, charindex('@', supervisor1email) - charindex('.', supervisor1email) - 1) AS SupervisorLastName

编辑:这是一个有效的模型,包括虚拟数据

create table v_MES_TcActivities
(
supervisor1email varchar(100),
AttCode varchar(100),
Hours decimal(18,8),
costctr varchar(100),
clockin datetime default getdate()
);

create table t_SAP_AttCodes
(
Code varchar(100),
Description varchar(100)
);


insert into t_SAP_AttCodes (Code, Description) values ('NON-MEAL', 'not a meal');

insert into v_MES_TcActivities (supervisor1email, AttCode, costctr, Hours, clockin) values ('john.doe@gmail.com', 'NON-MEAL', '106330', 2, dateadd(hh, -2, getdate()));

以下是来自上面的确切查询(格式化):

SELECT 
    sap.Description, Sum(main.Hours) AS SumOfHours, 
    substring(supervisor1email, 1, charindex('.', supervisor1email) - 1) AS SupervisorFirstName, 
    substring(supervisor1email, charindex('.', supervisor1email) + 1, charindex('@', supervisor1email) - charindex('.', supervisor1email) - 1) AS SupervisorLastName, 
    (
        SELECT SUM(sub.hours) 
        FROM v_MES_TcActivities sub 
        WHERE sub.costctr='106330' AND sub.AttCode Not Like 'MEAL' 
        AND sub.clockin Between dateadd(day, 1-datepart(dw, getdate()), CONVERT(date,getdate())) and dateadd(day, 8-datepart(dw, getdate()), CONVERT(date,getdate()))
        AND substring(sub.supervisor1email, 1, charindex('.', sub.supervisor1email) - 1) = substring(main.supervisor1email, 1, charindex('.', main.supervisor1email) - 1) 
        AND substring(sub.supervisor1email, charindex('.', sub.supervisor1email) + 1, charindex('@', sub.supervisor1email) - charindex('.', sub.supervisor1email) - 1) = substring(main.supervisor1email, charindex('.', main.supervisor1email) + 1, charindex('@', main.supervisor1email) - charindex('.', main.supervisor1email) - 1)
     ) AS TotalHours
FROM v_MES_TcActivities AS main
LEFT JOIN t_SAP_AttCodes AS sap 
ON main.AttCode = sap.Code
WHERE main.AttCode Not Like 'MEAL' 
AND main.CostCtr Like '106330' 
AND main.ClockIn Between dateadd(day, 1-datepart(dw, getdate()), CONVERT(date,getdate())) and dateadd(day, 8-datepart(dw, getdate()), CONVERT(date,getdate()))
GROUP BY sap.Description, substring(main.supervisor1email, 1, charindex('.', main.supervisor1email) - 1), substring(main.supervisor1email, charindex('.', main.supervisor1email) + 1, charindex('@', main.supervisor1email) - charindex('.', main.supervisor1email) - 1)
ORDER BY substring(main.supervisor1email, charindex('.', main.supervisor1email) + 1, charindex('@', main.supervisor1email) - charindex('.', main.supervisor1email) - 1)

结果显示已解析的名字/姓氏:

enter image description here

查询显然正确地解析了电子邮件,因此我认为您关于如何使用substring的原始问题已得到很好的解答。如果您仍然无法使查询正常工作,我建议您仔细查看表中的数据,以确定某些值是否不遵循" first.last@domain.name"模式,或者如果你需要sql的其他帮助,你应该把它作为一个单独的问题。祝你好运!