在SQL Server中使用INNERJOIN从日期中删除时间戳

时间:2019-03-20 07:53:09

标签: sql-server database timestamp inner-join

我在这里发现了很多东西,但没有任何帮助。我可以删除时间戳,但是使用INNER JOIN时无法删除。我尝试了很多事情,但是没有用。

这是我想要做的:

SELECT 
    CAST(CURRENT_TIMESTAMP as DATE) "END_DATE", LO_ID 
FROM 
    dbo.LO as L 
INNER JOIN 
    dbo.IL as I ON L.LO_ID = I.LO_ID
INNER JOIN 
    dbo.PO as P ON P.LO_ID = I.LO_ID
INNER JOIN 
    dbo.CO as C ON P.CO_ID = C.ID 
WHERE 
    P.LO = 7

我在LO_ID处出错。使用INNER JOIN时如何删除时间戳?

我还尝试了CONVERT而不是CAST的方法。

PS:这是我想要得到的结果:

SELECT * FROM loan.LOANS as L 
INNER JOIN insurance.INSURANCES as I ON L.LOAN_ID = I.LOAN_ID 
INNER JOIN insurance.POLICIES as P ON P.LOAN_ID = I.LOAN_ID 
INNER JOIN insurance.COMPANIES as C ON P.COMPANY_ID = C.ID WHERE P.LOAN_ID = 7

这很好用,这是我所期望的,但是在所有Date中都有一个时间戳,我只想删除该时间戳

enter image description here

1 个答案:

答案 0 :(得分:0)

此查询有什么问题?

SELECT CONVERT(date, [L.END_DATE]) as LOAN_END_DATE, 
       CONVERT(date, [I.END_DATE]) as INSURANCE_END_DATE, 
       L.LOAN_ID 
FROM loan.LOANS as L 
     INNER JOIN insurance.INSURANCES as I ON L.LOAN_ID = I.LOAN_ID 
     INNER JOIN insurance.POLICIES as P ON P.LOAN_ID = I.LOAN_ID 
     INNER JOIN insurance.COMPANIES as C ON P.COMPANY_ID = C.ID WHERE P.LOAN_ID = 7

您应该能够使用convert函数删除每个返回字段的时间部分。返回的字段是来自主表还是联接表都没关系。