Wednesday, April 29, 2009

Using Apply: CROSS APPLY and OUTER APPLY

The APPLY operator allows you to invoke a table-valued function for each row returned by an outer table expression of a query.

http://msdn.microsoft.com/en-us/library/ms175156.aspx

CRL Table Valued Functions

http://msdn.microsoft.com/en-us/library/ms131103.aspx

Thursday, April 23, 2009

Save (Not Permitted) Dialog Box in MS SQL 2008

The Save (Not Permitted) dialog box warns you that saving changes is not permitted because the changes you have made require the listed tables to be dropped and re-created.

The following actions might require a table to be re-created:

Adding a new column to the middle of the table

Dropping a column

Changing column nullability

Changing the order of the columns

Changing the data type of a column

To change this option, on the Tools menu, click Options, expand Designers, and then click Table and Database Designers. Select or clear the Prevent saving changes that require the table to be re-created check box.


http://msdn.microsoft.com/en-us/library/bb895146.aspx

Friday, April 17, 2009

Add leading zeros to a number in a MS SQL query

To add leading '0's to a column when length is less than 5


SELECT CASE WHEN LEN(rtrim(COLUMN)) < 5 THEN REPLICATE('0', 5 - LEN(rtrim(COLUMN))) + rtrim(COLUMN) ELSE rtrim(COLUMN) END

OR

SELECT RIGHT('00000' + RTRIM(COLUMN), 5)

OR

SELECT RIGHT(REPLICATE('0',5) + CONVERT(varchar( 6 ) , 1.2) ,5)