Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Strange date behavior in view

Posted on 2008-06-09
4
Medium Priority
?
236 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
[X]
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
  • 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

Enroll in October's Free Course of the Month

Do you work with and analyze data? Enroll in October's Course of the Month for 7+ hours of SQL training, allowing you to quickly and efficiently store or retrieve data. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
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…
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…
Suggested Courses

636 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