Recent Articles

MySQL Replication
Replication is a way to run several SQL servers so that all of them have a current copy of a database or databases. Keep in mind that replication is not the same as clustering and should not be confused as such...

Closing The Open Source Model Of MySQL
Okay, I didn't really want to write about Sun again today, but Simon has an interesting post about the "Sun Model" for open source business. Simon states that the model boils down to: "1. remove barriers to software...

SQL Loyalty And Competition
MySQL's Kaj Arno has an interesting post titled: "On Loyalty, Competition and Underdogs". Kaj starts with his son's list of reasons to stay with Bayern Munchen...

Microsoft Adds New Feature To SQL Server With...
Microsoft has introduced a new managed self-service analysis capabilities code-named "Project Gemini," and self-service reporting in the next release of Microsoft SQL Server focused on Business Intelligence (BI) code...

Emulating LIMIT With MSSQL 2005
I don't like MSSQL, I prefer MySQL, but many times I have to deal with it at work. Lots of developers, including me and my co-worker Enrico, hate that Microsoft...

Microsoft Tool Reduces SQL Injection Attacks
Microsoft has introduced a new security filter for its Internet Information Service (IIS) Web server that is aimed at reducing SQL injection attacks. The application called UrlScan 3.0 is free and is an add-on tool...

Prevent SQL Injections With CFQUERYPARAM
If you haven't been using the cfqueryparam tag, chances are you had a baptism by fire this week. As you may have heard, lots of ColdFusion powered sites...



Click to Play

Getting Your Viral Campaign Going
Viral campaigns are a current trend that everyone wants to take part in, but launching a campaign is not something that people should take lightly. Not...
12.09.08

Finding And Using DateFormat Within SQL

By Pete Freitag

Have you ever tried to find a DateFormat or date_format function in SQL Server? Chances are
you probably ended up concatenating a bunch of DatePart strings to get the format you are looking
for.

What about grouping by a date in SQL Server? This can be a pain because the datetime and the smalldatetime types both contain the time of day, so you can't simply add a datetime to the GROUP BY clause, because it will group by the time, not the date.

I have been working with SQL Server since version 7, and just today I found a good way to do this!

It turns out there is a third argument to the CONVERT function in SQL server that accepts a style (accepts an integer). If you pass in a 101 it will return the date in mm/dd/yyyy format.

So for example let's say you want to get the revenue for a given day you would do something like this:



The Fundamental Server: Everything You
Need Inside and Outside the Box - Learn More

Here are a few style values you can use:



Find a full list in the CONVERT docs.

This still isn't the prettiest solution, but it's much better than using DatePart. PS - Microsoft, why not give us a real DateFormat function?

Comments


About the Author:
Pete Freitag (http://www.petefreitag.com/) is a software engineer, and web developer located in central new york. Pete specializes in the HTTP protocol, web services, xml, java, and coldfusion. In 2003 Pete published the ColdFusion MX Developers Cookbook with SAMs Publishing.

Pete owns a Firm called Foundeo (http://foundeo.com/) that specializes in Web Consulting, and Products for Web Developers.
SQLproNews is brought to you by:

SecurityConfig.com NetworkingFiles.com
NetworkNewz.com WebProASP.com
DatabaseProNews.com SQLProNews.com
ITcertificationNews.com SysAdminNews.com
SQLproNews.com WirelessProNews.com
CProgrammingTrends.com SysAdminNews.com


About SQLproNews
SQLproNews is a collection of up to date tutorials and insightful articles designed to help SQL users of any skill level implement successful SQL systems and practices. SQL Strategies and Tactics for Business




-- SQLProNews is an iEntry, Inc. publication --
iEntry, Inc. 2549 Richmond Rd. Lexington KY, 40509
2008 iEntry, Inc.  All Rights Reserved  Privacy Policy  Legal

advertising info | news headlines | free newsletters | comments/feedback | submit article


SQL Strategies and Tactics for Business SQLproNews News Archives About Us Feedback SQLproNews Home Page About Article Archive News Downloads WebProWorld Forums Jayde iEntry Advertise Contact