Date/Time formatting trick for SQL Server

As we all know, ColdFusion has a stellar line up of date and time formatting functions. But what happens when you need to do this at the database query level in a CFC or a Stored Procedure? Plus a FREE Scalar function!

So here's a quick way using SQL Server's CONVERT function to produce a MM/DD/YYYY HH:MM AM type of format (example output: 05/10/2010 03:17 PM) directly in your SELECT statement...

view plain print about
1SELECT EntryDateTime,
2 CONVERT(VARCHAR(10), a.EntryDate, 101) + ' ' + REPLACE(REPLACE(RIGHT('0'+LTRIM(RIGHT(CONVERT(VARCHAR,a.EntryDateTime,100),7)),7),'AM',' AM'),'PM',' PM') AS EntryDateTimeDisplay,
3FROM MyTableName

This can prove to be useful if you want to handle the date and time formatting within a cfquery or maybe even inside of a T-SQL Stored Procedure that you'll call via ColdFusion. If you want to get downright modular and really fancy (and who doesn't?) then you could create your own SCALAR function in SQL Server and just call it inline directly in a SELECT statement. Here, just run this from a T-SQL prompt and you too can call it right from your very own cfquery! (Don't say I never gave you anything ;)

view plain print about
1CREATE FUNCTION [dbo].[formatDateTime] (@dateTimeData SMALLDATETIME)
2RETURNS VARCHAR(30)
3AS
4BEGIN
5    DECLARE @newDateTimeString VARCHAR(30)
6    SET @newDateTimeString = CONVERT(VARCHAR(10), @dateTimeData, 101) + ' ' + REPLACE(REPLACE(RIGHT('0'+LTRIM(RIGHT(CONVERT(VARCHAR,@dateTimeData,100),7)),7),'AM',' AM'),'PM',' PM')
7    RETURN @newDateTimeString
8END

So now that we've done the heavy lifting up front we can use this in any query just as if it were an out of the box SQL Server function - which means that you can also use it all over the place in ColdFusion cfquery statements too - WOOT!

view plain print about
1SELECT    GETDATE() AS CurrentDateTime,
2            dbo.formatDateTime(GETDATE()) AS CurrentDateTimeDisplay

Remember folks, FUNCTIONS are your friends!

Related Blog Entries

Comments
# Posted By rtrey | 7/15/10 6:43 AM
handychina's Gravatar It is very interesting Sciphones i68,ciphone c6,Ciphone,cect handy,Sciphones ,cecthandys,hiphone,HIPHONE 4,pinphone 3gs?Grosshandel in http:/www.efox-shop.com/
# Posted By handychina | 7/23/10 11:29 PM
NFLjerseys's Gravatar Low price, high quality, free shipping, [url=http://www.nfljerseys-shopping.com/]NFL jerseys[/url] and [url=http://www.nfljerseys-shopping.com/]football jerseys[/url] are hot sold online, you can save up to 72%. Get more details about [url=http://www.nfljerseys-shopping.com/]cheap jerseys[/url] at nfljerseys-shopping.com.
# Posted By NFLjerseys | 7/27/10 12:12 AM
tapout hat's Gravatar Happy to see your blog as it is just what I’ve looking for and excited to read all the posts. I am looking forward to another great article from you. After skimming through your website
# Posted By tapout hat | 7/27/10 6:27 AM
ciphone's Gravatar Hello!everyone!iphone is so expensive.so I like <a href=" www.efox-shop.com/index.php/cPath/5_35
" rel="nofollow"> Ciphone </a>. d^_^bIt is very interesting.I want to introduce <a href="http://www.efox-shop.com/"; rel="nofollow"> efox-shop</a> to you.
# Posted By ciphone | 7/30/10 11:16 PM
NFL Jerseys's Gravatar This is all very new to me and this article really opened my eyes.Thanks for sharing with us your wisdom.
http://www.nfljerseyspaypal.com cheap nfl jerseys
http://www.nfljerseysgo.com NFL Shop
http://www.nfljerseysgo.com NFL Jerseys
http://www.eightshop.com polo shirts
# Posted By NFL Jerseys | 8/23/10 11:48 PM
ray ban sunglasses's Gravatar I am totally agree with your oppinion.this blog post is very encouraging to people who want to know these topics.
http://www.onesunglasses.com oakley sunglasses
http://www.onesunglasses.com ray ban sunglasses
http://www.onesunglasses.com gucci sunglasses
# Posted By ray ban sunglasses | 8/23/10 11:53 PM
BlogCFC was created by Raymond Camden. This blog is running version 5.9.5.007. Contact Blog Owner