473,799 Members | 3,009 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

ASP Date: get records with date = today (SQL Server)

Hello!

I'm trying to get all records from my SQL Server Database with
"DeadlineDa te" = today (not today - 24 hours).

All records has a field called "DeadlineDa te", and the date is stored in
this field like this: 13.08.2005 07:00:00

I dont care about the hours (Ex: 07:00:00), just the date (ex: 13.08.2005).
This is the SQL I have made, it gets all the record with the date = today -
24 hours... but that is not what I want.

sql = "select title from tblProject where (deadlineDate BETWEEN DATEADD(d, -
1, GETDATE()) AND GETDATE())"

How can I get just the records that has the date = today's date??
Thanks for all tips :)
Aug 13 '05 #1
7 60508
Vinnie Davidson wrote:
Hello!

I'm trying to get all records from my SQL Server Database with
"DeadlineDa te" = today (not today - 24 hours).

All records has a field called "DeadlineDa te", and the date is stored
in this field like this: 13.08.2005 07:00:00

I dont care about the hours (Ex: 07:00:00), just the date (ex:
13.08.2005). This is the SQL I have made, it gets all the record with
the date = today - 24 hours... but that is not what I want.


This expression strips the time from a date (I'll use GETDATE() to supply
the date in this example, but any datetime variable could be used):

dateadd(day,dat ediff(day,0,GET DATE() )*, 0)

Where DeadlineDate >= dateadd(day,dat ediff(day,0,GET DATE() )*, 0)
AND DeadlineDate <
dateadd(day,1,d ateadd(day,date diff(day,0,GETD ATE() )*, 0) )

It looks as if you are using dynamic sql which can leave your site
vulnerable to hackers using sql injection:
http://mvp.unixwiz.net/techtips/sql-injection.html
http://www.sqlsecurity.com/DesktopDefault.aspx?tabid=23

You can prevent sql injection by using parameters, either via stored
procedures:
http://tinyurl.com/jyy0

or by using a Command object to pass parameters to a string containing ODBC
parameter markers:
http://groups-beta.google.com/group/...e36562fee7804e

HTH,
Bob Barrows
--
Microsoft MVP - ASP/ASP.NET
Please reply to the newsgroup. This email account is my spam trap so I
don't check it very often. If you must reply off-line, then remove the
"NO SPAM"
Aug 13 '05 #2
Vinnie Davidson wrote:
sql = "select title from tblProject where (deadlineDate BETWEEN
DATEADD(d, - 1, GETDATE()) AND GETDATE())"

How can I get just the records that has the date = today's date??


For starters, you can use VBScript's [Date], which contains no time info:
http://msdn.microsoft.com/library/en.../vsfctdate.asp

The rest is simple, especially if you are using a stored procedure, such as:

CREATE PROCEDURE GetProjectsByDa te
@D DATETIME
AS
SELECT Title
FROM tblProject
WHERE DeadlineDate BETWEEN @D AND DATEADD(d,1,@D)
GO

VBScript usage:
Set oRS = oCN.Execute("Ge tProjectsByDate '" & Date & "'")

If you insist on dynamic SQL, you can do something like this:
Today = Date
Tomorrow = DateAdd("d",1,T oday)
SQL = "select title from tblProject where deadlineDate " _
"BETWEEN '" & Today & "' AND '" & Tomorrow & "'"

--
Dave Anderson

Unsolicited commercial email will be read at a cost of $500 per message. Use
of this email address implies consent to these terms. Please do not contact
me directly or ask me to contact you directly for assistance. If your
question is worth asking, it's worth posting.
Aug 13 '05 #3
try this:

select cast(cast(getda te() as integer) as datetime)


"Vinnie Davidson" <ad***@webressu rs.no> wrote in message
news:OJ******** ******@TK2MSFTN GP09.phx.gbl...
Hello!

I'm trying to get all records from my SQL Server Database with
"DeadlineDa te" = today (not today - 24 hours).

All records has a field called "DeadlineDa te", and the date is stored in
this field like this: 13.08.2005 07:00:00

I dont care about the hours (Ex: 07:00:00), just the date (ex:
13.08.2005). This is the SQL I have made, it gets all the record with the
date = today - 24 hours... but that is not what I want.

sql = "select title from tblProject where (deadlineDate BETWEEN
DATEADD(d, - 1, GETDATE()) AND GETDATE())"

How can I get just the records that has the date = today's date??
Thanks for all tips :)

Aug 14 '05 #4
or for your case, can try this code in your asp page
<%
strDate=right(" 00" & month(now()),2) & "/" & right("00" & day(now()),2) &
"/" & year(now())

strSQL="select * from table_name where cast(cast(Deadl ineDate as integer) as
datetime)='" & strDate & "'"

objConnection.e xecute strSQL
.....
%>
"Vinnie Davidson" <ad***@webressu rs.no> wrote in message
news:OJ******** ******@TK2MSFTN GP09.phx.gbl...
Hello!

I'm trying to get all records from my SQL Server Database with
"DeadlineDa te" = today (not today - 24 hours).

All records has a field called "DeadlineDa te", and the date is stored in
this field like this: 13.08.2005 07:00:00

I dont care about the hours (Ex: 07:00:00), just the date (ex:
13.08.2005). This is the SQL I have made, it gets all the record with the
date = today - 24 hours... but that is not what I want.

