Solved

Date conversion error in SQL Server stored procedure

Posted on 2012-04-03
7
268 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 150 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
LVL 15

Accepted Solution

by:
gplana earned 150 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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, …
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
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: …

724 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