Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i am a newbie and a student i have a windows form in which is have a datadrid view. i have table in sql server, i have binded with the datagrid, in the table there is a 3 column and in which one column named date of small-date-time datatype, i just binded the datagrid view to the table and it is showing all the rows of the three column i want to filter that. i have three column one is "name" second is "surname" and third is "date" date contains smalldatetime datatype and i want to show all the three columns but the columns which has the "date" columns as 30/12/12 if the date is 31/12/12 means the record from the previous date sorry for the bad explanation i am weak in english i dont know what to use here can i achieve this?
Posted
Updated 31-Dec-12 2:27am
v2
Comments
sariqkhan 31-Dec-12 8:23am    
bro i have commented on my freinds and called them... they will help u soon. i am also searching

1 solution

Try:
SQL
SELECT * FROM myTable WHERE CONVERT(date, [date])=CONVERT(date, DATEADD(d, -1, GETDATE()))
 
Share this answer
 
Comments
shaikh-adil 31-Dec-12 8:36am    
sir where i have to write this
OriginalGriff 31-Dec-12 8:48am    
Exactly where you have to write it depends on how you are loading your DataGridView. But if you find the Select command you are currently using (it will start with "SELECT" and contain your table name) and replace the text with the above - remembering to change the table name - it should work.
Look at where you create your DataAdapter if you are using one.
shaikh-adil 31-Dec-12 8:59am    
thank you very much sir thanks a lot
can you explain what it is?
CONVERT(date, [date])=CONVERT(date, DATEADD(d, -1, GETDATE()))
how it works
OriginalGriff 31-Dec-12 9:16am    
You're welcome!
CONVERT is an SQL function that changes one datatype to another. In this case, it takes a DateTime and convertes it to a Date - the difference being that Date has no time element, and DateTime (obviously) does.
DATEADD is an SQL function that adds a specified amount of time to an existing DateTime. In this case, we specify "d" for "days" and the number as minus one, meaning that it returns the date yesterday.
You need to CONVERT on each side to discard the time element - both from the current date and teh version you are checking against. Otherwise, it would compare the time stored in the database against the time now, and not return records entered 25 hours ago, but still after midnight yesterday.
So if the Database record is '2012-12-30 06:45' and the current date is '2012-12-31 14:13'
CONVERT(date, [date])
returns '2012-12-30' having discarded the time part
DATEADD(d, -1, GETDATE())
returns '2012-12-30 14:13' having taken one day from the current
and
CONVERT(date, DATEADD(d, -1, GETDATE()))
returns '2012-12-30' having discarded the time part of that.
The comparison then matches, and the record is returned.
shaikh-adil 31-Dec-12 9:29am    
current date is '2012-12-31 14:13'
CONVERT(date, [date]) returns '2012-12-31' having discarded the time part
i think this must be written in the explanation right?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900