Sunday, March 22, 2009

SQL GROUP BY techniques

SELECT
C.CustomerID, C.CustomerName,
C.CustomerType, C.Address1, C.City,
C.State, S.TotalSales
FROM
Customers C
INNER JOIN
(SELECT
CustomerID, SUM(Sales) as TotalSales
FROM
Sales
GROUP BY
CustomerID) S
ON
C.CustomerID = S.CustomerID

Sunday, March 1, 2009

MS SQL Check for empty String

MS SQL one cannot use the <> '' on text, ntext and varchar data types:

SQL:

SELECT * FROM myTable WHERE myField LIKE '_%'

OR If you want to get all the field with empty string use

SQL:

SELECT * FROM myTable WHERE myField NOT LIKE '_%'