Showing posts with label Dates. Show all posts
Showing posts with label Dates. Show all posts

Sunday, 17 February 2013

Fun with dates

Every year me and a group of friends attend the Royal International Air Tattoo (RIAT) at RAF Fairford, for us it is a long weekend where we camp close to the airfield and get to take in three full days (if you include Friday practice  of utterly breath-taking aerobatic displays from some of the greatest military aircraft in the world.  We have a running joke during the year long wait between events that once we get Christmas out of the way RIAT will be here before we know it.  


We all use dates as DBA’s in fact we use them all the time, every single day even without realising it.  I often use T-SQL to show me the difference between dates, but this particular script I like because it gives me a countdown to RIAT, so on a bad day or even on a good day for that matter I can see just how long, or little time is left until we set off once again J

Here is a script I use for fun;

/*
      -----------------------------------------------------------------
      Fun with dates
      -----------------------------------------------------------------
    
      For more SQL resources, check out SQLServer365.blogspot.com

      -----------------------------------------------------------------

      You may alter this code for your own purposes.
      You may republish altered code as long as you give due credit.
      You must obtain prior permission before blogging this code.

      THIS CODE AND INFORMATION ARE PROVIDED "AS IS"
    
      -----------------------------------------------------------------
*/
-- Set database context
USE master;
GO

-- Declare variables
DECLARE @startTime DATETIME
DECLARE @endTime DATETIME
DECLARE @Seconds INT

-- Set date range
SET @startTime = GETDATE()
SET @endTime = '20130719 04:30'

-- Get the difference in seconds
SELECT  @Seconds = DATEDIFF(ss, @startTime, @endTime);

-- Return how long left                                                       
SELECT  'T Minus ' + CONVERT(VARCHAR(10), ( @Seconds / 86400 )) + ' Days '
        + CONVERT(VARCHAR(10), ( ( @Seconds % 86400 ) / 3600 )) + ' Hours '
        + CONVERT(VARCHAR(10), ( ( ( @Seconds % 86400 ) % 3600 ) / 60 ))
        + ' Minutes ' + CONVERT(VARCHAR(10), ( ( ( @Seconds % 86400 ) % 3600 )
                                               % 60 )) + ' Seconds and counting!';
GO

Enjoy!

Chris

Tuesday, 8 November 2011

CONVERT Dates and Confusion

Right I've been itching to post another Blog and actually contribute something this time.  This isn't something I've learned but something I've taught many times in the past.


It can't be just me but I get asked a lot about the formatting of dates especially for reports.


This is something that is almost always over complicated and misunderstood, one table in BOL covers it.  Search for 'CONVERT function' and the first remark is 'Date and Time Styles'.  This table gives you certainly every date format I have ever had to use and more.  My particular favourite is ISO 112 it confuses the hell out of people when you use it on a form, but that's just my inner geek coming out.


I've included a selection of the most common conversions I have used in the past in the below T-SQL



-- Month first  
SELECT CONVERT(VARCHAR(12),GETDATE(), 101)  -- 11/08/2011
SELECT CONVERT(VARCHAR(12),GETDATE(), 110)  -- 11-08-2011
SELECT CONVERT(VARCHAR(12),GETDATE(), 100)  -- Nov  8 2011 
SELECT CONVERT(VARCHAR(12),GETDATE(), 107)  -- Nov 08, 2011


-- Year first  
SELECT CONVERT(VARCHAR(12),GETDATE(), 102)  -- 2011.11.08
SELECT CONVERT(VARCHAR(12),GETDATE(), 111)  -- 2011/11/08
SELECT CONVERT(VARCHAR(12),GETDATE(), 112)  -- 20111108
   
-- Day first  
SELECT CONVERT(VARCHAR(12),GETDATE(), 103)  -- 08/11/2011 
SELECT CONVERT(VARCHAR(12),GETDATE(), 105)  -- 08-11-2011 
SELECT CONVERT(VARCHAR(12),GETDATE(), 104)  -- 08.11.2011
SELECT CONVERT(VARCHAR(12),GETDATE(), 106)  -- 08 Nov 2011 
   
-- Time only  
SELECT CONVERT(VARCHAR(12),GETDATE(), 108)  -- November
SELECT CONVERT(VARCHAR(12),GETDATE(), 114)  -- 14:36:14:043


SELECT DATENAME(MONTH, GETDATE()) -- September 
SELECT DATENAME(DAY,GETDATE()) -- 8  
SELECT DATENAME(YEAR, GETDATE()) -- 2011  


-- Concatente values  
-- November-8-2011
SELECT DATENAME(MONTH, GETDATE()) + '-' + DATENAME(DAY,GETDATE()) + '-' + DATENAME(YEAR, GETDATE()) 


Enjoy, Chris!