Solved

Strange date behavior in view

Posted on 2008-06-09
4
180 Views
Last Modified: 2010-03-19
Hi Experts,

I have a view where I pass in a where clause requesting all records on a particular date and for some records it won't retrieve them.
The field in the table is datetime and in the view i use datepart to get just the date.  In my vb app I use a date variable for the requested date.  
"Select * from vSchedules where scheddate = '" & dDate & "'" _
   & " ORDER BY ColumnNumb, starttime"
The requested records are in the view, but I did notice the date format is different coming from vb to sql server.  VB sends the date as 6/9/2008 whereas sql server has the date as 06/09/2008.  
What's weird is that some records will be pulled, allthough they have the same format, but others won't.
Any ideas on what's going on and what I should do to resolved this?
0
Comment
Question by:Sheritlw
  • 2
  • 2
4 Comments
 
LVL 22

Expert Comment

by:dportas
ID: 21746735
>> I use a date variable for the requested date. "Select * from vSchedules where scheddate = '" & dDate & "'"

Never concatenate SQL strings in that way. The reliable and safe way to pass parameters is to use a parameters collection with parameters of the correct types.

>> sql server has the date as 06/09/2008

SQL Server does not have a "format" for DATETIME columns. Assuming this is a DATETIME or SMALLDATETIME column you should can pass in the variables of the correct type and not have to worry about any formatting. Be aware however that SQL Server 2005 always stores both date and time in a DATETIME column. If you test only for equality then you will only get a result based on a single point in time - the default is midnight if you don't specify a time.
0
 

Author Comment

by:Sheritlw
ID: 21746778
The variable I am using dDate is a date variable and my datepart funtion is
ALTER FUNCTION [dbo].[DatePart]
  ( @fDate datetime )
RETURNS varchar(10)
AS
BEGIN
  RETURN ( CONVERT(varchar(10),@fDate,101) )
END
Normally I would use a stored procedure but with this particular view, there may be other filters assigned and it's easier for me to create the where string in code.
0
 
LVL 22

Accepted Solution

by:
dportas earned 500 total points
ID: 21746859
I don't understand what that function has to do with your original question.
DatePart is a reserved word and a SQL Server function. Don't use it for a user-defined function name.
0
 

Author Comment

by:Sheritlw
ID: 21746894
Thank you for your help.
I figured out what was going on.  In the view I was sending in a date and comparing it to another date using datepart, returning just the date.  When I changed it not to return just the date it worked perfectly.  Thank you for getting me thinking.
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

757 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

19 Experts available now in Live!

Get 1:1 Help Now