NULLs Sort Last : Order by « Query « SQL Server / T-SQL Tutorial

SQL Server / T-SQL Tutorial
1. Query
2. Insert Delete Update
3. Table
4. Table Join
5. Data Types
6. Set Operations
7. Constraints
8. Subquery
9. Aggregate Functions
10. Date Functions
11. Math Functions
12. String Functions
13. Data Convert Functions
14. Analytical Functions
15. Sequence Indentity
16. View
17. Index
18. Cursor
19. Database
20. Transact SQL
21. Procedure Function
22. Trigger
23. Transaction
24. XML
25. System Functions
26. System Settings
27. System Tables Views
28. User Role
29. CLR
Java
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
SQL Server / T-SQL Tutorial » Query » Order by 
1. 3. 11. NULLs Sort Last
4>
5>
6CREATE TABLE Customers (
7>      CustomerID nchar (5NOT NULL ,
8>      CompanyName nvarchar (40NOT NULL ,
9>      ContactName nvarchar (30NULL ,
10>     ContactTitle nvarchar (30NULL ,
11>     Address nvarchar (60NULL ,
12>     City nvarchar (15NULL ,
13>     Region nvarchar (15NULL ,
14>     PostalCode nvarchar (10NULL ,
15>     Country nvarchar (15NULL ,
16>     Phone nvarchar (24NULL ,
17>     Fax nvarchar (24NULL
18)
19> GO
1>
2INSERT Customers VALUES('1','A','Maria',    'Sales',  'Str. 57', 'Berlin'    ,NULL,'12209', 'Germany','111-1111111','111-1111111')
3INSERT Customers VALUES('2','M','Joe',      'Owner',  'Ave. 231','Vancouver' ,NULL,'05023', 'Mexico', '(222222-3332',NULL)
4INSERT Customers VALUES('3','H','Thomas',   'Sales',  'Sq.  111','London'    ,NULL,'1D00P', 'UK',     '(444444-4444','(444444-4444')
5INSERT Customers VALUES('4','B','Berg',     'Order',  'Blv    8','Toronto'   ,NULL,'00222', 'Sweden', '4444-55 55 65','5555-55 55 55')
6INSERT Customers VALUES('5','S','Moos',     'Sales',  'Fort  57','New York'  ,NULL,'68306', 'Germany','6666-66666','6666-77777')
7INSERT Customers VALUES('6','F','Cite',     'Manager','24      ','Dalles'    ,NULL,'67000', 'France', '88.60.15.31','88.60.15.32')
8INSERT Customers VALUES('7','C','Sommer',   'Owner',  'Araq, 67','Paris'     ,NULL,'28023', 'Spain',  '(91555 22 82','(91555 91 99')
9INSERT Customers VALUES('8','P','Leb',      'Owner',  '12      ','Beijing'   ,NULL,'13008', 'France', '91.24.45.40','91.24.45.41')
10INSERT Customers VALUES('9','D','Elizabeth','Manager','23 Blvd.','Tsawassen','BC', 'T2F8M4','Canada', '(604555-4729','(604555-3745')
11> go

(rows affected)

(rows affected)

(rows affected)

(rows affected)

(rows affected)

(rows affected)

(rows affected)

(rows affected)

(rows affected)
1>
2>
3SELECT
4>   *
5FROM
6>   Customers
7> ORDER BY
8>   CASE
9>     WHEN Region IS NULL THEN 1
10>     ELSE 0
11>   END,
12>   Region
13> GO
CustomerID CompanyName                              ContactName                    ContactTitle                   Address                                                      City            Region
       PostalCode Country         Phone                    Fax
---------- ---------------------------------------- ------------------------------ ------------------------------ ------------------------------------------------------------ --------------- ---------
------ ---------- --------------- ------------------------ ------------------------
9          D                                        Elizabeth                      Manager                        23 Blvd.                                                     Tsawassen       BC
       T2F8M4     Canada          (604555-4729           (604555-3745
9          D                                        Elizabeth                      Manager                        23 Blvd.                                                     Tsawassen       BC
       T2F8M4     Canada          (604555-4729           (604555-3745
1          A                                        Maria                          Sales                          Str. 57                                                      Berlin          NULL
       12209      Germany         111-1111111              111-1111111
2          M                                        Joe                            Owner                          Ave. 231                                                     Vancouver       NULL
       05023      Mexico          (222222-3332           NULL
3          H                                        Thomas                         Sales                          Sq.  111                                                     London          NULL
       1D00P      UK              (444444-4444           (444444-4444
4          B                                        Berg                           Order                          Blv    8                                                     Toronto         NULL
       00222      Sweden          4444-55 55 65            5555-55 55 55
5          S                                        Moos                           Sales                          Fort  57                                                     New York        NULL
       68306      Germany         6666-66666               6666-77777
6          F                                        Cite                           Manager                        24                                                           Dalles          NULL
       67000      France          88.60.15.31              88.60.15.32
7          C                                        Sommer                         Owner                          Araq, 67                                                     Paris           NULL
       28023      Spain           (91555 22 82           (91555 91 99
8          P                                        Leb                            Owner                          12                                                           Beijing         NULL
       13008      France          91.24.45.40              91.24.45.41

(rows affected)
1>
2>
3> drop table Customers;
4>
5> GO
1>
1. 3. Order by
1. 3. 1. The expanded syntax of the ORDER BY clause
1. 3. 2. ORDER BY Clause: ORDER BY {[col_name | col_number [ASC | DESC]]}, ...
1. 3. 3. Sorting your grouped results with ORDER BY clause.
1. 3. 4. An ORDER BY clause that sorts by one column in descending sequence
1. 3. 5. the order criterion may contain more than one column.
1. 3. 6. An ORDER BY clause that sorts by three columns
1. 3. 7. An ORDER BY clause that uses an alias
1. 3. 8. An ORDER BY clause that uses an expression
1. 3. 9. An ORDER BY clause that uses column positions
1. 3. 10. Sorting your grouped results by an aggregate.
1. 3. 11. NULLs Sort Last
1. 3. 12. order criterion contains any aggregate function.
1. 3. 13. Order by not null date
1. 3. 14. Using the TOP keyword with Ordered Results
1. 3. 15. do our sorting using numeric fields
1. 3. 16. use CAST function to sort during ordering
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.