Click here to Skip to main content
14,429,289 members
Rate this:
Please Sign up or sign in to vote.
See more:
i want to find date time fromdate -> todate
if i write fromdate 03.12.2019 todate 03.12.2019 it doesn't give me any results but i have this date in my datebase .

i just one to display one date ?!
what should be the condition in this case ?

What I have tried:

DECLARE @fromdate datetime = Convert (datetime, '27.11.19', 4), @todate datetime= Convert (datetime, '27.11.19', 4)
Updated 3-Dec-19 5:55am
phil.o 3-Dec-19 6:02am
You already got an answer to this question here[^]. You even accepted the solution.
So, how come you cannot apply today what you have learned yesterday?
[no name] 3-Dec-19 6:57am
Hi . it's another question or another problem . here i wanted to get data from the same day or same date
Rate this:
Please Sign up or sign in to vote.

Solution 2

Either add 23:59:59.9999 to your ToDate, or
SELECT * FROM MyTable WHERE EnterDate BETWEEN @fromdate AND DATEADD(dd, 1, @todate)
The reason is that - certainly in my DB's - the EnterDate column is a timestamp - which means it includes a time portion. Since you don't supply a time portion to your from or to values, they default to midnight, so no "real" timestamps match them.
Adding one day to the to value sets that to midnight of the next day so the whole of 2019-03-12 matches.
[no name] 3-Dec-19 6:17am
like always you are the best
Rate this:
Please Sign up or sign in to vote.

Solution 1

Try to include time in your 2 date : 00:00:00 and 23:59:59
Rate this:
Please Sign up or sign in to vote.

Solution 3

If this is going to be commonly used, you may want to create a User Defined Function to take care of this.

This example will return the last second of the date you entered
CREATE FUNCTION [dbo].[EndOfDay] (
  @DateText  Char(8)
  RETURN DateAdd(Ss, 86399, Convert (DateTime, @DateText, 4))
You could then call this function using this standalone query
SELECT dbo.EndOfDay('27.11.19')
And in the query you provided you could use it in your declaration statement
DECLARE @fromdate datetime = Convert (datetime, '27.11.19', 4)
,       @todate   datetime = dbo.EndOfDay('27.11.19')

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

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100