0

I have a SQL Server table Registration with name and datetime, I want to search the data with datetime which is less then datetime.now.

SqlDataAdapter cs = 
    new SqlDataAdapter("Select * from Registration where Date > DateTime.now")

I want that datetime is less then today.

4

5 Answers 5

4

The T-SQL built-in function getdate() returns the current server date/time.

Therefore your query should be, SELECT * FROM Registration WHERE [Date] < getdate().

Using this in your code would be:

SqlDataAdapter cs = new SqlDataAdapter("Select * from Registration where [Date] < getdate()")
Sign up to request clarification or add additional context in comments.

5 Comments

the question say less then datetime.now
@IswantoSan Yeah, bad copy-paste. Thanks
But Dear Table Columen DataType is DateTime not only date??
@SaimaMaheen getdate() contains the date and time. Look at the linked documentation.
Dear But if i want manual enter date in textbox or selection date from monthcalender1 what is code?
3

You can use SQL GETDATE() to do this:

SqlDataAdapter cs = new SqlDataAdapter("Select * from Registration where Date < GETDATE()")

2 Comments

But Dear Table Columen DataType is DateTime not only date??
Dear But if i want manual enter date in textbox or selection date from monthcalender1 what is code?
2

Use GETDATE() to get current date in your query.

SqlDataAdapter cs = new SqlDataAdapter("Select * from Registration where Date < GETDATE()");

1 Comment

But Dear Table Columen DataType is DateTime not only date??
1

This may work... if you want to use .NET DateTime, that will be the date of the local system.

     SqlDataAdapter cs = new SqlDataAdapter(
           "SELECT * FROM [Registration] 
            WHERE [Date] < '" + DateTime.Now.ToShortDateString() +"';");

     //output: SELECT * FROM [Registration] WHERE [Date] < '2/22/2013';

It is going to depend upon the data type of the [date] column, if it is string or datetime or something else. If it is YYYYmmdd, then you can manipluate the DateTime.Now output to match.

2 Comments

Dear But if i want manual enter date in textbox or selection date from monthcalender1 what is code?
if using a text box, collect and DateTime.TryParse() the entered text. if you are using a calendar control, it should have a means to export the date in whatever format you need it in. Place the data-values in the string in place of 'DateTime.Now.ToShortDateString()'
1

Its very simple..

if you want to use sql current datetime use SELECT * FROM Registration WHERE [Date] < getdate()

and if you want your application current datetime use

"SELECT * FROM Registration WHERE [Date] < '+Datetime.Now.Tostring() +'"

Also try to use parameterized query if you want second option.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.