Wrox Programmer Forums
Go Back   Wrox Programmer Forums > SQL Server > SQL Server 2000 > SQL Server 2000
|
SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2000 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 21st, 2006, 10:47 AM
Registered User
 
Join Date: Jun 2006
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default Syntax error converting datetime from character st

For the following proc:

CREATE PROCEDURE sample
@createdate datetime
AS

Declare @Str varchar(8000)
SET @Str = 'SELECT * from table_name
where CreationDate = "'+convert(datetime,@createdate,101)+'" AND
ID = Number'.
Exec(@Str)

I am getting the error

"Syntax error converting datetime from character string"

Please help.

Thanks.

 
Old August 21st, 2006, 12:49 PM
Friend of Wrox
 
Join Date: May 2006
Posts: 246
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Why involve dynamic SQL?

CREATE PROCEDURE sample
@createdate datetime
AS

SET NOCOUNT ON

SELECT *
from table_name
where CreationDate = @createdate AND ID = Number

 
Old August 22nd, 2006, 08:19 AM
Registered User
 
Join Date: Jun 2006
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Peso,

I need to dynamically pass values to @createdate.

Please reply.

Thanks.

 
Old August 22nd, 2006, 11:10 AM
Authorized User
 
Join Date: Jun 2003
Posts: 95
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi.

As Peso says, you do not need dynamic SQL. Your in-parameter is of type DateTime and does not need to be converted. But, you need to make sure that if you use a string when calling the procedure, then the parameter must be in a format that can be converted to a datetime. If you use 'YYYYMMDD hh:mm' , you should be safe.

Gert

 
Old August 22nd, 2006, 10:11 PM
Registered User
 
Join Date: Jun 2006
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I am passing a value in the format 'MM/DD/YYYY' from a data file, to @createdate using dynamic properties task.

If I use
CreationDate = "'+convert(varchar(20),@createdate,101)+'"

I am getting the syntax error.

Please advise.

Thanks.

 
Old August 24th, 2006, 05:15 AM
Authorized User
 
Join Date: Jun 2003
Posts: 95
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi.

Try to use the format YYYYMMDD instead.

Gert

 
Old August 24th, 2006, 05:48 AM
Friend of Wrox
 
Join Date: May 2006
Posts: 246
Thanks: 0
Thanked 0 Times in 0 Posts
Default

or use SET DATEFORMAT MDY in the stored procedure.

BUT YOU DON'T HAVE TO CONVERT THE DATE IN THE FIRST PLACE!

 
Old August 24th, 2006, 03:16 PM
Registered User
 
Join Date: Jun 2006
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi,

Actually in the proc below

Declare @Str varchar(8000)
SET @Str = 'SELECT * from table_name
where CreationDate = "'+convert(varchar(20),@createdate,101)+'" AND
ID = Number'.
Exec(@Str)


the CreationDate is in the 'MM/DD/YYYY hh:mm:ss' format whereas the date value I am passing to @createdate is in 'MM/DD/YYYY' format


How do I remove/ignore the 'hh:mm:ss' part in the CreationDate?

Please help.

Thanks.

 
Old August 25th, 2006, 12:20 AM
Friend of Wrox
 
Join Date: May 2006
Posts: 246
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Don't use dynamic SQL !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

SELECT *
from table_name
where CONVERT(VARCHAR, CreationDate, 112) = CONVERT(VARCHAR, @CreateDate, 112)
        AND ID = Number
 
Old August 25th, 2006, 12:46 AM
Friend of Wrox
 
Join Date: May 2006
Posts: 246
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Or

SELECT *
from table_name
where CreationDate BETWEEN @CreateDate AND DATEADD(day, 1, @CreateDate)
        AND ID = Number







Similar Threads
Thread Thread Starter Forum Replies Last Post
Syntax error converting datetime from character st vinod_yadav1919 SQL Server 2000 2 September 10th, 2010 10:11 AM
Syntax error converting the varchar value... Earl Hickey ASP.NET 2.0 Basics 5 January 23rd, 2008 10:25 PM
Converting datetime to milliseconds since.. ravishakya SQL Server 2000 1 September 11th, 2006 07:47 AM
Syntax error converting datetime from character st Uppa SQL Server 2000 2 February 8th, 2005 08:01 PM
Error converting data type nvarchar to datetime. Andreas ADO.NET 2 August 18th, 2004 03:03 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.