SQL query to display data by current (current) date?

I am trying to get a record from a database with the current date, only here is the query that I tried without getting any records from the table:

SELECT *
FROM Transactions
WHERE transaction_date = GETDATE(); 
+4
source share
3 answers

Just compare the date

   Select *from Transactions
        WHERE 
    CONVERT(char(10), transaction_date  ,126)=CONVERT(char(10), getdate(),126);
+2
source

You can delete / reset the time until 00:00, this will allow comparisons to be made today.

select DATEADD(dd, DATEDIFF(dd, 0, getdate()), 0)

You can define variables with today's start and end dates as follows:

declare @TodayStart datetime = getdate()
declare @TodayEnd datetime = getdate()

set @TodayStart = DATEADD(dd, DATEDIFF(dd, 0, getdate()), 0) -- example: 2017-08-20 00:00:00.000
set @TodayEnd = DATEADD(ss, -1, DATEADD(dd, 1, @TodayStart)) -- example: 2017-08-20 23:59:59.000

Thus, your request to receive all transactions within today's date range will be as follows:

Select *from Transactions
    WHERE transaction_date  between @TodayStart AND @TodayEnd
+2
source

CONCAT

. :

var currentDate = DateTime.Now.ToString("yyyy-MM-dd");
query = "select * from table where Date = " + currentDate;
+1

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


All Articles