Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Error converting data type nvarchar to datetime.

Author  Topic 

collie
Constraint Violating Yak Guru

400 Posts

Posted - 2007-10-07 : 13:28:09
Hi,

I need some help please :-)

I have a stored procedure as below and when I execute it I ge the following error:
Error converting data type nvarchar to datetime.

The date sent to the sp must be in the format dd/MM/yyyy. How can i convert the dates in the sp so that the format will be recognised?


EXEC @return_value = [dbo].[rep_seminars]
@from_date = N'31/01/2000',
@to_date = N'10/10/2007',
@Country_id = 0






ALTER PROCEDURE [dbo].[rep_seminars]

@from_date datetime
,@to_date datetime
,@Country_id int
AS
BEGIN

SET NOCOUNT ON;


if @country_id=0
set @country_id=null


SELECT Seminars.Venue_id, Venues.Venue_Name, Venues.Country_id,
Countries.Country,
CONVERT(datetime, Seminars.Start_Date, 103) AS start_date,
dbo.Users_sys.Given_Names + ' '
+ dbo.Users_sys.SurName AS Presenter
FROM Seminars LEFT OUTER JOIN
Venues ON Seminars.Venue_id = Venues.Venue_id LEFT OUTER JOIN
Users_sys ON Seminars.Presenter_id = Users_sys.user_id LEFT OUTER JOIN
Countries ON Venues.Country_id = Countries.Country_id
WHERE

((Seminars.Start_Date BETWEEN @from_date AND @to_date)
AND (Countries.Country_id = @Country_id or @Country_id is null ))

or

((@from_date is null and @to_date is null) and
(Seminars.Start_Date >= CONVERT(smalldatetime, getdate(), 103))
AND
(Countries.Country_id = @Country_id or @Country_id is null))

END
















Whisky-my beloved dog who died suddenly on the 29/06/06-I miss u so much.

Kristen
Test

22859 Posts

Posted - 2007-10-07 : 13:40:14
Change the receiving parameters on the Sproc to VARCHAR

Leave the "N" off the front of the N'xxx' date constants (that's Nvarchar, which will be slower for this job!)

Convert the 'dd/mm/yyyy' text strings to DATETIME using either:

SET DATEFORMAT DMY (at the top of your Sproc)

or explicitly:

CONVERT(datetime, @from_date_string, 103)

Kristen
Go to Top of Page
   

- Advertisement -