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

Problem with DateTime and strings in stored procedures

I'm keeping in the database a log of all the sessions for my
application. I'm trying to write a stored procedure that returns all
the sessions that; the login contains a certain string, loggedin after
a certain datetime and loggedout before another datetime. Any
combination of these parameters can be used and, if none, returns all
the log.

Below is the code I came up with but I'm having a "Syntax error
converting datetime from character string" exception. When not using
DateTime parameters everything works fine. Can you tell me how can I
avoid this exception? Thanks in advance...

ALTER PROCEDURE dbo.RetrieveAllSessionHistoryItemsContaining
(
@Pattern Varchar(255),
@From DateTime,
@To DateTime
)
AS
DECLARE @Query VARCHAR(500)
SET @Query = 'SELECT * FROM SessionHistoryItems, Sessions WHERE
SessionHistoryItems.SessionId = Sessions.SessionId'

DECLARE @conditions nvarchar(257)
SET @conditions = '';

IF LEN(@Pattern) > 0 BEGIN
SET @conditions = @conditions + ' Sessions.Login LIKE ''%' + @Pattern
+ '%'''
END

IF @From IS NOT NULL BEGIN
IF LEN(@conditions) > 0 BEGIN
SET @conditions = @conditions + ' AND '
END
SET @conditions = @conditions + ' SessionHistoryItems.LoggedOutAt >=
' + @From
END

IF @To IS NOT NULL BEGIN
IF LEN(@conditions) > 0 BEGIN
SET @conditions = @conditions + ' AND '
END
SET @conditions = @conditions + ' SessionHistoryItems.LoggedInAt <= '
+ @To
END

IF LEN(@conditions) > 0 BEGIN
EXEC(@Query + ' AND ' + @conditions)
END
ELSE BEGIN
EXEC(@Query)
END
RETURN

Jul 23 '05 #1
1 2043

<an***@iilab.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
I'm keeping in the database a log of all the sessions for my
application. I'm trying to write a stored procedure that returns all
the sessions that; the login contains a certain string, loggedin after
a certain datetime and loggedout before another datetime. Any
combination of these parameters can be used and, if none, returns all
the log.

Below is the code I came up with but I'm having a "Syntax error
converting datetime from character string" exception. When not using
DateTime parameters everything works fine. Can you tell me how can I
avoid this exception? Thanks in advance...

ALTER PROCEDURE dbo.RetrieveAllSessionHistoryItemsContaining
(
@Pattern Varchar(255),
@From DateTime,
@To DateTime
)
AS
DECLARE @Query VARCHAR(500)
SET @Query = 'SELECT * FROM SessionHistoryItems, Sessions WHERE
SessionHistoryItems.SessionId = Sessions.SessionId'

DECLARE @conditions nvarchar(257)
SET @conditions = '';

IF LEN(@Pattern) > 0 BEGIN
SET @conditions = @conditions + ' Sessions.Login LIKE ''%' + @Pattern
+ '%'''
END

IF @From IS NOT NULL BEGIN
IF LEN(@conditions) > 0 BEGIN
SET @conditions = @conditions + ' AND '
END
SET @conditions = @conditions + ' SessionHistoryItems.LoggedOutAt >=
' + @From
END

IF @To IS NOT NULL BEGIN
IF LEN(@conditions) > 0 BEGIN
SET @conditions = @conditions + ' AND '
END
SET @conditions = @conditions + ' SessionHistoryItems.LoggedInAt <= '
+ @To
END

IF LEN(@conditions) > 0 BEGIN
EXEC(@Query + ' AND ' + @conditions)
END
ELSE BEGIN
EXEC(@Query)
END
RETURN


It looks like you need to CAST or CONVERT the datetime to a string (and add
quotes) in order to build up the @conditions string:

declare @dt datetime
set @dt = getdate()

select 'x' + @dt -- fails
select 'x''' + cast(@dt as varchar(20)) + '''' -- succeeds

But in this case, using sp_executesql would probably be a better approach
anyway:

exec sp_executesql
N'select col1, col2 from dbo.MyTable where datecol >= @From and datecol <=
@To',
N'@From datetime, @To datetime',
@From, @To

See sp_executesql in Books Online, and also these articles for more
information/ideas:

http://www.sommarskog.se/dynamic_sql.html
http://www.sommarskog.se/dyn-search.html

Simon
Jul 23 '05 #2

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

Similar topics

4
by: Minh Tran | last post by:
In order to minimize the number of connection strings I have to use to access different databases on the same Sql Server, I was considering storing all stored procedures in just one database. I...
2
by: Demetris | last post by:
Hello people! I have a strange problem with some stored procedures here is a sample of a stored procedure that has a problm with the dates. CREATE PROCEDURE TEST_DATE (OUT G_LAST_DATE DATE) ...
2
by: Steven Blair | last post by:
Hi, I am currently writing a method for calling stored procedures. I want the method to be able to handle variable amount of parameters and need some advice on the design of this. The way I...
8
by: craigkenisston | last post by:
I have a generic function that receives a couple of datetime values to work with. They can or cannot have a value, therefore I wanted to use null. This function will call a database stored...
30
by: dbuchanan | last post by:
ComboBox databindng Problem == How the ComboBox is setup and used: My comboBox is populated by a lookup table. The ValueMember is the lookup table's Id and the DisplayMember is the text from a...
7
by: Dabbler | last post by:
I'm using an ObjectDataSource with a stored procedure and am getting the following error when trying to update (ExecuteNonQuery): System.Data.SqlClient.SqlException: Procedure or Function...
4
by: =?Utf-8?B?QmFidU1hbg==?= | last post by:
Hi, I have a GridView and a SqlDataSource controls on a page. The SqlDataSource object uses stored procedures to do the CRUD operations. The DataSource has three columns one of which -...
6
by: Mark | last post by:
Hi, i have an application which works with date. The regional settings of the computer (XP prof. dutch version) are set to French (Belgium). Asp.net and Sql server take the short date format of...
5
by: Sreenivas | last post by:
Hi every one, I need to compare to datetime values , done in stored procedure.The input datetime parameter is in dd/mm/yyyy hh:mm:ss AM/PM format ,the datetime values to be compared are also...
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: 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:
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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,...
0
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...

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.