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
Solved

Strange date behavior in view

Posted on 2008-06-09
4
207 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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
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…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

790 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