SQL Server Tips and Tricks

How to Use Aggregate Functions in Your Queries by Kelly Lynn

If you’re new to writing SQL queries, you may not know about aggregate functions. These handy tools can provide you with some quick information about your tables. For example, you can use aggregate functions to find the number of rows in your table or to do simple calculations like find the minimum or maximum values in a column. Let’s take a closer at how aggregate functions work:   How Aggregate Functions are Different from Basic Queries – If you’re learning to write queries, you’re probably comfortable with the “SELECT * FROM Tablename” syntax to return all the rows in a table. When you use a function in your query, you’re only going to get one result (unless you include a group by condition on your query, but we won’t get … Continue reading

Get 12-Hour Time from Datetime Datatype in SQL Server 2005 by Ken Baker

It's not as simple as you may think to extract the time in a 12-hour format (as opposed to 24-hour, military format) from a datetime field in SQL Server 2005.   Let me walk you through the solution. First, let's select the datetime field. SELECT getdate() AS MyTime If you didn't want to use the current time, then you can of course substitute the appropriate field in like so: SELECT mydatetimefield AS MyTime FROM mytable For simplistic sake, I'll just use getdate() going forward in this tutorial as my source datetime field.  The result should result in the current date and time: 2010-11-07 12:04:17.903 Next, we're going to convert the current datetime field to a varchar format using style 100 which will include AM/PM. SELECT CONVERT(VARCHAR, getdate(), 100) AS MyTime … Continue reading