?
Solved

Date conversion error in SQL Server stored procedure

Posted on 2012-04-03
7
Medium Priority
?
270 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

 
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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Audit has been really one of the more interesting, most useful, yet difficult to maintain topics in the history of SQL Server. In earlier versions of SQL people had very few options for auditing in SQL Server. It typically meant using SQL Trace …
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

771 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