﻿<?xml version='1.0' encoding='UTF-8'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>SQLServerCentral / SQL Server 2005 / T-SQL (SS2K5)  / Getting datepart to work for query / Latest Posts</title><generator>InstantForum.NET v2.9.0</generator><description>SQLServerCentral</description><link>http://www.sqlservercentral.com/Forums/</link><webMaster>notifications@sqlservercentral.com</webMaster><lastBuildDate>Sat, 18 May 2013 03:01:04 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: Getting datepart to work for query</title><link>http://www.sqlservercentral.com/Forums/Topic743164-338-1.aspx</link><description>Queries for a date range should normally be in this form:[code]where MyDatetimeColumn &gt;= @StartDatetime  and MyDatetimeColumn &lt; @EndDatetime[/code]In other words greater than or equal to the first point in time that we know that we want, and less than the first point in time that we don't want.There is never any guarantee that the precision of time intervals will stay the same in future releases, so it is unrealistic to code end time as "2009-06-29 23:59:59.997", even if this is the last possible moment that we want to select with the current version of SQL Server.  Just code it as less than '2009-06-30'.</description><pubDate>Mon, 29 Jun 2009 21:23:44 GMT</pubDate><dc:creator>Michael Valentine Jones</dc:creator></item><item><title>RE: Getting datepart to work for query</title><link>http://www.sqlservercentral.com/Forums/Topic743164-338-1.aspx</link><description>[quote][b]Mark Eytcheson (6/29/2009)[/b][hr]I usually create two datasets:1. StartDateSELECT CAST((CONVERT(varchar(10), GETDATE()-9, 101)) AS DATETIME) AS StartDate2. EndDateSELECT CAST((CONVERT(varchar(10), GETDATE()-2, 101) + ' 23:59:59') AS DATETIME) AS EndDateThen set the parameters @StartDate and @EndDate equal to the values of these two datasets.[/quote]SELECT CAST((CONVERT(varchar(10), GETDATE()-2, 101) + ' 23:59:59') AS DATETIME) AS EndDate --&gt; This will miss any records with a datetime value &gt; [Date] 23:59:59.000 and [Date + 1] 00:00:00.000.</description><pubDate>Mon, 29 Jun 2009 13:26:41 GMT</pubDate><dc:creator>Lynn Pettis</dc:creator></item><item><title>RE: Getting datepart to work for query</title><link>http://www.sqlservercentral.com/Forums/Topic743164-338-1.aspx</link><description>I usually create two datasets:1. StartDateSELECT CAST((CONVERT(varchar(10), GETDATE()-9, 101)) AS DATETIME) AS StartDate2. EndDateSELECT CAST((CONVERT(varchar(10), GETDATE()-2, 101) + ' 23:59:59') AS DATETIME) AS EndDateThen set the parameters @StartDate and @EndDate equal to the values of these two datasets.</description><pubDate>Mon, 29 Jun 2009 13:22:56 GMT</pubDate><dc:creator>Mark Eytcheson</dc:creator></item><item><title>RE: Getting datepart to work for query</title><link>http://www.sqlservercentral.com/Forums/Topic743164-338-1.aspx</link><description>Thanks so much for helping out. I will use the syntax posted in the solution.Here is the part that I had needed help with in my where clause...(where  (timesheet.wcstart between [b]@start_date and @end_date [/b]and timesheet.wcfinish is null))INSERT INTO #timesheet            SELECT      emp.firstname + ' ' + emp.surname, mo.monumber, inv.customer_part_num,  inv.description, t.opno, routing.description,workcenter.name,              Case         When timesheet.wcstart &lt; @start_date then @start_date                              Else timesheet.wcstart                        End,                timesheet.wcfinish,                        ISNULL(workcenter.machine,'X'),                        '',                TSNo               FROM mfg_timesheet t                Left outer join cubs..mfg_mo_routing routing On                    routing.companyid = timesheet.companyid and                            routing.mo_key = timesheet.mo_key and                           routing.opno = timesheet.opno                         Left outer join cubs..mfg_workcenter workcenter On                    workcenter.wcno = timesheet.wcno and                   workcenter.companyid = timesheet.companyid,                       mfg_mo mo, inv_item inv,mfg_employee emp,slsord sls                  where  (timesheet.wcstart between [b]@start_date and @end_date [/b]and timesheet.wcfinish is null) and                        mo.mo_key = timesheet.mo_key and                        emp.employeeid = timesheet.employeeid and                        mo.companyid = inv.company_id and                        mo.stocknumber = inv.stock_number and                        timesheet.companyid = 1 and                         workcenter.machine IN ('W') And                TSNo NOT IN (SELECT TsNo FROM mfg_timesheetAdj) and                sls.company_id = mo.companyid and                sls.slsord_number = mo.slsord_number</description><pubDate>Sun, 28 Jun 2009 22:45:57 GMT</pubDate><dc:creator>shree dhavale</dc:creator></item><item><title>RE: Getting datepart to work for query</title><link>http://www.sqlservercentral.com/Forums/Topic743164-338-1.aspx</link><description>[code]select        *from        MyTablewhere        -- Greater than or equal to yesterday at 04:30        MyDate &gt;= dateadd(day,datediff(day,0,getdate())-1,'04:30')	and        -- Less than today at 04:30        MyDate &lt;  dateadd(day,datediff(day,0,getdate())  ,'04:30')[/code]</description><pubDate>Sat, 27 Jun 2009 22:53:28 GMT</pubDate><dc:creator>Michael Valentine Jones</dc:creator></item><item><title>RE: Getting datepart to work for query</title><link>http://www.sqlservercentral.com/Forums/Topic743164-338-1.aspx</link><description>Here are a few things for you to look at:Select dateadd(minute, 990, dateadd(day, datediff(day, 0, getdate()), 0))       ,dateadd(minute, 990, dateadd(day, datediff(day, 0, getdate()) - 1, 0))Now, with the above you could do something like:Select {columns}From YourTableWhere YourDate &gt;= dateadd(minute, 990, dateadd(day, datediff(day, 0, getdate()), 0))And YourDate &lt; dateadd(minute, 990, dateadd(day, datediff(day, 0, getdate()) - 1, 0))</description><pubDate>Sat, 27 Jun 2009 19:45:00 GMT</pubDate><dc:creator>Jeffrey Williams 3188</dc:creator></item><item><title>RE: Getting datepart to work for query</title><link>http://www.sqlservercentral.com/Forums/Topic743164-338-1.aspx</link><description>shree dhavaleIt would help those who will be attempting to help you if you provided the table structure, sample data and the T-SQL statement that you have attempted to utilize.  All this per the article whose link is in my signature block.</description><pubDate>Sat, 27 Jun 2009 15:14:25 GMT</pubDate><dc:creator>bitbucket-25253</dc:creator></item><item><title>Getting datepart to work for query</title><link>http://www.sqlservercentral.com/Forums/Topic743164-338-1.aspx</link><description>Hi,I have a report where I need to see all current activity in a mfg shop from 4:30 am of yesterday to 4:29 am of today. I cant seem get the datepart to work for my time part of the date. Could someone please help me with the syntax for - Where startdate &gt;= yesterday 4:30 am and startdate &lt;=today 4:29 amThere is also a report I need to generate for Monday 4:30a.m to Saturday 4:30 am every week.Thanks for all the help in advance!Shree</description><pubDate>Sat, 27 Jun 2009 13:50:10 GMT</pubDate><dc:creator>shree dhavale</dc:creator></item></channel></rss>