473,503 Members | 1,953 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

SQL query in .NET with DATETIME issues

27 New Member
I'm working in vb.net. I'm trying to create a query for a table adapter that will filter data. The query is
Expand|Select|Wrap|Line Numbers
  1. SELECT CommitteeId, CommitteePostion FROM MemberIndexApp WHERE ((UserName = @UserName) AND (LoginDate = @LoginDate))
The table lies in a SQL Server and the LoginDate's type is DATETIME.
Right now, the query is returning nothing. I'm fairly certain the problem lies in the DATETIME format surrounding the LoginDate. The parameter @LoginDate starts out as a string, and I've tried converting it to datetime with CONVERT(DATETIME, @LoginDate, 102) and CONVERT(DATETIME, @LoginDate). I've also tried other formats beside 102, but nothing seems to work.

How can I get LoginDate and @LoginDate into the same format? The format must contain the date and the time (including seconds).

Thanks in advance.
Jul 10 '08 #1
10 4090
Plater
7,872 Recognized Expert Expert
In your current situation, if your datetime variable is in the string format, surrounding it with ' will implictly cast it as a datetime

Expand|Select|Wrap|Line Numbers
  1. SELECT CommitteeId, CommitteePostion FROM MemberIndexApp WHERE ((UserName = @UserName) AND (LoginDate = '@LoginDate'))
  2.  
Another alternative would be to use the parameter objects and set the type to DateTime then give it a .NET DateTime object.
Jul 10 '08 #2
shweta123
692 Recognized Expert Contributor
Hi,

To remove the error you have to convert both the dates into same format . You can do this way :
In your vb .net code format LoginDate value as following :

e.g.
Expand|Select|Wrap|Line Numbers
  1.   LoginDate = Format(LoginDate,"MM/dd/yyyy")
You have to make change into sql query as follows :

SELECT CommitteeId, CommitteePostion FROM MemberIndexApp WHERE ((UserName = @UserName) AND
(Convert(varchar,LoginDate,101) = @LoginDate))

Hope this helps.
Jul 10 '08 #3
SenileOwl
27 New Member
Thank you for your replies.

Unfortunately, I keep getting the same thing. I tried to convert to varchar, but the table was still blank. I tried the format, but it wasn't' "a recognized function." I also tried to put the @LoginDate in '' but it quit recognizing it as a parameter. I also tried changing the parameter type to DateTime, but I still received a blank table.



I tried < and >in place if the = just to see what it would happen.

Expand|Select|Wrap|Line Numbers
  1. SELECT CommitteeId, CommitteePostion, LoginDate, MemberId, Note, TermEnd, TermStart, TypeOfMember, UserName, WhoAppointed FROM MemberIndexApp WHERE ((UserName = @UserName) AND (LoginDate  >  @LoginDate))
The program believes the parameter is smaller than the column field. Which implies that the data in the table somehow has extra data. Doesn't it? Either seconds, where the parameter has none or else 0's added on. I'm nearly positive the parameter has seconds. At least it has them when I right before it gets sent to the SQL statement.

I think I might have to create just make sure the column is between the parameter and the parameter plus one second. However, I really don't want to do this, unless I have to. Is there anyway to get the program to recognize they have the same data?
Jul 10 '08 #4
Plater
7,872 Recognized Expert Expert
All DateTimes have seconds, whether they show up or not (they could just always be set to zero) depends on the printing format.
I am not sure what you are doing differently then the rest of us?
Expand|Select|Wrap|Line Numbers
  1. //where 'sc' is my SqlConnection object
  2. //and 'SDate' is an instance of DateTime
  3. SqlCommand com = new SqlCommand("SELECT * FROM myTable WHERE myDateColumn >= @StartDate", sc);
  4. com.Parameters.Add("@StartDate", SqlDbType.DateTime);
  5. com.Parameters["@StartDate"].Value = SDate;
  6.  
Jul 10 '08 #5
SenileOwl
27 New Member
Alas, I know what I'm doing different than everyone else, but I'm not sure why that is affecting everything. I'm also not willing to change my ways yet.

You see, I learned to program from a "Teach yourself VB.NET programming" book. Unfortunately the book taught me that to connect to the database, I had to let .NET do it for me. So unlike the rest of the true programmers out there, I don't know how to create my connection in code. Although I plan to rectify this soon, I'm not ready yet to change my program this late in the game. Thus, my SQL select statement is not in my code. It is in a query I'm adding to my table adapter using the add query wizard in the dataset designer page.

