Apr 23rd, 2009 | No Comments

Here is the script to rename an SQL column,

EXEC sp_rename
@objname = 'table_name.old_column_name',
@newname = 'new_column_name',
@objtype = 'COLUMN'

Written by Ajay Matharu

April 23rd, 2009 at 10:06 am

Posted in Microsoft,SQL

Tagged with , , ,

Apr 6th, 2009 | No Comments

This is how you can user select case in sql statements,

USE AdventureWorks;
GO
SELECT   ProductNumber, Category =
CASE ProductLine
WHEN 'R' THEN 'Road'
WHEN 'M' THEN 'Mountain'
WHEN 'T' THEN 'Touring'
WHEN 'S' THEN 'Other sale items'
ELSE 'Not for sale'
END,
Name
FROM Production.Product
ORDER BY ProductNumber;
GO
USE AdventureWorks;
GO
SELECT   ProductNumber, Name, 'Price Range' =
CASE
WHEN ListPrice =  0 THEN 'Mfg item - not for resale'
WHEN ListPrice < 50 THEN 'Under $50'
WHEN ListPrice >= 50 and ListPrice < 250 THEN 'Under $250'
WHEN ListPrice >= 250 and ListPrice < 1000 THEN 'Under $1000'
ELSE 'Over $1000'
END
FROM Production.Product
ORDER BY ProductNumber ;
GO

this is similar to if condition that you use in any other programming language but this you can use within a query to check the value and perform the respective task.

Written by Ajay Matharu

April 6th, 2009 at 10:28 am

Posted in Development,SQL

Tagged with , ,