DATETIME SELECT SELECT INTO DATE PAD STRING DYNAMIC SQL CURSOR MONEY FORMAT PERCENT STORED PROCEDURE SQL SERVER AGENT JOB OPTIMIZATION WHILE LOOP OVER PARTITION BY UPDATE
 
 
SITE SEARCH SQLUSA.com VIDEOS
 
 
SQL E/BOOKS   WORLD, USA & SQL NEWS   FORMAT
SCRIPTS SQL 2005 SQL 2008 ARTICLES
How to sort on date column?

If a date column in char/varchar MM/DD/YY format, it will not sort correctly in an order by clause.

It is better to use the 111, 112 string date formats to define the date column as YYYY/MM/DD or YYYY.MM.DD :

select convert(char(10), getdate(),112)

-- Sort MM/DD/YY similar string date format

ORDER BY convert(datetime, YourDate,1)

 

-- or in SQL Server 2008

 

ORDER BY convert(date, YourDate,1)

 

More on datetime / date conversion at the following link:


http://www.sqlusa.com/bestpractices/datetimeconversion/

 

Exam Prep 70-461
SQL 2016 DESIGN & PROGRAMMING
 
 
 
DATETIME SELECT SELECT INTO DATE PAD STRING DYNAMIC SQL CURSOR MONEY FORMAT PERCENT STORED PROCEDURE SQL SERVER AGENT JOB OPTIMIZATION WHILE LOOP OVER PARTITION BY UPDATE