Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > SQL Server > SQL Server 2000 > SQL Server 2000
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old August 21st, 2006, 11:47 AM
Registered User
 
Join Date: Jun 2006
Location: , , .
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.

Reply With Quote
  #2 (permalink)  
Old August 21st, 2006, 01:49 PM
Friend of Wrox
 
Join Date: May 2006
Location: Helsingborg, , Sweden.
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

Reply With Quote
  #3 (permalink)  
Old August 22nd, 2006, 09:19 AM
Registered User
 
Join Date: Jun 2006
Location: , , .
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Peso,

I need to dynamically pass values to @createdate.

Please reply.

Thanks.

Reply With Quote
  #4 (permalink)  
Old August 22nd, 2006, 12:10 PM
Authorized User
 
Join Date: Jun 2003
Location: , , Norway.
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

Reply With Quote
  #5 (permalink)  
Old August 22nd, 2006, 11:11 PM
Registered User
 
Join Date: Jun 2006
Location: , , .
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.

Reply With Quote
  #6 (permalink)  
Old August 24th, 2006, 06:15 AM
Authorized User
 
Join Date: Jun 2003
Location: , , Norway.
Posts: 95
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi.

Try to use the format YYYYMMDD instead.

Gert

Reply With Quote
  #7 (permalink)  
Old August 24th, 2006, 06:48 AM
Friend of Wrox
 
Join Date: May 2006
Location: Helsingborg, , Sweden.
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!

Reply With Quote
  #8 (permalink)  
Old August 24th, 2006, 04:16 PM
Registered User
 
Join Date: Jun 2006
Location: , , .
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.

Reply With Quote
  #9 (permalink)  
Old August 25th, 2006, 01:20 AM
Friend of Wrox
 
Join Date: May 2006
Location: Helsingborg, , Sweden.
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
Reply With Quote
  #10 (permalink)  
Old August 25th, 2006, 01:46 AM
Friend of Wrox
 
Join Date: May 2006
Location: Helsingborg, , Sweden.
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


Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

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 11: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 08: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 04:03 AM



All times are GMT -4. The time now is 12:34 PM.


Powered by vBulletin®
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.