473,396 Members | 1,967 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Dates in SQL Query

Hi,
I am trying to get the results out of an SQL database where the date is
greater than todays date.
this is thecode that I am trying to use.

SQLQuery = new SqlDataAdapter("SELECT Id, Title, Publication, InsertDate,
NoPages, Colour, Status " +
"FROM Features WHERE InsertDate >#" +
DateTime.Now.ToString("dd/MM/yyyy") +
"# AND Publication='x' OR Publication='y' ORDER BY " + strSortBy + ";",
DataBaseConnection);

All I keep getting is this error "Incorrect syntax near '#'." What am I
doing wrong. The query is copied straight from an access database query to
the same SQL Database.

Thanks for any help
Kenneth
Nov 18 '05 #1
3 1795
As a Rule to avaoid problems with international dates etc I always build
my date queries like this. Not perfect but will get you past your problem.

WHERE Date_Column > convert(datetime,'01 Jan 2004')

"Kenneth Keeley" <ke*******@hotmail.com> wrote in message
news:eE**************@TK2MSFTNGP14.phx.gbl...
Hi,
I am trying to get the results out of an SQL database where the date is
greater than todays date.
this is thecode that I am trying to use.

SQLQuery = new SqlDataAdapter("SELECT Id, Title, Publication, InsertDate,
NoPages, Colour, Status " +
"FROM Features WHERE InsertDate >#" +
DateTime.Now.ToString("dd/MM/yyyy") +
"# AND Publication='x' OR Publication='y' ORDER BY " + strSortBy + ";", DataBaseConnection);

All I keep getting is this error "Incorrect syntax near '#'." What am I
doing wrong. The query is copied straight from an access database query to
the same SQL Database.

Thanks for any help
Kenneth

Nov 18 '05 #2
Hi,

The general recommendation is to use sql parameters for queries - it is
safer, faster and errorproof. For examples see:

http://msdn.microsoft.com/library/en...ClassTopic.asp
http://msdn.microsoft.com/library/en...etersTopic.asp
http://msdn.microsoft.com/library/en...mmandTopic.asp

Hope this helps
Martin

"Kenneth Keeley" <ke*******@hotmail.com> wrote in message
news:eE**************@TK2MSFTNGP14.phx.gbl...
Hi,
I am trying to get the results out of an SQL database where the date is
greater than todays date.
this is thecode that I am trying to use.

SQLQuery = new SqlDataAdapter("SELECT Id, Title, Publication, InsertDate,
NoPages, Colour, Status " +
"FROM Features WHERE InsertDate >#" +
DateTime.Now.ToString("dd/MM/yyyy") +
"# AND Publication='x' OR Publication='y' ORDER BY " + strSortBy + ";", DataBaseConnection);

All I keep getting is this error "Incorrect syntax near '#'." What am I
doing wrong. The query is copied straight from an access database query to
the same SQL Database.

Thanks for any help
Kenneth

Nov 18 '05 #3
Kenneth Keeley wrote:
Hi,
I am trying to get the results out of an SQL database where the date
is greater than todays date.
this is thecode that I am trying to use.

SQLQuery = new SqlDataAdapter("SELECT Id, Title, Publication,
InsertDate, NoPages, Colour, Status " +
"FROM Features WHERE InsertDate >#" +
DateTime.Now.ToString("dd/MM/yyyy") +
"# AND Publication='x' OR Publication='y' ORDER BY " + strSortBy
+ ";", DataBaseConnection);

All I keep getting is this error "Incorrect syntax near '#'." What am
I doing wrong. The query is copied straight from an access database
query to the same SQL Database.

Thanks for any help
Kenneth


The "#" syntax for dates is Access-specific, that's why you get syntax errors
on SqlServer.
Dates and strings are always difficult. As others said, it is better to use parameters,
there you can use the plain DateTime value, without string conversion.

Hans Kesting
Nov 18 '05 #4

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

Similar topics

8
by: Donna Sabol | last post by:
First, I should start by saying I am creating a database to be used by some very impatient, non-computer literate people. It needs to be seameless in it's operation from their point of view. I...
5
by: Mal | last post by:
Hello. I have a database that tracks reservations at a campground. I want to be able to make a calendar type report that shows how many people are here in given period. Stored for each...
1
by: Peter Bailey | last post by:
I have a student table with studentid as pk and an enrolment date field as date. I have made a qry to group on date and count the number of enrolments for a particular day. What this doesnt...
1
by: Don Sealer | last post by:
I have a report that includes 5 different subreports. I'd like to be able to open this report using a date function (Start Date and End Date). I'd like all five subreports to show the data from...
6
by: Bill R via AccessMonster.com | last post by:
I have a query: SELECT tblCalendar.CalendarDay AS LastSunday FROM tblCalendar WHERE (((tblCalendar.CalendarDay)>=(Now()-7) And (tblCalendar.CalendarDay)...
18
by: PC Datasheet | last post by:
An Access user saw my name in a newsgroup and sent me a request for help on a project. As part of the project, a list of the dates in a month was needed. For anyone needing a list of dates in a...
1
by: yk | last post by:
I am setting up a simple hotel reservation application which have two dates, i.e. CheckIn and CheckOut. I would like to construct a graph based on a crosstab query. Therefore, I need to have...
10
by: John | last post by:
Hi I have a form with tow fields for dates in dd/mm/yyyy format. I am trying to use the fields in a query's where clause as below; "SELECT * " & _ "FROM Orders " & _ " WHERE Orders.)>= #" &...
1
by: pitfour.ferguson | last post by:
My dbase has the start date and end date of each visit. How can I ask Access to list the day of the week of the start (easy), end (easy) and, more importantly, the dates of the visit itself - ie...
13
newnewbie
by: newnewbie | last post by:
I have an Access database that I upload a data extract intoto daily. I want to create a query that will give me a list of dates that no data can be found for. E.g. there are no records created on the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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...
0
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...
0
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,...

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.