• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1002
  • Last Modified:

SQL 2005 stored procedure wont acccept UK date format

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
michaelhenderson
Asked:
michaelhenderson
  • 5
  • 4
1 Solution
 
AndyAinscowFreelance programmer / ConsultantCommented:
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
 
michaelhendersonAuthor Commented:
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
 
AndyAinscowFreelance programmer / ConsultantCommented:
>>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 Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

 
michaelhendersonAuthor Commented:
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
 
michaelhendersonAuthor Commented:
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
 
AndyAinscowFreelance programmer / ConsultantCommented:
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
 
michaelhendersonAuthor Commented:
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
 
AndyAinscowFreelance programmer / ConsultantCommented:
>>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
 
michaelhendersonAuthor Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now