dbTalk Databases Forums  

MSSQL implicit conversion problem

comp.databases.ms-sqlserver comp.databases.ms-sqlserver


Discuss MSSQL implicit conversion problem in the comp.databases.ms-sqlserver forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
sahinceylan
 
Posts: n/a

Default MSSQL implicit conversion problem - 07-14-2010 , 04:35 PM






Hi,

I have a stored procedure which accept a datetime input parameter.

spMyProcedure '2010-07-14 13:25:48'

Above statement works fine in my development environment. But in the
server it gives 'varchar to datetime conversion error'. It treats '14'
as month and '07' as day.

I can resolve this issue by changing the input parameters type to
nvrachar and explicitly convert to date time
or by adding 'SET DATEFORMAT ymd' statement.

But i have a lot of stored procedures like this and i don't want to
change all of them.

Can you suggest any other solution.

Thanks.

Reply With Quote
  #2  
Old   
Erland Sommarskog
 
Posts: n/a

Default Re: MSSQL implicit conversion problem - 07-14-2010 , 04:49 PM






sahinceylan (sahinceylan2001 (AT) gmail (DOT) com) writes:
Quote:
I have a stored procedure which accept a datetime input parameter.

spMyProcedure '2010-07-14 13:25:48'

Above statement works fine in my development environment. But in the
server it gives 'varchar to datetime conversion error'. It treats '14'
as month and '07' as day.

I can resolve this issue by changing the input parameters type to
nvrachar and explicitly convert to date time
or by adding 'SET DATEFORMAT ymd' statement.

But i have a lot of stored procedures like this and i don't want to
change all of them.

Can you suggest any other solution.
If you use date literals in SQL Server, you should use YYYYMMDD or
YYYY-MM-DDThh:mm:ss, where T stands for itself. These format are
safe, that is they don't depend on dateformat setting.

If these are calls from a client, you are calling your stored procedures
in the wrong way. You should *not* send EXEC statements, you should use
RPC. There are two reasons for this:

1) When you build EXEC strings, and interleave user input, you open
yourself for SQL injection.

2) You get the mess with datetime parameters that you have just run
into.

Since you don't say which client API you are using, I can't say how
you should do to implement RPC calls, but all client API I know,
supports RPC.



--
Erland Sommarskog, SQL Server MVP, esquel (AT) sommarskog (DOT) se

Links for SQL Server Books Online:
SQL 2008: http://msdn.microsoft.com/en-us/sqlserver/cc514207.aspx
SQL 2005: http://msdn.microsoft.com/en-us/sqlserver/bb895970.aspx
SQL 2000: http://www.microsoft.com/sql/prodinf...ons/books.mspx

Reply With Quote
  #3  
Old   
sahinceylan
 
Posts: n/a

Default Re: MSSQL implicit conversion problem - 07-15-2010 , 11:46 AM



Thanks;
I used YYYY-MM-DDThh:mm:ss format, and everything works fine.

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.