|
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
|
|
|
August 21st, 2006, 10:47 AM
|
Registered User
|
|
Join Date: Jun 2006
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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.
|
August 21st, 2006, 12:49 PM
|
Friend of Wrox
|
|
Join Date: May 2006
Posts: 246
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Why involve dynamic SQL?
CREATE PROCEDURE sample
@createdate datetime
AS
SET NOCOUNT ON
SELECT *
from table_name
where CreationDate = @createdate AND ID = Number
|
August 22nd, 2006, 08:19 AM
|
Registered User
|
|
Join Date: Jun 2006
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Hi Peso,
I need to dynamically pass values to @createdate.
Please reply.
Thanks.
|
August 22nd, 2006, 11:10 AM
|
Authorized User
|
|
Join Date: Jun 2003
Posts: 95
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|
August 22nd, 2006, 10:11 PM
|
Registered User
|
|
Join Date: Jun 2006
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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.
|
August 24th, 2006, 05:15 AM
|
Authorized User
|
|
Join Date: Jun 2003
Posts: 95
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Hi.
Try to use the format YYYYMMDD instead.
Gert
|
August 24th, 2006, 05:48 AM
|
Friend of Wrox
|
|
Join Date: May 2006
Posts: 246
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
or use SET DATEFORMAT MDY in the stored procedure.
BUT YOU DON'T HAVE TO CONVERT THE DATE IN THE FIRST PLACE!
|
August 24th, 2006, 03:16 PM
|
Registered User
|
|
Join Date: Jun 2006
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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.
|
August 25th, 2006, 12:20 AM
|
Friend of Wrox
|
|
Join Date: May 2006
Posts: 246
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Don't use dynamic SQL !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
SELECT *
from table_name
where CONVERT(VARCHAR, CreationDate, 112) = CONVERT(VARCHAR, @CreateDate, 112)
AND ID = Number
|
August 25th, 2006, 12:46 AM
|
Friend of Wrox
|
|
Join Date: May 2006
Posts: 246
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Or
SELECT *
from table_name
where CreationDate BETWEEN @CreateDate AND DATEADD(day, 1, @CreateDate)
AND ID = Number
|
|
|