SQL Filter by Date

Handian Sudianto 5,666 Reputation points
2023-12-12T23:41:13.3266667+00:00

Hello,

I have some records like this picture, we can see i have data with today time stamp for some days.

User's image

Now when i make an query to filter to get record for today with below syntax and i found no result.

Anyone know why?

select top 10 * from InterfaceTrafficByDays where datetime= CAST( GETDATE() AS Date )

User's image

SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
14,320 questions
{count} votes

Accepted answer
  1. LiHongMSFT-4306 29,906 Reputation points
    2023-12-13T05:24:58.5133333+00:00

    Hi @Handian Sudianto

    I tun the query in this morning at 06.30 and below 07AM with no result

    You run this query 12/13/2023 06.30 and at the same time the Top 10 datas in InterfaceTrafficByDays table are all 12-12-2023. That's why you got no result with this condition where Datetime = cast(getdate() as date).

    but if i test now i got the result. Its strange for me.

    Check if the InterfaceTrafficByDays table have records of 12-13-2023 now. As I guessed, you got the result now because records of 12-13-2023 are inserted.

    Best regards,

    Cosmog Hong


1 additional answer

Sort by: Most helpful
  1. PercyTang-MSFT 12,506 Reputation points Microsoft Vendor
    2023-12-13T02:13:51.3933333+00:00

    Hi @Handian Sudianto

    I have done a test.

    create table test(id int,DateTime datetime);
    insert into test values
    (1,'2023-12-12'),
    (2,'2023-12-13');
    
    select top 1 * from test where Datetime = cast(getdate() as date);
    

    User's image

    There does not seem to be a problem with the query.

    User's image

    Did you run that query yesterday?

    Maybe you need to provide test data.

    Best regards,

    Percy Tang


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.