Solved

SQL 2005 stored procedure wont acccept UK date format

Posted on 2014-01-16
9
927 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 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
 

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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 

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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

758 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now