Getting the number of rows for todays date in MS SQL

Great in this example I am going to show you how you can get the rows from sql table (lets say you want to get the number of records that your users had made). For this purpose I will call our sql table just "table" and will make a field called "PostDate" where I am going to store DateTime.Now when user makes a post and I will use this field to compare it with the T-SQL getdate() which returns SQL servers date time. Ok, The "104" argument that you can see is German date time format dd.mm.yyyy.

SELECT COUNT(*) AS [rowcounttoday] FROM table CONVERT(char,PostDate,104) = CONVERT(char,getdate(),104)

Ok let's make a use of this and bind it to a lable control for exapmle:

<asp:SqlDataSource ID="SqldataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:MyConnection %>" SelectCommand="Select Count(*) As [rowcounttoday] from posts where convert(char, PostDate, 104) = convert(char, getdate(),104)" />

<asp:DataList ID="datalist1" runat="server" DataSourceID="SqldataSource1">

<ItemTemplate>

<asp:Lable ID="lblTodaysPosts" runat="server" Text="Number of todays posts: " />

<asp:Lable ID="lblNumber" runat="server" Text='<%# Bind ("rowcounttoday")%>' />

</ItemTemplate>

</DataList>

Hope it helps

Cheers

Published Monday, March 03, 2008 5:48 AM by stoian bucovich

Comments

# re: Getting the number of rows for todays date in MS SQL

Monday, March 03, 2008 2:00 PM by Richard

Even if you have a clustered index on PostDate, your query will ignore it an fall back to scanning the whole table:

www.sqlservercentral.com/.../61809

# Stoian bucovich | Alliterationpl

Saturday, November 26, 2011 3:50 PM by Stoian bucovich | Alliterationpl

Pingback from  Stoian bucovich | Alliterationpl

Leave a Comment

(required) 
(required) 
(optional)
(required)