So having gotten that embarrassing confession off my chest. There are a few things that I can see different. 1. in your example you were using a greater than or equal to. I'm just using an equal to. If I use a greater than or equal to I can capture the data I want, but I'm also grabbing some extra, that I don't want. 2. I set the DBtype property of my parameter to DateTime. It doesn't say SQLdbtype like yours did. Maybe this has something to do with it.

I have played with my code and I added a second on to my parameter LoginDate. I then selected only the rows where the LoginDate was between the old Parameter and the new Parameter (+ second) This seems to work just fine. If I can't solve the problem, I know I can at least make my code work. Yeah!
Jul 10 '08 #6
Plater
7,872 Recognized Expert Expert
You see, I learned to program from a "Teach yourself VB.NET programming" book. Unfortunately the book taught me that to connect to the database, I had to let .NET do it for me.
Ick, yet another reason I dislike VB, everything is geared around drag/drop, no focus on how it works behind the scenes.
Jul 10 '08 #7
shweta123
692 Recognized Expert Contributor
Hi,

If you dont want to consider time part of the date while comparing the 2 dates
you can use format 101 or 103 as these 2 formats are standard and also they do not have time in their format.

Also ,
If you are not getting records by query through vb .Net code then one way is that you goto query Analyzer and execute the same query there. It is possible that Sql statement is right and there is some other error in the code.
Jul 11 '08 #8
SenileOwl
27 New Member
Alas I don't have access to the SQL Server to use the Query Analyzer to check my query language (I assume that was the Query Analyzer you were referring to) The closest I can come to it, is calling the tables into Access and then querying it there. Unfortunately I can not use the same functions (i.e.CONVERT) as I've used in vb.net. Is this normal? Have I missed something really important?

As for using 101 and 103. I've tried them both, but maybe I've used them incorrectly. I tried using
Expand|Select|Wrap|Line Numbers
  1. (LoginDate =  CONVERT(DATETIME, @LoginDate, 103))
and
Expand|Select|Wrap|Line Numbers
  1. (CONVERT(DATETIME, LoginDate, 101) =  CONVERT(DATETIME, @LoginDate, 101)))
both give me an empty table. However I can get
Expand|Select|Wrap|Line Numbers
  1. (LoginDate > @LoginDate) AND (LoginDate <  @LoginDate2)
to work, where the second login date is one second more than the first.

I need to know the time for my program. But I'm curious to see if I can get it to work with just a date. So far nothing has worked.
Jul 11 '08 #9
shweta123
692 Recognized Expert Contributor
Hi,

Are you using SQL Server for database ?
Please read this Article
for datetime handling with sql.
Jul 13 '08 #10
SenileOwl
27 New Member
yes I am using SQL. I've now decided to bag it. I cannot find the cause of the difference. So I'm using the plus one second and minus one second. Seems to work great and I'll try to solve the other problem later.
Jul 23 '08 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

4
5763
by: Russell | last post by:
I'm having a fit with a query for a range of dates. The dates are being returned from a view. The table/field that they are being selected from stores them as varchar and that same field also...
3
9768
by: Philip Yale | last post by:
A colleague of mine has a query which fails to run under SQLAgent batch with the following error: The conversion of a char data type to a datetime data type resulted in an out-of-range datetime...
4
1864
by: sah | last post by:
I need some help with the following query: DECLARE @SRV VARCHAR(20), @date smalldatetime SET @SRV = (select @@servername) SET @date = '20040901' select Srv_Name = @SRV, DB_Name = 'DB_NAME',...
3
1728
by: Myron | last post by:
I'm trying to create a query that will tell me which requests took longer than 10 days to move one from particular state to another state. The query I've created returns the correct requests, but...
2
2367
by: Ray Holtz | last post by:
I have a form that shows a single record based on a query criteria. When I click a button it is set to use an append query to copy that record to a separate table, then deletes the record from the...
9
4899
by: Phil B | last post by:
I am having a problem with a datetime from a web services provider The provider is sending the following SOAP response <?xml version="1.0" encoding="utf-8"?> <soap:Envelope...
1
1450
by: dm1608 | last post by:
Hi all -- I'm rapidly learning ASP.NET 2.0 (never really did much .NET before... mainly an ASP and PHP guy until now) and seem to be having some technical challenges that I'm hoping someone can...
3
6625
by: =?Utf-8?B?c2lwcHl1Y29ubg==?= | last post by:
Hi I am getting an Exception and trying to fix as MS suggest From C# help this comes If I try
3
2005
by: JCCDEVEL | last post by:
Hello, I'm writing a basic query in Sql Server Mgmt Studio 2005. Basically, I'm trying to query a table to get all "issues" reported in a month by "Project Category". The query is working fine...
0
7205
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7093
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7287
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7348
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7006
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7467
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5592
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5021
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3175
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.