How to Group by Date in SQL

Save

Microsoft SQL Server has a "group by" code phrase that lets you group all of your records by the date when the records are saved. You must convert a date to a string value in Microsoft SQL Server before you can group your records by date. Converting to "varchar" allows the SQL engine to recognize the grouped results instead of treating each record as a separate value in the results.

  • Click the Windows Start button and select All Programs. Click Microsoft SQL Server, and then click SQL Server Management Studio to open your SQL editor.

  • Click New Query after the editor software loads. This opens the SQL editor window where you can type your SQL statement to group records by date.

  • Create the code that selects and groups the results by date. Type the following code to create your group by statement:

    select distinct convert(varchar,date_field,111) as thedate, name as name

    from customers

    group by convert(varchar,date_field,111), name

    The preceding code returns all customers grouped by the date on which they signed up on your site. Replace "date_field" with your own date field, and change "customers" to your own table name.

  • Press F5 to execute the code. Notice that your customers are grouped by date in the database results.

References

Promoted By Zergnet

Comments

Related Searches

Check It Out

Geek Vs Geek: Robot battles, hoverboard drag race, and more

M
Is DIY in your DNA? Become part of our maker community.
Submit Your Work!