473,407 Members | 2,326 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,407 software developers and data experts.

Date/Time select Problem

Hi NG,

i've got a mystic problem i can't solve, perhaps one of you has a good idea...
i will explain with some code, so fo better understanding my probroblem:
i'll use asp, but for testing the syntax i use the MS Query Aanalyzer.

following Stored Procedure is ok and does it's job fine with "EXECUTE
IntDayView"

CREATE PROCEDURE dbo.IntDayView AS
SELECT Mitarbeiter.UserName, Interviews.Interview_Nr, Ergebnisse.Thema_Nr,
Ergebnisse.Bewertung, Ergebnisse.Bemerkung
FROM Interviews, Mitarbeiter, Ergebnisse
WHERE (Interviews.DatumZeit >=convert(datetime,'2004-02-10 00:00:00',120)
AND Interviews.DatumZeit <=convert(datetime,'2004-05-16 23:59:59',120)
AND Interviews.User_Nr=Mitarbeiter.User_Nr
AND Interviews.Interview_Nr=Ergebnisse.Interview_Nr)

but now, i want to have variables for the Date string, so i tried following
syntax:

CREATE PROCEDURE dbo.IntDayView @DayViewf DATETIME,@DayViewl DATETIME AS
SELECT Mitarbeiter.UserName, Interviews.Interview_Nr, Ergebnisse.Thema_Nr,
Ergebnisse.Bewertung, Ergebnisse.Bemerkung
FROM Interviews, Mitarbeiter, Ergebnisse
WHERE (Interviews.DatumZeit >=@DayViewf
AND Interviews.DatumZeit <=@DayViewl
AND Interviews.User_Nr=Mitarbeiter.User_Nr
AND Interviews.Interview_Nr=Ergebnisse.Interview_Nr)
--------
EXECUTE IntDayView
@DayViewf=convert(datetime,'2004-02-10 00:00:00',120),
@DayViewl=convert(datetime,'2004-05-16 23:59:59',120)

so i tried the convert in execute, and also in the Procedure, but both times an
error occures Error Nr. 156, or 8114,
so something with the convert is wrong but, also if i do it without convert it
does not work.

So how to select by time, if this doesn't work?????
Any suggestions??

Please help me,
thanks

Jan Schmidt

Jul 20 '05 #1
3 3969
Hi Jan,

I think that the first thing to point out is that SQL Server is
smarter than your average toaster, so it doesn't actually need you to
use the CONVERT function when specifying DATETIME values. As long as
you are using an accepted non-ambiguous format it should be fine. For
example, the following code works without error (also notice the use
of the BETWEEN operator, which you might find useful):

CREATE TABLE Test (my_id INT IDENTITY, my_date DATETIME NOT NULL)
GO

INSERT INTO Test VALUES ('2004-01-01')
INSERT INTO Test VALUES ('2003-01-01')
INSERT INTO Test VALUES ('2005-01-01')
INSERT INTO Test VALUES ('2004-06-01')
GO

CREATE PROCEDURE My_Proc
@start_date DATETIME,
@end_date DATETIME
AS
BEGIN
SELECT *
FROM Test
WHERE my_date BETWEEN @start_date AND @end_date
END
GO

EXEC My_Proc @start_date = '2004-01-01', @end_date = '2004-09-01'
GO

DROP PROCEDURE My_Proc
GO

DROP TABLE Test
GO

-Tom.

"Jan Schmidt" <hi*****@gmx.net> wrote in message news:<2h************@uni-berlin.de>...
Hi NG,

i've got a mystic problem i can't solve, perhaps one of you has a good idea...
i will explain with some code, so fo better understanding my probroblem:
i'll use asp, but for testing the syntax i use the MS Query Aanalyzer.

following Stored Procedure is ok and does it's job fine with "EXECUTE
IntDayView"

CREATE PROCEDURE dbo.IntDayView AS
SELECT Mitarbeiter.UserName, Interviews.Interview_Nr, Ergebnisse.Thema_Nr,
Ergebnisse.Bewertung, Ergebnisse.Bemerkung
FROM Interviews, Mitarbeiter, Ergebnisse
WHERE (Interviews.DatumZeit >=convert(datetime,'2004-02-10 00:00:00',120)
AND Interviews.DatumZeit <=convert(datetime,'2004-05-16 23:59:59',120)
AND Interviews.User_Nr=Mitarbeiter.User_Nr
AND Interviews.Interview_Nr=Ergebnisse.Interview_Nr)

but now, i want to have variables for the Date string, so i tried following
syntax:

