Solved

Strange date behavior in view

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
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.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

863 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

21 Experts available now in Live!

Get 1:1 Help Now