Most efficient way in SQL Server to get date from date+time?
在MS SQL 2000和2005年,一起为DateTime搜索2008年09月25 12:56 34:什么是最有效的方式得到一个DateTime含只是2008年09月25?
复制在这里。
(P)I must admit I hadn't seen the floor-float conversion shown by matt before.我要测试这个出来。(p)(P)I tested a pure select(which will return date and time,and is not what we want),the reigning solution here(floor-float),a common'naive'one mentioned here(stringconvert)and the one mentioned here that I was using(as I thought it was the fast test).(p)(P)I tested the want on a test-server MS SQL server 2005 running on a win 2003 SP2 server with a Xeon 3GHZ CPU running on max memory(32 bit,so that's about 3.5 gb).It's night where I am so the machine i s idling along at almost no load.I've got it all to myself.(p)(P)这是我的测试结果——从一个大型表格中选出的数据,其中包括从千年发展目标的时间变化。This particular database includes dates ranging over 2.5 years.The table itself has over 13 million rows,so that's why I restrict to the top million.(p)字母名称布尔奇1(P)我们在这里看到了什么?(p)(P)Let's's focus on the CPU time(we're looking at conversion),and we can see that we have the following numbers:(p)字母名称(P)From this it look to me like the dated(at least in this particular case)is slightly faster than the floor-cast method.(p)(P)在你走之前,我走过了这一考验的多个时代,有着不断变化的愿望,同样的结果。(p)(P)这是我服侍的地方,还是什么?(p)
字母名称(P)Datediff(Day,0,Gettate(…))is the same as dateff(Day,'1900-01-01',Gettate(…))(p)(P)随着Datediff Returns an Integer,you will get the number of days that have Elapsed since Jan 1190.You then add that integer number of days to Jan 1190.The Net effect is removing the time component.(p)(P)I should also mention that this method works for any date/time part(like year,quarter,month,day,hour,minute,and second).(p)字母名称(P)The last one,for seconds,requires special handling.如果你使用Jan 1,1900你会犯一个错误。(p)(P)2.Difference of two datetings caused overflow at runtime.(p)(P)You can circumvent this mistake by using a different reference date(Like Jan 1,2000).(p)
字母名称(P)Works because casting a datetime to flat gives the number of days(including fractions of a day)since Jan 1190.Flooring it removes the fractional days and leaves the number of whole days,which can they be cast back to a datetime.(p)
(P)在2012年使用(p)字母名称
字母名称
(P)字母名称:0You can see the proof&;tests when get the date without time in sql server(p)
(P)三种方法描述在连接的领域。I haven't performance tested them to determine which is quickest.(p)(P)http://www.blackwasp.co.uk/sqldatefrmdatetime.aspx(p)
(P)转换,Floor,and datediff will perform just the same.(p)(P)How to return the date part only from a sql server date dattype(p)
(P)To get YYYY-MM-DD,use:(p)字母名称(P)Edit:近东救济工程处,he wants a datetime instead of a string.The equivalent of trunc(……)in oracle.You can take what I posted and cast back to a date:(p)字母名称
(P)What about EDOCX1 plus 1?是(p)
字母名称