?
Solved

SQL 2005 stored procedure wont acccept UK date format

Posted on 2014-01-16
9
Medium Priority
?
984 Views
Last Modified: 2014-01-21
I have ASP.NET 2010 code which executes a stored procedure on SQL 2005.  It passes some parameters to this procedure including two dates.  In this code, I’m using the following CDate functions for the dates.

tempWorkflowStartDate = CDate(txtWorkflowStartDate.Text).ToString("yyyy-MM-dd")
tempSalesEffectiveFrom = CDate(txtSaleseffectiveFrom.Text).ToString("yyyy-MM-dd")

The output from the ASP.NET code can be run in a query window on SQL:

EXEC spSubmitNPRRequest 'Test', 'test.test@test.com', 'brief description', '14/01/2014', '31/01/2014'

The date format is and should be British.  I’ve used a calander extender which accepts the British date format and displays it that way too.  

When I execute the query, I get the following error: “Msg 8114, Level 16, State 5, Procedure spSubmitNPRRequest, Line 0 Error converting data type varchar to datetime.”

If I change the date formats manually on the query to look like US format it executes and works fine like so:

EXEC spSubmitNPRRequest 'Test', 'test.test@test.com', 'brief description', '01/14/2014', '01/31/2014'

When viewing the data table, the formats are back to how I expect (UK format).  '14/01/2014', '31/01/2014'

Does anyone know how I can get the stored procedure to accept the UK date formats?  I’ve tried Google and a lot of obvious formatting to get it to work but I’m not having any success.

Thanks
0
Comment
Question by:michaelhenderson
  • 5
  • 4
9 Comments
 
LVL 45

Expert Comment

by:AndyAinscow
ID: 39784812
From the question:
CDate(txtWorkflowStartDate.Text).ToString("yyyy-MM-dd")
and
EXEC spSubmitNPRRequest 'Test', 'test.test@test.com', 'brief description', '14/01/2014', '31/01/2014'



Erm - the date strings are NOT in the format you specify, where do you get them from?  Does it work if you pass in the date string in the format you want ?
eg.
EXEC spSubmitNPRRequest 'Test', 'test.test@test.com', 'brief description', '2014-01-14', '2014-01-31'
0
 

Author Comment

by:michaelhenderson
ID: 39784835
Hi,

Thanks for replying.  

The strings coming from "CDate(txtWorkflowStartDate.Text).ToString("yyyy-MM-dd")" come out as UK format (i.e. 31/01/2014).  

When I execute this date against the stored procedure, it doesn't work as it wants it in 01/31/2014 format.  I can manually change the string to 01/31/2014 on the EXEC statements and it works.  It even goes back to UK format when you look at the table afterwards.
0
 
LVL 45

Expert Comment

by:AndyAinscow
ID: 39784848
>>The strings coming from "CDate(txtWorkflowStartDate.Text).ToString("yyyy-MM-dd")" come out as UK format (i.e. 31/01/2014).  


?  According to the format you specify that should return a string such as 2014-01-31

Where do you perform that conversion?
How do you test to see the result?
0
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 

Author Comment

by:michaelhenderson
ID: 39784883
It's done at ASP.NET 2010 level.  If I use CDate(txtWorkflowStartDate.Text).ToString("dd-MM-yyyy") I just get an error message.  

The code I've used has worked perfectly in the past on ASP.NET 2005 on SQL 2005 but 2010 doesn't seem to work this way.
0
 

Author Comment

by:michaelhenderson
ID: 39785351
I'm going around the houses looking at Google.  Lots of people seem to have similar issues but none of the solutions work for me.  One guy even wrote a function to chop up the string and re-arrange it in the right place but that just gave errors!

This code CDate(txtWorkflowStartDate.Text).ToString("dd-MM-yyyy") gives me a parameter value of dd/mm/yyyy which the stored proceudre won't accept.

Is there a way in ASP.NET to get convert txtWorkflowStartDate.Text code to a format of yyyyMMdd?  The stored procedure accepts that format and it appears in the database in the correct dd/mm/yyyy format.

I've tried the obvious CDate(txtWorkflowStartDate.Text).ToString("yyyyMMdd")  including variations on dashes and back slashes but I just get invalid cast exception errors.

I just want the stored procedure to read a UK date 31/01/2014 as a valid date OR get ASP.NET to convert my valid UK date of 31/01/2014 into 20140131 as the stored procedure likes this format.
0
 
LVL 45

Expert Comment

by:AndyAinscow
ID: 39785498
I've just done a quick project with a button.
ASP.net with .net 4 and using Visual Studio 2010

ToString
There one clearly sees the string is as one expects.  I don't understand why you get something different as the formatted string.
0
 

Accepted Solution

by:
michaelhenderson earned 0 total points
ID: 39785640
Thanks for your ideas guys.

I have the solution eventually.  The ASP.NET 2010 code is fine.  My stored procedure was the issue.

I was declaring my date variables as DATETIME.  This doesn't seem to work so I set them as VARCHAR(20)'s instead.

e.g.

ALTER PROCEDURE [dbo].[spSubmitNPRRequest]

/*** Set Parameters ***/
@RequestedByUserAlias NVARCHAR(50),
@RequestedByUserEmailAddress NVARCHAR(100),
@BriefDescription NVARCHAR(MAX),
etc...
@SalesEffectiveFrom VARCHAR(20),
@WorkflowStartDate VARCHAR(20)

Then on my INSERT statement, I had to change the date values from @SalesEffectiveFrom and @WorkflowStartDate to (convert(datetime,@SalesEffectiveFrom ,103)) and (convert(datetime,@WorkflowStartDate,103)).

Basically the SP wanted the parameters as VARCHARS and then I had to do the convertion within the SP 'SQL insert statement'.  I spent a lot of time changing my code on the ASP.NET solution but that was fine!

Thanks
0
 
LVL 45

Expert Comment

by:AndyAinscow
ID: 39785685
>>The ASP.NET 2010 code is fine

I would definately disagree with that.

You perform a ToString conversion on a date and get a totally incorrect result back.  A result which I can't duplicate.  This makes me think you have a serious problem somewhere, maybe it will bite you again later.
0
 

Author Closing Comment

by:michaelhenderson
ID: 39796409
It worked although I don't see why a stored procedure would not read in a valid date and allow the parameter to declared as a DATETIME.  It makes little sense to declare it as a VARCHAR.
0

Featured Post

Configuration Guide and Best Practices

Read the guide to learn how to orchestrate Data ONTAP, create application-consistent backups and enable fast recovery from NetApp storage snapshots. Version 9.5 also contains performance and scalability enhancements to meet the needs of the largest enterprise environments.

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…

862 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