Solved

SQL 2005 stored procedure wont acccept UK date format

Posted on 2014-01-16
9
960 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
[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
  • 5
  • 4
9 Comments
 
LVL 44

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 44

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 eBook: Backup on AWS

Everything you need to know about backup and disaster recovery with AWS, for FREE!

 

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 44

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 44

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

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

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

In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

730 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