Check smalldatetime column equal to GetDate () - ignore time

I have a column of type smalldatetime, date

I would like to run a query that only retrieves rows:

where date = convert (smalldatetime, GetDate ())

However, this never finds a match, as it is also a comparison of time.

That is: 01-01-2010 12: 23: 00! = 01-01-2010 12:25:00

How to find matches only for a part of a date?

+3
source share
6 answers

One way to use the index

where date >= dateadd(dd, datediff(dd, 0, getdate()), 0)
and date < dateadd(dd, datediff(dd, 0, getdate()), +1)

See also: How to work with dates in SQL Server?

+3
source

Try:

where datediff(dd, yourdate, GetDate()) = 0
+2
source

DateTime Date. , .

Date Time MSSQL .

+1
source
where cast(CONVERT(char(10),DATETIME_FIELD_1,101) as datetime) 
  = cast(CONVERT(char(10),DATETIME_FIELD_2,101) as datetime)
0
source

You can try:

Where CAST(FLOOR(CAST([Date] AS FLOAT)) AS DATETIME) = CAST(FLOOR(CAST(GETDATE() AS FLOAT)) AS DATETIME)

This will result in both dates being displayed as:

2010-08-05 00: 00: 00.000

and thus only a comparison of dates ignoring times

0
source

found this:

CAST(CONVERT (CHAR(8), GETDATE(), 112) AS smalldatetime)

from here: link

this sql worked for me when I added a smalldatetime field to an existing table:

SELECT     testing 
FROM         LTCCAssessment
WHERE     (testing = CAST(CONVERT(CHAR(8), GETDATE(), 112) AS smalldatetime))
0
source

Source: https://habr.com/ru/post/1758228/


All Articles