posted 5/7/2012 by RussLoski - Views: [910]
One of my favorite expressions is a time string expression. I use it all the time when renaming files. In a recent MSDN question someone asked how to create a time string that used plain time, not military time (24 hour clock). I thought that would be easy and was going to tell the writer to find it for him/herself. But in trying to put it together, I found it rather challenging:
(DT_WSTR,24)((DT_WSTR,8)((10000 * YEAR( GETDATE())) + (100 * MONTH( GETDATE())) + DAY( GETDATE())) + "_" + RIGHT(REPLICATE("0",2) + (DT_WSTR,6)( (10000 * (DATEPART("Hh", GETDATE()) % 12 == 0?12: DATEPART("Hh", GETDATE()) % 12)) + (100 * DATEPART("mi", GETDATE())) + DATEPART("ss", GETDATE())),6) + (DATEPART("Hh", GETDATE()) > 11? "PM":"AM") )
If anyone knows of a simpler way to do this, let me know.