CREATE PROCEDURE dbo.IntDayView @DayViewf DATETIME,@DayViewl DATETIME AS
SELECT Mitarbeiter.UserName, Interviews.Interview_Nr, Ergebnisse.Thema_Nr,
Ergebnisse.Bewertung, Ergebnisse.Bemerkung
FROM Interviews, Mitarbeiter, Ergebnisse
WHERE (Interviews.DatumZeit >=@DayViewf
AND Interviews.DatumZeit <=@DayViewl
AND Interviews.User_Nr=Mitarbeiter.User_Nr
AND Interviews.Interview_Nr=Ergebnisse.Interview_Nr)
--------
EXECUTE IntDayView
@DayViewf=convert(datetime,'2004-02-10 00:00:00',120),
@DayViewl=convert(datetime,'2004-05-16 23:59:59',120)

so i tried the convert in execute, and also in the Procedure, but both times an
error occures Error Nr. 156, or 8114,
so something with the convert is wrong but, also if i do it without convert it
does not work.

So how to select by time, if this doesn't work?????
Any suggestions??

Please help me,
thanks

Jan Schmidt

Jul 20 '05 #2
Thomas R. Hummel (to********@hotmail.com) writes:
I think that the first thing to point out is that SQL Server is
smarter than your average toaster, so it doesn't actually need you to
use the CONVERT function when specifying DATETIME values. As long as
you are using an accepted non-ambiguous format it should be fine. For
example, the following code works without error (also notice the use
of the BETWEEN operator, which you might find useful):

CREATE TABLE Test (my_id INT IDENTITY, my_date DATETIME NOT NULL)
GO

INSERT INTO Test VALUES ('2004-01-01')


But that is not an unambiguous format. This format fails with SET
DATEFORMAT DMY or SET LANGUAGE GERMAN. There are two safe dateformats:

YYYYMMDD and YYYY-MM-DDTHH:MM:SS[.mmm] T in the latter formats stands
for itself.

--
Erland Sommarskog, SQL Server MVP, so****@algonet.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 20 '05 #3
[posted and mailed, please reply in news]

Jan Schmidt (hi*****@gmx.net) writes:
EXECUTE IntDayView
@DayViewf=convert(datetime,'2004-02-10 00:00:00',120),
@DayViewl=convert(datetime,'2004-05-16 23:59:59',120)

so i tried the convert in execute, and also in the Procedure, but both
times an error occures Error Nr. 156, or 8114, so something with the
convert is wrong but, also if i do it without convert it does not work.


You cannot pass expressions as parameters when you call stored procedure.
You need to do:

DECLARE @d1 datetime,
@d2 datetime
SELECT @d1 = convert(datetime,'2004-02-10 00:00:00',120),
@d2 = convert(datetime,'2004-02-10 23:59:59',120)
EXEC IntDayView @d1, @d2

Or:

EXEC IntDayView '2004-02-10T00:00:00', '2004-02-10T23:59:59'

I would suggest that you rewrite the procedure as:

WHERE (Interviews.DatumZeit >= @DayViewf
AND Interviews.DatumZeit < @DayViewl

Now you can say:

EXEC '20040210', '20040211'

Please see my reply to Thomas Hummel about date formats.



--
Erland Sommarskog, SQL Server MVP, so****@algonet.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 20 '05 #4

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

Similar topics

7
by: vnl | last post by:
I'm trying to run a SQL query but can't find any records when trying to select a certain date. Here's the sql: SELECT field 1, field2, date_and_time, FROM table1 WHERE date_and_time =...
2
by: zaceti | last post by:
I'm new to MySQL and I am having a problem selecting the highest valued date/time for a particular day. Here is the table structure: ...
1
by: Raghu | last post by:
Hello... I am running into a problem while running a query..can some1 help.. this is the query : ************** SELECT * from Table S where S.dtDate1 BETWEEN...
1
by: simina | last post by:
Hi... I have an "appointments" page where the user should (or not necessarily) choose a date and time for his appointment, from 6 combo boxes:year, month, day, hour, minute and AM or PM, without...
1
by: E. Liepins | last post by:
I am working on a vehicle database. We track when a vehicle is borrowed and when it is returned. We also track the number of kilometres travelled on a particular trip. There are several tables:...
6
by: Scott | last post by:
I have a start date and an end date in my project that is defaulted to the current date at run-time using the following code... Date1.Value = Now() Date2.Value = Now() I then have some code...
10
by: Daniel | last post by:
In Microsoft Access I can write a query that includes the criteria: Between Date()-7 And Date() to retrieve the records from a table that fall within the last week. I'm trying to write a...
4
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,...
30
by: fniles | last post by:
On my machine in the office I change the computer setting to English (UK) so the date format is dd/mm/yyyy instead of mm/dd/yyyy for US. This problem happens in either Access or SQL Server. In the...
22
risk32
by: risk32 | last post by:
Hi all. I'm having a little bit of a problem with the new Date() object in JS. I can get it to work just fine, however, I don't want the .getDay() to return a number. I started to create an array...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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:
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...
0
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,...
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.