-1

I have table xxx (,,,,,); among these one column has DateTime datatype (say Login_date)

Login_date has values like: " 27-Nov-13 3:12:07 PM "

My Problem is that

i want to select the rows of table xxx where Login_date is " 27-Nov-13 " i am getting error when executing a query like this

DateTime dt=new DateTime();
dt=DateTime.Now;
Command Query is:      " SELECT * FROM xxx WHERE (Login_Date=@date)  "
cmd.Parameters.add("@date",dt);

This is not selecting any rows from table xxx.

This is not working because of difference in Time.

& here i am using MYSQL data base Please bring me out of this problem.

Clockwork-Muse
  • 12,806
  • 6
  • 31
  • 45
Rahul Uttarkar
  • 3,367
  • 3
  • 35
  • 40

11 Answers11

1

pls try this,

 var dt=DateTime.Now.ToString("dd-MMM-yy");
Ankit
  • 207
  • 1
  • 11
0

Try this:

dt.ToString("dd-MMM-yy")

So when adding your parameters:

cmd.Parameters.add("@date",dt.ToString("dd-MMM-yy"));
dav_i
  • 27,509
  • 17
  • 104
  • 136
0

Try where convert(datetime, cast(login_date as varchar(12)),103)='27-Nov-13'

I always find the following page on DateTime an excellent resource: sql server datetime workbench

Christian Phillips
  • 18,399
  • 8
  • 53
  • 82
Lima
  • 1,203
  • 3
  • 22
  • 51
0

Try this:

DateTime dt = DateTime.Now;

var cmd = new SqlCommand("SELECT * FROM xxx WHERE (DATEADD(dd, 0, DATEDIFF(dd, 0, Login_Date)) = DATEADD(dd, 0, DATEDIFF(dd, 0, @date)))");
cmd.Parameters.add("@date", dt);

this way we are comparing only date without time.

gzaxx
  • 17,312
  • 2
  • 36
  • 54
  • er, doing this kind of date math will cause the optimizer to not use any index on that column, meaning the query will take longer than it should. – Clockwork-Muse Nov 28 '13 at 11:09
0

Try this:

DateTime dt=DateTime.Today; // Note Today, not Now
Command Query is:      
"SELECT * FROM xxx WHERE Login_Date>=@date AND Login_Date < DATEADD(d,1,@date)"
cmd.Parameters.add("@date",dt);

Using a date range in your query like this can be more efficient than casting to DATE because it enables you to take advantage of any index there may be on the DATETIME column.

Joe
  • 122,218
  • 32
  • 205
  • 338
0

try below query,

SELECT * FROM xxx WHERE (convert(date,Login_Date,101))=convert(date,@date,101)

RobertKing
  • 1,853
  • 8
  • 30
  • 54
0

This should work...

CREATE TABLE #tabledates ( dt DATETIME )

INSERT  INTO #tabledates
        ( dt )
VALUES  ( '2013-11-27 10:30:07' )

INSERT  INTO #tabledates
        ( dt )
VALUES  ( '2013-11-27 10:30:07' )

INSERT  INTO #tabledates
        ( dt )
VALUES  ( '2013-11-28 10:30:07' )

INSERT  INTO #tabledates
        ( dt )
VALUES  ( '2013-11-28 10:30:07' ) 

SELECT  *
FROM    #tabledates
WHERE   DATEADD(dd, 0, DATEDIFF(dd, 0, dt)) = '2013-11-28'

So, in your case, you would have...

DateTime dt=new DateTime();
dt=DateTime.Now;
Command Query is:"SELECT * FROM xxx WHERE 
                    DATEADD(dd, 0, DATEDIFF(dd, 0, Login_Date))=@date"
cmd.Parameters.add("@date",dt);
Christian Phillips
  • 18,399
  • 8
  • 53
  • 82
  • Doing this (converting/doing date math) will completely invalidate the use of any indices on that column for a search, meaning this is going to take longer than it should. – Clockwork-Muse Nov 28 '13 at 11:06
0

Okay, try this as your command query:

SELECT [t0].[Column], [t0].[Date]
FROM [Table] AS [t0]
WHERE CONVERT(DATE, [t0].[Date]) = @p0

And use:

var dt = DateTime.Today;
dav_i
  • 27,509
  • 17
  • 104
  • 136
  • This will work in SQL-Server... But i need in **MY-SQL** server... Please Try some.. – Rahul Uttarkar Nov 28 '13 at 09:53
  • He shouldn't need to convert the column to get the answer. Among other things, this will completely invalidate the use of any index during query execution, meaning it's going to take longer than it should. – Clockwork-Muse Nov 28 '13 at 11:04
0

Please Try it

SELECT * FROM xxx  WHERE (CONVERT(varchar(10), Login_Date,110)='27-Nov-13') 
code save
  • 1,054
  • 1
  • 9
  • 15
0

@Joe's answer is correct, but unfortunately is written for SQL Server.
Here's a version for MySQL:

SELECT Login_Date
FROM tabledates
WHERE Login_Date >= DATE(@date)
      AND Login_Date < DATE_ADD(DATE(@date), INTERVAL 1 DAY)

(working SQL Fiddle example, only with an explicit timestamp instead of the replacement variable)
Will use an index, if available.

Clockwork-Muse
  • 12,806
  • 6
  • 31
  • 45
0

Having an index on a date/time variable is not the best practice. I'd suggest that you create another field in the table, which will have only the date value, and then it would be easy to execute your own script and that will retrieve to you exactly what is needed. In case this solution is not doable; I'd suggest that you use:

SELECT *
FROM xxx
WHERE Login_Date BETWEEN Date1 AND Date2

Where Date1 will be the date of when you want the Login attempts to show (November 27, 2013) and Date2 will be the next day (November 28, 2013)

N.B.: The most important thing to keep in your mind, in the second example, that you make sure that the times are 00:00.000, so no attempts would be included from the next day

CodingMate
  • 333
  • 4
  • 16