sql = "select title from tblProject where (deadlineDate BETWEEN
DATEADD(d, - 1, GETDATE()) AND GETDATE())"

How can I get just the records that has the date = today's date??
Thanks for all tips :)

Aug 14 '05 #5
Michael wrote:
or for your case, can try this code in your asp page
<%
strDate=right(" 00" & month(now()),2) & "/" & right("00" &
day(now()),2) & "/" & year(now())

strSQL="select * from table_name where cast(cast(Deadl ineDate as
integer) as datetime)='" & strDate & "'"

objConnection.e xecute strSQL
....


You may find that this "works", but, if you have an index on DeadlineDate,
you will find tat it will not be used, leading your query to perform a table
scan, not exactly the quickest way to extract data from a table ...

Bob Barrows

--
Microsoft MVP - ASP/ASP.NET
Please reply to the newsgroup. This email account is my spam trap so I
don't check it very often. If you must reply off-line, then remove the
"NO SPAM"
Aug 14 '05 #6
> WHERE DeadlineDate BETWEEN @D AND DATEADD(d,1,@D)

I'd really stay away from BETWEEN here. This says

WHERE col BETWEEN '20050815 0:00' AND '20050816 0:00'

The nature of the data might be such that many rows are inserted for the
16th with no time associated, so many rows for the 16th will come back with
data for the 16th.

I would much prefer

WHERE col >= @D AND col < (@D + 1)

The back side of the range should not include the end point because it's a
different day. I wrote about this here:
http://www.aspfaq.com/2280

A
Aug 15 '05 #7
> Set oRS = oCN.Execute("Ge tProjectsByDate '" & Date & "'")

And the problem with this, as opposed to letting SQL Server figure out what
today is, is that your web server and SQL Server better be in sync, or else
you could get wrong data or an error, e.g. if VBScript gives you dd/mm/yyyy
and SQL Server is expecting mm/dd/yyyy.
Aug 15 '05 #8

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

Similar topics

16
7288
by: !TG | last post by:
I have a table with a date field. All I want to do it get all the records with a date before today. I tried the following: "SELECT * FROM StateLicenses Where (Exp < #07/26/2005#) Order By BranchNo,Satellite;" "SELECT * FROM StateLicenses Where (Exp < 07/26/2005) Order By BranchNo,Satellite;" "SELECT * FROM StateLicenses Where (Exp < " & Now() & ") Order By BranchNo,Satellite;" "SELECT * FROM StateLicenses Where (Exp < #" & Now() & "#)...
3
11584
by: Lyn | last post by:
Hi, I am developing a project in which I am checking for records with overlapping start/end dates. Record dates must not overlap date of birth, date of death, be in the future, and must not overlap existing records from the same table. I had this all working some time ago, but recently when I have gone back to do more testing, part of these validations no longer work. While there have been changes to the code in the meantime, I cannot...
5
2305
by: Darrel | last post by:
I'm trying to check a few fields in my SQL query to ensure that they fall between a range (namly that today's date is greater than one field and less than another field.) This is what I have: ========================================= dim strCurrentDate as String = microsoft.VisualBasic.DateAndTime.Today().ToString("mm/dd/yyyy")
6
1358
by: darrel | last post by:
I'm grabbing today's date with some ASP.net code: Dim Today As Date Today = Microsoft.VisualBasic.Today() I'm then trying to run a SQL query with the following WHERE statement: WHERE postdate <= " & Today & " But it's pulling up records that have a postdate in the future.
15
18896
by: Khurram | last post by:
I have a problem while inserting time value in the datetime Field. I want to Insert only time value in this format (08:15:39) into the SQL Date time Field. I tried to many ways, I can extract the value in timeonly format by using this command Format(now,"HH:mm:ss") But when I insert it into the Sql Server database, it embadded date value with it. the output looks like that "01/01/1900 08:59:00" in that case time is
3
4037
by: noone | last post by:
Hi, I am designing an application which displays news topics until midnight on the DisplayUntil date and then they should drop out. Unfortunately, they seem to be dropping out at mid-day. I'm storing the 'DisplayFrom' and 'DisplayUntil' dates as SmallDateTime fields so the date is in the format "25/09/2006 00:0:00" and I'm comparing them with GetDate() which (I suspect) includes the actual time as well.
2
10450
by: AKorsakova | last post by:
Hi Everyone, I am trying to write something to give me back all the data for a sertain time range for today. So for example: I need to get all records where change_date is <= today 2pm and today at 8pm. I know i can get just the date for today by using CONVERT(CHAR(10),getdate(),102) but can i add a time range to that? Thanks in advance,
4
3170
by: Simon Gare | last post by:
Hi all, I am trying to retrieve a count of booking entries made 30 days ago, below is the end of the query I am having problems with. dbo.booking_form.TimeOfBooking = DATEADD(day, -30, GetDate()) GROUP BY dbo.booking_form.TimeOfBooking") When I use the = sign the error reads
8
2430
by: Trev | last post by:
Hi Can anyone point me in the right direction here, I would like to open a table in access 2003 by date. I have an asp web page which needs to read data from a table with each days today's date (which ever day that is) then a new table is created with today's date. Example: I have a table called 17-may-2007 my ASP page reads this table for 24hours then tomorrow (12:00 midnight 18th) I will have a new table called 18-may-2007 and the...
0
9541
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,...
0
10485
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10231
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
10027
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9073
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...
1
7565
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6805
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5463
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...
2
3759
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.