By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,991 Members | 1,888 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,991 IT Pros & Developers. It's quick & easy.

DateTime in SQL

100+
P: 102
I have a field in sqlserver that is defined as datetime. It contains a date & time value, ex: 2016-06-20 08:30:00.000. In a vb program I need to filter this field as date only and put into a data grid view. How do I accomplish this?

Thanks in advance.
Jun 21 '16 #1
Share this Question
Share on Google+
1 Reply


100+
P: 299
I actually got done doing this exact request for my own personal use. This is partially dependent on how you're reading data, but please see below for how I do it:

Expand|Select|Wrap|Line Numbers
  1. Dim lrd As SqlDataReader = cmd.ExecuteReader()
  2.  
  3. While lrd.Read()
  4.   DataGridView1.Rows.Add(i, lrd(1), lrd(2).ToString().Substring(0, lrd(2).ToString().IndexOf(" ")))
  5.   i = i + 1
  6. End While
  7.  
The piece of code that's extracting the date is this:
Expand|Select|Wrap|Line Numbers
  1. lrd(2).ToString().Substring(0, lrd(2).ToString().IndexOf(" ")))
-lrd is my SqlDataReader
-2, is the 3rd result from my select statement
-ToString() converts the value to a string to be able to format
-Substring allows us to pull a partial string
-0 tells the program to start at the beginning index of the string
-lrd(2).ToString().IndexOf(" ") looks for the first space within the string. In our case, it's the space between the date and the time

Hope this helps!
Jun 21 '16 #2

Post your reply

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