开发者

How To Change SQL Query [duplicate]

This question already has answers here: How can I truncate a datetime in SQL Server? (15 answers) Closed 9 years ago.

How can I modify this query to >>> @Date1 is a date (date only no time)

SELECT DISTIN开发者_StackOverflow中文版CT TOP (200) dbo.tb_ShowTime.VenueId, DATEADD(Hour, DATEDIFF(Hour, 0, dbo.tb_ShowTime.StartDateTime), 0) AS StartDateTime
FROM         tb_ShowTime
WHERE     (dbo.tb_ShowTime.IsDeleted = 0) AND (dbo.tb_ShowTime.StartDateTime BETWEEN @Date1 AND @Date2)
UNION
SELECT DISTINCT TOP (200) dbo.tb_EventSectionTime.VenueId, DATEADD(Hour, DATEDIFF(Hour, 0, dbo.tb_EventSectionTime.EventTime), 0) AS StartDateTime
FROM         tb_EventSectionTime
WHERE     (dbo.tb_EventSectionTime.IsDeleted = 0) AND (dbo.tb_EventSectionTime.EventTime BETWEEN @Date1 AND @Date2)
ORDER BY StartDateTime 


Not sure which dbms you're using, but in Sybase you can convert to string then back to datetime, to lose time portion. Sure you can do something similar with others.

convert(datetime,convert(varchar,@Date1,109))


You can use these functions
Day(@date1) return day of the date
Month(@date1) return month of the date
year(@date1)return year of the date


When I came accross this problem I used CAST(@Date1 as DATE) That is in SQL-Server. It truncates time piece to 00:00:00. If you are using a different DBMS try to find casting function for the relevant engine.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