locked
SQL Statement.... RRS feed

  • Question

  • User-507786106 posted

    How to create an SQL statement, that select top 10, (count) records created today where Created = Today?

    Wednesday, July 25, 2018 12:42 PM

Answers

  • User-369506445 posted

    hi

    <g class="gr_ gr_6 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" id="6" data-gr-id="6">pleasetry</g> below query 

    select top 10 * from #Temp where CAST([date] AS DATE)=CAST(GETDATE() AS DATE)

    <g class="gr_ gr_7 gr-alert gr_gramm gr_inline_cards gr_run_anim Punctuation only-ins replaceWithoutSep" id="7" data-gr-id="7">also</g> I create a sample with Temp Table below like

    create table #Temp
    (
        ID int, 
        Title Varchar(50), 
        Date DateTime
    )
    
    
    insert into #Temp values(1 , 'title 1' , GETDATE());
    insert into #Temp values(1 , 'title 1' , GETDATE());
    insert into #Temp values(1 , 'title 1' , GETDATE()-1); -- insert yesterday
    insert into #Temp values(1 , 'title 1' , GETDATE()-2); -- last day
    
    
    select top 10 * from #Temp where CAST([date] AS DATE)=CAST(GETDATE() AS DATE)

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, July 25, 2018 12:52 PM

All replies

  • User-369506445 posted

    hi

    <g class="gr_ gr_6 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" id="6" data-gr-id="6">pleasetry</g> below query 

    select top 10 * from #Temp where CAST([date] AS DATE)=CAST(GETDATE() AS DATE)

    <g class="gr_ gr_7 gr-alert gr_gramm gr_inline_cards gr_run_anim Punctuation only-ins replaceWithoutSep" id="7" data-gr-id="7">also</g> I create a sample with Temp Table below like

    create table #Temp
    (
        ID int, 
        Title Varchar(50), 
        Date DateTime
    )
    
    
    insert into #Temp values(1 , 'title 1' , GETDATE());
    insert into #Temp values(1 , 'title 1' , GETDATE());
    insert into #Temp values(1 , 'title 1' , GETDATE()-1); -- insert yesterday
    insert into #Temp values(1 , 'title 1' , GETDATE()-2); -- last day
    
    
    select top 10 * from #Temp where CAST([date] AS DATE)=CAST(GETDATE() AS DATE)

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, July 25, 2018 12:52 PM
  • User77042963 posted

    You need an Order by clause.

    Select top 10 * from yourtable 
    where Created>=CAST(GETDATE() AS DATE)
    Order by Created

    Wednesday, July 25, 2018 2:56 PM