简体   繁体   English

MS SQL将日期转换为简短

[英]MS SQL Convert date to short

I am trying to get all of the records from a table where the ExpiryDate field is exactly one month away. 我试图从一个表中获取所有记录,其中ExpiryDate字段恰好在一个月之后。

The problem however is that the ExpiryDate is stored with the hours, minutes and seconds. 但问题是ExpiryDate存储的是小时,分钟和秒。 How do I therefore say - Get me all of the records from my table where the ExpiryDate (dd/MM/yyyy) is one month ahead of now (dd/MM/yyyy). 因此,我如何说 - 从我的表中获取所有记录,其中ExpiryDate(dd / MM / yyyy)提前一个月(dd / MM / yyyy)。

This is what I have currently: 这就是我目前所拥有的:

SELECT *    FROM dbo.Custom_MembershipTransaction mt (nolock)
WHERE       mt.ExpiryDate = DATEADD(MONTH, 1, GETDATE())
AND         mt.IsComplete = 1;

Try this: 尝试这个:

SELECT *    FROM dbo.Custom_MembershipTransaction mt (nolock)
WHERE       convert(varchar, mt.ExpiryDate, 102) = convert(varchar, DATEADD(MONTH, 1, GETDATE()),102)
AND         mt.IsComplete = 1;

You need some buffertime like 你需要一些喜欢的时间

SELECT *    FROM dbo.Custom_MembershipTransaction mt (nolock)
WHERE       mt.ExpiryDate < DATEADD(MONTH, 1, GETDATE())
AND         mt.ExpiryDate > DATEADD(DAY, -1, DATEADD(MONTH, 1, GETDATE()))
AND         mt.IsComplete = 1;

This will get you all recors that have an expiredate between 1 month ahead an (1 month - 1 day) ahead. 这将为您提供所有已提前1个月(1个月 - 1天)过期的回忆录。

SELECT *    FROM dbo.Custom_MembershipTransaction mt (nolock)
WHERE       mt.ExpiryDate >= DATEADD(DAY, DATEDIFF(DAY, 0, DATEADD(MONTH, 1, GETDATE())), 0)
AND         mt.ExpiryDate < DATEADD(DAY, DATEDIFF(DAY, 0, DATEADD(MONTH, 1, GETDATE()) + 1), 0)
AND         mt.IsComplete = 1;

I didn't make operation on ExpiryDate as it might be used by an index 我没有在ExpiryDate上进行操作,因为索引可能会使用它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM