?
Solved

Date conversion error in SQL Server stored procedure

Posted on 2012-04-03
7
Medium Priority
?
277 Views
Last Modified: 2012-06-21
I have a stored procedure that is throwing the following error when I run it "Msg 241, Level 16, State 1, Procedure sp_Matl_OpenPO_Amt_Adhoc, Line 54
Conversion failed when converting date and/or time from character string." I am using '2012001' for @begdate value and '20120131' for @enddate value when running the stored procedure.

I can't figure out why it is giving me this error.  If I pull out the section of the stored procedure that includes the date reference and run it as a query it runs fine.  Can someone take a look at my procedure and see if anything stands out to you?  

I am using SQL Server 2008.
matlOpenstoredprocedure.sql
0
Comment
Question by:imstac73
  • 4
  • 2
7 Comments
 
LVL 25

Expert Comment

by:Lee Savidge
ID: 37802330
2012001 isn't a valid date. I assume this is a copy and paste error?
0
 

Author Comment

by:imstac73
ID: 37802335
Yes sorry.  It should be '20120101'
0
 
LVL 25

Assisted Solution

by:Lee Savidge
Lee Savidge earned 450 total points
ID: 37802387
Instead of declaring them as date types, declare them as nvarchar(8) and then cast them as a date in the query.

So...

 not between cast(@begdate as date) and cast(@enddate as date)
0
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
LVL 15

Accepted Solution

by:
gplana earned 450 total points
ID: 37802444
I think you should use "convert" sqlserver function for converting a varchar to datetime or viceversa. Please see this link:
http://msdn.microsoft.com/en-us/library/ms187928.aspx
0
 

Author Comment

by:imstac73
ID: 37802617
Do I need to change my conversion on my field?

replace(CONVERT(date,CONVERT(char,ph.IDTRV),112),'-','')
0
 

Author Comment

by:imstac73
ID: 37802628
gplana...are you referring to adding a convert to the parameters?
0
 

Author Comment

by:imstac73
ID: 37802666
I got it working.  What I did was made the parameters data type decimal(8,0) then I used the following conversion in my case statement
CONVERT(char, @begdate, 112) and CONVERT(char, @enddate, 112).

Thanks for your help.
0

Featured Post

 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Long way back, we had to take help from third party tools in order to encrypt and decrypt data.  Gradually Microsoft understood the need for this feature and started to implement it by building functionality into SQL Server. Finally, with SQL 2008, …
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question