473,796 Members | 2,867 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Date poition comparison of a datetime field

I have a datetime variable coming from my ASP.NET application that has
a time portion. I give my users the option to perform an equals,
greater than, less than, or between comparison. The trouble comes in
the way the application builds the criteria string. The WHERE clause
passed in is in the format, "(start_dt = '2005/05/16 07:00:00.000')" .

What I want to do is only compare the date portion of start_dt to the
date portion of the passed in time. Manipulating the start_dt with the
built-in SQL functions isn't a problem, but altering the date passed in
from the ASP.NET would be a massive framework change in the app.

Is there any way to only compare the date portions of both the SQL
field and the passed in value?

Thanks.

Jul 23 '05 #1
1 2272
On 16 May 2005 07:54:28 -0700, colinhumber wrote:
I have a datetime variable coming from my ASP.NET application that has
a time portion. I give my users the option to perform an equals,
greater than, less than, or between comparison. The trouble comes in
the way the application builds the criteria string. The WHERE clause
passed in is in the format, "(start_dt = '2005/05/16 07:00:00.000')" .

What I want to do is only compare the date portion of start_dt to the
date portion of the passed in time. Manipulating the start_dt with the
built-in SQL functions isn't a problem, but altering the date passed in
from the ASP.NET would be a massive framework change in the app.

Is there any way to only compare the date portions of both the SQL
field and the passed in value?

Thanks.


Hi colinhumber,

There are several ways to strip the time portion from a datetime column
or variable. My favorite is

DATEADD(day,
DATEDIFF(day, '20050101', datecol),
'20050101')

Note that the choice of '20050101' is purely arbitrary; the same formula
will work with any date constant (or even with an integer constant; some
people use 0, which is implicitly converted to 1900/01/01.

If both the datetime column in your data and the passed value may have a
time portion, you could use the above formula for both. But if there's
an index on the column, then it's better not to use the column as part
of the formula. You could use this little trick:

DECLARE @start datetime, @end datetime
SET @start = DATEADD(day,
DATEDIFF(day, '20050101', @PassedDate),
'20050101')
SET @end = DATEADD(day, 1, @start)
SELECT ???
FROM ???
WHERE DateTimeColumn >= @start
AND DateTimeColumn < @End

For much more information about datetime data in SQL Server, check out
Tibor's page: http://www.karaszi.com/SQLServer/info_datetime.asp.

Best, Hugo
--

(Remove _NO_ and _SPAM_ to get my e-mail address)
Jul 23 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
13133
by: Donnal Walter | last post by:
I was very surprised to discover that >>> import datetime >>> x = datetime.date(2004, 9, 14) >>> y = datetime.datetime(2004, 9, 14, 6, 43, 15) >>> print x == y True How can these two objects be considered equal? Is there a *general* way to test for date != datetime as well as 4.5 != 4.6?
4
5784
by: Russell | last post by:
I'm having a fit with a query for a range of dates. The dates are being returned from a view. The table/field that they are being selected from stores them as varchar and that same field also stores other fields from our dynamic forms. The field is called 'FormItemAnswer' and stores text, integer, date, float, etc. Anything the user can type into one of our web forms. The query looks like, select distinct from...
4
350
by: Robert Scarborough | last post by:
I have a Table in a Typed Dataset which contains a Date field called EventDate. I've ensured that the field is defined as Date as opposed to DateTime in the Typed Dataset. When I generate an xml file from an instance of this typed dataset using the ds.WriteXml method, all the dates come out formatted as follows: <EventDate>2004-01-26T00:00:00.0000000-05:00</EventDate>
5
1433
by: Dinçer | last post by:
I need to compare dates. But this way: I get a date from database. I need to understand if this date is newer then "6 months" or not. What I need is get the current date, calculate the date 6 months ago, compare "the date from DB" & "the date 6 months ago from now" show the result.
4
5565
by: jty202 | last post by:
I have string that contains a date in this format (14-Jan-05). I want to store in date object if theres one and access each part of the date (month, year, dates, day of week). Specifically I want to convert it in this format 20050114 because this way, I can compare it with another date in this format to see which is greater. Can something show me how to do that. Thanks
6
8493
by: MarkAurit | last post by:
Im having difficulty coming up with a good algorithm to express the following comparison: "if <a given date> falls between the (current date - 5 days) and the (current date)" Obviously. DateTime.Now and something like (AddDays(DateTime.Now,-5) are used for the inner and outer ranges, its how to express the "between" that has me. /* what Id like to do, in pseudo code */ dateToTest=DateTime.Parse("mm/dd/yy");
9
1976
by: Shapper | last post by:
Hello, I created a dataset from a XML file. One of the dataset fields is . In an ASP:Repeater I am displaying the field. <%# DataBinder.Eval(Container.DataItem, "pubDate", "{0:f}") %> The date displayed has the following format, the same as in the XML file:
8
1380
by: JFB | last post by:
Hi All, I'm trying to find if a file name that includes the date is before todays to do some procedures. In my IF statement is working is just a few dates before but not is a year before. How can I fix this? Tks in advance... This is what I have: 'Set today's date
7
2774
by: mr.nimz | last post by:
hello, this is antenio. recently i've come to a problem. i got a way through it, somehow, still it left me in a curious state, so i'm posting it here, if i can get an answer from some techy, here is my table structure, Name: Table1
0
9673
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9525
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10169
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9050
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5440
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5569
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4115
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3730
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2924
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.