[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Strange date behavior in view

Posted on 2008-06-09
4
Medium Priority
?
241 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 2000 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

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.
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 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…
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…

872 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