VB.net Check if certain Date has been entered in a DateTime SQL field

Hi

In my VB.net project I need to check whether an entry has been made already that has a certain date.
The DateTime field in my database called DateTimeField1 is not a "Date" field. Will the following
SQL statement be OK or do I need to use Cast or Convert or something like that.

 SELECT Count(*) As MyCount FROM Table1 WHERE  DateTimeField1 = '25 January 2015'
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Scott PletcherSenior DBACommented:
If "DateTimeField1" is not a date or datetime, but char/varchar instead, then your literal will need to match the format of the column.  You don't need to convert them to datetime to compare them as long as the strings in the table are consistent.
0
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAuthor Commented:
but it is datetime
0
Ryan ChongCommented:
what Scott means is at the database end, you NO need to do any field conversion for comparison. But in your vb.net interface, you would need to convert your date value into the same string format to your field which need to be compared in the database.

so, probably you can tell us:

1. what's the format of the field in database for comparison? how's the value looks like?
2. which control was used to keep the date value in your vb.net project? how's the value looks like?
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAuthor Commented:
1. The format of the database field is date time with values that look like "2015/10/16 12:00:00 AM"
2. There is a calendar control called Calendar1 that holds the date value in my VB.net project
0
Ryan ChongCommented:
1. The format of the database field is date time with values that look like "2015/10/16 12:00:00 AM"
but you had mentioned:

>>The DateTime field in my database called DateTimeField1 is not a "Date" field.

so, can we reconfirmed the data field : "DateTimeField1" is a date or Not a date data field?

>>2. There is a calendar control called Calendar1 that holds the date value in my VB.net project
This depends on first question on how we later determine how we should format this calendar control's value.
0
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAuthor Commented:
OK I see the confusion here. I shouldn't have worded it like that because a datetime field essentially is a date type. So essentially DateTimeField is a date type, specifically datetime. It was the time contingent of this that worried me because "2015/10/16 12:00:00 AM" does not equal "2015/10/16". Or does it?
0
Ryan ChongCommented:
>>It was the time contingent of this that worried me because "2015/10/16 12:00:00 AM" does not equal "2015/10/16". Or does it?

both should be the same, I guess you're using SQL Management Studio and it was how the date values were being displayed.

hence you mentioned DateTimeField1 is a date data type, then you probably can try:

SELECT Count(*) As MyCount FROM Table1 WHERE  DateTimeField1 = '25 January 2015'

yes, this query should managed to get results from Table1 with DateTimeField1 = 25 Jan 2015.

let us know if you need helps in coding part on how to make the query.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Scott PletcherSenior DBACommented:
If it's a date or datetime, then you do not want to cast the column at all.  Simply specify the date literal appropriately.

With the correct date & language settings, you can use '25 January 2015' directly, I think.

But format 'YYYYMMDD' always work in SQL Server.

So, to list everything for the entire day of Jan 25, do this:

SELECT Count(*) As MyCount FROM Table1 WHERE  DateTimeField1 >= '20150125' AND DateTimeField1 < '20150126'
0
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAuthor Commented:
Thank you both very much and sorry again about the confusion
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.