Sunday, June 9, 2013

SELECT SalesOrderID
,   ShipDate
FROM Sales.SalesOrderHeader
WHERE ShipDate BETWEEN '2005-07-23T00:00:00'
                       AND '2005-07-24T23:59:59'


IS NULL
IS NOT NULL


SELECT ProductID,
   Name,
   ISNULL(Weight,0) AS Weight
FROM Production.Product
WHERE Weight IS NULL;

SELECT ProductID,
   Name,
   Weight
FROM Production.Product
WHERE Weight IS NOT NULL;


IN

WHERE Color IN ('Silver', 'Black', 'Red')
WHERE Color = 'Silver' OR Color = 'Black' OR Color = 'Red'

WHERE Description LIKE '%/%%' ESCAPE '/
Wildcards for the LIKE predicate
WildcardUsage
%The percent sign. Represents a string of zero or more characters
_The underscore. Represents a single character
[…]A list of characters enclosed within square brackets. Represents a single character from among any in the list. You may use the hyphen as a shorthand to translate a range into a list. For example, [ABCDEF]-flatcan be written more succinctly as[A-F]-flat.You can also mix and match single characters and ranges. The expressions[A-CD-F]-flat[A-DEF]-flat, and [ABC-F]-flat all mean the same thing and ultimately resolve to [ABCDEF]-flat.
[^…]A list of characters enclosed within square brackets and preceeded by a caret. Represents a single character from among any not in the list.
ORDER BY

ORDER BY p.Name DESC,
   h.EndDate DESC

ORDER BY p.Name ASC,
   h.EndDate ASC;

ORDER BY p.Name ASC,
   h.EndDate DESC;


One issue when ordering by unselected columns is that ORDER BY items must appear in the SELECT list if SELECT DISTINCT is specified. That’s because the grouping operation used internally to eliminate duplicate rows from the result set has the effect of disassociating rows in the result set from their original underlying rows in the table. That behavior makes perfect sense when you think about it. A deduplicated row in a result set would come from what originally were two or more table rows. And which of those rows would you go to for the excluded column? There is no answer to that question, and hence the caveat.


SELECT p.ProductID,
   p.Name,
   p.Color
FROM Production.Product AS p
WHERE p.Color IS NOT NULL
ORDER BY 
CASE  LOWER(p.Color)
    WHEN 'red' THEN ' '
    ELSE LOWER(p.Color)
END;

SELECT ProductID, Name
FROM Production.Product
ORDER BY Name
OFFSET 0 ROWS FETCH NEXT 10 ROWS ONLY



DECLARE @AddressLine2 nvarchar(60)
DECLARE @AddressLine3 nvarchar(60)
SELECT @AddressLine3=ISNULL(@AddressLine2,'10, lake view')
SELECT @AddressLine3


DECLARE @AddressLine1 nvarchar(60) = '101 E. Varnum'
DECLARE @AddressLine2 nvarchar(60) = 'Ambulance Desk'
SELECT @AddressLine1 = AddressLine1, @AddressLine2 = AddressLine2
FROM Person.Address
WHERE AddressID = 49862;
IF @@ROWCOUNT = 1
  SELECT @AddressLine1, @AddressLine2
ELSE
  SELECT 'Either no rows or too many rows found.';

No comments:

Post a Comment