<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom"><id>tag:sscheralbidar.blog.co.uk,2009-11-12:/</id><title>No One is Perfect</title><link rel="self" href="http://sscheralbidar.blog.co.uk/feed/atom/posts/"/><link rel="alternate" type="text/html" href="http://sscheralbidar.blog.co.uk/"/><subtitle>This is the bloc where i want share some code snippets.</subtitle><generator version="1.0">MokoFeed</generator><updated>2009-11-12T03:50:22+01:00</updated><entry><id>tag:sscheralbidar.blog.co.uk,2007-09-21:/2007/09/21/rarely_used_sql_server_functions~3017668/</id><title>Rarely used SQL Server Functions</title><link rel="alternate" type="text/html" href="http://sscheralbidar.blog.co.uk/2007/09/21/rarely_used_sql_server_functions~3017668/"/><author><name>sscheral</name></author><published>2007-09-21T14:22:36+02:00</published><updated>2007-09-21T14:22:36+02:00</updated><content type="html">	&lt;p&gt;&lt;strong&gt;BINARY_CHECKSUM&lt;br&gt;
SIGN&lt;br&gt;
COLUMNPROPERTY&lt;br&gt;
DATALENGTH&lt;br&gt;
ASCII, UNICODE&lt;br&gt;
NULLIF&lt;br&gt;
PARSENAME&lt;br&gt;
STUFF&lt;br&gt;
REVERSE&lt;br&gt;
GETUTCDATE&lt;/strong&gt;&lt;/p&gt;
	&lt;p&gt;&lt;strong&gt;BINARY_CHECKSUM &lt;/strong&gt;&lt;br&gt;
BINARY_CHECKSUM is handy if you want to check for data differences between 2 rows of data&lt;/p&gt;
	&lt;p&gt;In order to see what rows are in table 1 and not in table 2 and vice versa you can do 2 left joins, 2 right joins or 1 left and 1 right join. To get the rows that are different you can use BINARY_CHECKSUM. You have to run this example o SQL Server 2000 to see it work, you can ofcourse use any tables just modify the queries&lt;br&gt;
Let’s get started…&lt;/p&gt;
	&lt;p&gt;--let's copy over 20 rows to a table named authors2&lt;br&gt;
SELECT TOP 20 * INTO tempdb..authors2&lt;br&gt;
FROM pubs..authors&lt;/p&gt;
	&lt;p&gt;--update 5 records by appending X to the au_fname&lt;br&gt;
SET ROWCOUNT 5&lt;/p&gt;
	&lt;p&gt;UPDATE tempdb..authors2&lt;br&gt;
SET au_fname =au_fname +'X'&lt;/p&gt;
	&lt;p&gt;--Set rowcount back to 0&lt;br&gt;
SET ROWCOUNT 0&lt;/p&gt;
	&lt;p&gt;--let's insert a row that doesn't exist in pubs&lt;br&gt;
INSERT INTO tempdb..authors2&lt;br&gt;
SELECT '666-66-6666', au_lname, au_fname, phone, address, city, state, zip, contract&lt;br&gt;
FROM tempdb..authors2&lt;br&gt;
WHERE au_id ='172-32-1176'&lt;/p&gt;
	&lt;p&gt;--&lt;strong&gt;* The BIG SELECT QUERY --&lt;/strong&gt;*&lt;/p&gt;
	&lt;p&gt;--Not in Pubs&lt;br&gt;
SELECT 'Does Not Exist On Production',t2.au_id&lt;br&gt;
FROM pubs..authors t1&lt;br&gt;
RIGHT JOIN tempdb..authors2 t2 ON t1.au_id =t2.au_id&lt;br&gt;
WHERE t1.au_id IS NULL&lt;br&gt;
UNION ALL&lt;br&gt;
--Not in Temp&lt;br&gt;
SELECT 'Does Not Exist In Staging',t1.au_id&lt;br&gt;
FROM pubs..authors t1&lt;br&gt;
LEFT JOIN tempdb..authors2 t2 ON t1.au_id =t2.au_id&lt;br&gt;
WHERE t2.au_id IS NULL&lt;br&gt;
UNION ALL&lt;br&gt;
--Data Mismatch&lt;br&gt;
SELECT 'Data Mismatch', t1.au_id&lt;br&gt;
FROM( SELECT BINARY_CHECKSUM(*) AS CheckSum1 ,au_id FROM pubs..authors) t1&lt;br&gt;
JOIN(SELECT BINARY_CHECKSUM(*) AS CheckSum2,au_id FROM tempdb..authors2) t2 ON t1.au_id =t2.au_id&lt;br&gt;
WHERE CheckSum1 &lt;&gt; CheckSum2&lt;/p&gt;
	&lt;p&gt;--Clean up&lt;br&gt;
DROP TABLE tempdb..authors2&lt;br&gt;
GO&lt;/p&gt;
	&lt;p&gt;SIGN&lt;br&gt;
Sometimes you are asked by the front-end/middle-tier developers to return a rowcount as well with the result set. However the developers want you to return 1 if there are rows and 0 if there are none. How do you do such a thing?&lt;br&gt;
Well I am going to show you two ways. the first way is by using CASE and @@ROWCOUNT, the second way is by using the SIGN function&lt;/p&gt;
	&lt;p&gt;For CASE we will do this&lt;/p&gt;
	&lt;p&gt;RETURN CASE WHEN @@ROWCOUNT &gt; 0 THEN 1 ELSE 0 END&lt;/p&gt;
	&lt;p&gt;So that's pretty simple, if @@ROWCOUNT is greater than 0 return 1 for everything else return 0&lt;/p&gt;
	&lt;p&gt;Using the SIGN function is even easier, all you have to do is this&lt;/p&gt;
	&lt;p&gt;RETURN SIGN(@@ROWCOUNT)&lt;/p&gt;
	&lt;p&gt;That's all, SIGN Returns the positive (+1), zero (0), or negative (-1) sign of the given expression. In this case -1 is not possible but the other two values are&lt;br&gt;
So let's see this in action&lt;/p&gt;
	&lt;p&gt;USE pubs&lt;br&gt;
GO&lt;/p&gt;
	&lt;p&gt;--Case Proc&lt;br&gt;
CREATE PROCEDURE TestReturnValues&lt;br&gt;
@au_id VARCHAR(49) ='172-32-1176'&lt;br&gt;
AS&lt;br&gt;
SELECT *&lt;br&gt;
FROM authors&lt;br&gt;
WHERE au_id =@au_id&lt;/p&gt;
	&lt;p&gt;RETURN CASE WHEN @@ROWCOUNT &gt; 0 THEN 1 ELSE 0 END&lt;br&gt;
GO&lt;/p&gt;
	&lt;p&gt;--Sign Proc&lt;br&gt;
CREATE PROCEDURE TestReturnValues2&lt;br&gt;
@au_id VARCHAR(49) ='172-32-1176'&lt;br&gt;
AS&lt;br&gt;
SELECT *&lt;br&gt;
FROM authors&lt;br&gt;
WHERE au_id =@au_id&lt;/p&gt;
	&lt;p&gt;RETURN SIGN(@@ROWCOUNT)&lt;br&gt;
GO&lt;/p&gt;
	&lt;p&gt;--Case Proc, 1 will be returned; default value is used&lt;br&gt;
DECLARE @Rowcount int&lt;br&gt;
EXEC @Rowcount = TestReturnValues&lt;br&gt;
SELECT @Rowcount&lt;br&gt;
GO&lt;/p&gt;
	&lt;p&gt;--Case Proc, 0 will be returned; dummy value is used&lt;br&gt;
DECLARE @Rowcount int&lt;br&gt;
EXEC @Rowcount = TestReturnValues 'ABC'&lt;br&gt;
SELECT @Rowcount&lt;br&gt;
GO&lt;/p&gt;
	&lt;p&gt;--Sign Proc, 1 will be returned; default value is used&lt;br&gt;
DECLARE @Rowcount int&lt;br&gt;
EXEC @Rowcount = TestReturnValues2&lt;br&gt;
SELECT @Rowcount&lt;br&gt;
GO&lt;/p&gt;
	&lt;p&gt;--Sign Proc, 0 will be returned; dummy value is used&lt;br&gt;
DECLARE @Rowcount int&lt;br&gt;
EXEC @Rowcount = TestReturnValues2 'ABC'&lt;br&gt;
SELECT @Rowcount&lt;br&gt;
GO&lt;/p&gt;
	&lt;p&gt;--Help the environment by recycling ;-)&lt;br&gt;
DROP PROCEDURE TestReturnValues2,TestReturnValues&lt;br&gt;
GO&lt;/p&gt;
	&lt;p&gt;COLUMNPROPERTY&lt;br&gt;
COLUMNPROPERTY is handy if you need to find scale, precision, if it is an identity column and more. I have listed all of them below&lt;/p&gt;
	&lt;p&gt;CREATE TABLE blah (ID DECIMAL(5,2) not null DEFAULT 99)&lt;br&gt;
INSERT blah DEFAULT VALUES&lt;/p&gt;
	&lt;p&gt;SELECT * FROM blah&lt;br&gt;
SELECT COLUMNPROPERTY( OBJECT_ID('blah'),'ID','AllowsNull') AS AllowsNull,&lt;br&gt;
COLUMNPROPERTY( OBJECT_ID('blah'),'ID','IsComputed') AS IsComputed,&lt;br&gt;
COLUMNPROPERTY( OBJECT_ID('blah'),'ID','IsCursorType') AS IsCursorType,&lt;br&gt;
COLUMNPROPERTY( OBJECT_ID('blah'),'ID','IsDeterministic') AS IsDeterministic,&lt;br&gt;
COLUMNPROPERTY( OBJECT_ID('blah'),'ID','IsFulltextIndexed') AS IsFulltextIndexed,&lt;br&gt;
COLUMNPROPERTY( OBJECT_ID('blah'),'ID','IsIdentity') AS IsFulltextIndexed,&lt;br&gt;
COLUMNPROPERTY( OBJECT_ID('blah'),'ID','IsIdNotForRepl') AS IsIdNotForRepl,&lt;br&gt;
COLUMNPROPERTY( OBJECT_ID('blah'),'ID','IsIndexable') AS IsIndexable,&lt;br&gt;
COLUMNPROPERTY( OBJECT_ID('blah'),'ID','IsOutParam') AS IsOutParam,&lt;br&gt;
COLUMNPROPERTY( OBJECT_ID('blah'),'ID','IsPrecise') AS IsPrecise,&lt;br&gt;
COLUMNPROPERTY( OBJECT_ID('blah'),'ID','IsRowGuidCol') AS IsRowGuidCol,&lt;br&gt;
COLUMNPROPERTY( OBJECT_ID('blah'),'ID','Precision') AS 'Precision',&lt;br&gt;
COLUMNPROPERTY( OBJECT_ID('blah'),'ID','Scale') AS Scale,&lt;br&gt;
COLUMNPROPERTY( OBJECT_ID('blah'),'ID','UsesAnsiTrim') AS UsesAnsiTrim&lt;br&gt;
FROM Blah&lt;/p&gt;
	&lt;p&gt;So what does all that stuff mean?&lt;/p&gt;
	&lt;p&gt;AllowsNull&lt;br&gt;
Allows null values. 1 = TRUE&lt;br&gt;
0 = FALSE&lt;br&gt;
NULL = Invalid input&lt;/p&gt;
	&lt;p&gt;IsComputed&lt;br&gt;
The column is a computed column. 1 = TRUE&lt;br&gt;
0 = FALSE&lt;br&gt;
NULL = Invalid input&lt;/p&gt;
	&lt;p&gt;IsCursorType&lt;br&gt;
The procedure parameter is of type CURSOR. 1 = TRUE&lt;br&gt;
0 = FALSE&lt;br&gt;
NULL = Invalid input&lt;/p&gt;
	&lt;p&gt;IsDeterministic&lt;br&gt;
The column is deterministic. This property applies only to computed columns and view columns. 1 = TRUE&lt;br&gt;
0 = FALSE&lt;br&gt;
NULL = Invalid input. Not a computed column or view column.&lt;/p&gt;
	&lt;p&gt;IsFulltextIndexed&lt;br&gt;
The column has been registered for full-text indexing. 1 = TRUE&lt;br&gt;
0 = FALSE&lt;br&gt;
NULL = Invalid input&lt;/p&gt;
	&lt;p&gt;IsIdentity&lt;br&gt;
The column uses the IDENTITY property. 1 = TRUE&lt;br&gt;
0 = FALSE&lt;br&gt;
NULL = Invalid input&lt;/p&gt;
	&lt;p&gt;IsIdNotForRepl&lt;br&gt;
The column checks for the IDENTITY_INSERT setting. If IDENTITY NOT FOR REPLICATION is specified, the IDENTITY_INSERT setting is not checked. 1 = TRUE&lt;br&gt;
0 = FALSE&lt;br&gt;
NULL = Invalid input&lt;/p&gt;
	&lt;p&gt;IsIndexable&lt;br&gt;
The column can be indexed. 1 = TRUE&lt;br&gt;
0 = FALSE&lt;br&gt;
NULL = Invalid input&lt;/p&gt;
	&lt;p&gt;IsOutParam&lt;br&gt;
The procedure parameter is an output parameter. 1 = TRUE&lt;br&gt;
0 = FALSE&lt;br&gt;
NULL = Invalid input&lt;/p&gt;
	&lt;p&gt;IsPrecise&lt;br&gt;
The column is precise. This property applies only to deterministic columns. 1 = TRUE&lt;br&gt;
0 = FALSE&lt;br&gt;
NULL = Invalid input. Not a deterministic column&lt;/p&gt;
	&lt;p&gt;IsRowGuidCol&lt;br&gt;
The column has the uniqueidentifier data type and is defined with the ROWGUIDCOL property. 1 = TRUE&lt;br&gt;
0 = FALSE&lt;br&gt;
NULL = Invalid input&lt;/p&gt;
	&lt;p&gt;Precision&lt;br&gt;
Precision for the data type of the column or parameter. The precision of the specified column data type&lt;br&gt;
NULL = Invalid input&lt;/p&gt;
	&lt;p&gt;Scale&lt;br&gt;
Scale for the data type of the column or parameter. The scale&lt;br&gt;
NULL = Invalid input&lt;/p&gt;
	&lt;p&gt;UsesAnsiTrim&lt;br&gt;
ANSI padding setting was ON when the table was initially created. 1= TRUE&lt;br&gt;
0= FALSE&lt;br&gt;
NULL = Invalid input&lt;/p&gt;
	&lt;p&gt;DATALENGTH&lt;br&gt;
Okay so you know the LEN function but do you know the DATALENGTH function? There are two major difference between LEN and DATALENGTH.&lt;br&gt;
The first one deals with trailing spaces, execute the following code and you will see that LEN returns 3 while DATALENGTH returns 4&lt;/p&gt;
	&lt;p&gt;DECLARE @V VARCHAR(50)&lt;br&gt;
SELECT @V ='ABC '&lt;br&gt;
SELECT LEN(@V),DATALENGTH(@V),@V&lt;/p&gt;
	&lt;p&gt;The second difference deals with unicode character data, as you know unicode uses 2 bytes to store 1 character&lt;br&gt;
Run the following example and you will see that LEN returns 3 while DATALENGTH returns 6&lt;br&gt;
DECLARE @V NVARCHAR(50)&lt;br&gt;
SELECT @V ='ABC'&lt;br&gt;
SELECT LEN(@V),DATALENGTH(@V),@V&lt;/p&gt;
	&lt;p&gt;If you do DATALENGTH(CONVERT(VARCHAR,@V)) you will get the same as LEN because LEN does a RTRIM and converts to VARCHAR before returning&lt;/p&gt;
	&lt;p&gt;ASCII, CHAR,UNICODE&lt;br&gt;
ASCII will give you the ascii code for a character so for A you will get 65&lt;br&gt;
CHAR does the reverse of ascii CHAR(65) returns A&lt;br&gt;
UNICODE will give you the unicode value for a character&lt;br&gt;
NCHAR will give you the character for a unicode or ascii value&lt;br&gt;
let's see how this works&lt;/p&gt;
	&lt;p&gt;SELECT ASCII('A'),CHAR(65),CHAR(ASCII('A')),&lt;br&gt;
UNICODE(N'Λ'),NCHAR(923),NCHAR(UNICODE(N'Λ'))&lt;/p&gt;
	&lt;p&gt;NULLIF&lt;br&gt;
NULLIF Returns a null value if the two specified expressions are equivalent.&lt;/p&gt;
	&lt;p&gt;Syntax&lt;br&gt;
NULLIF ( expression , expression )&lt;/p&gt;
	&lt;p&gt;DECLARE @v VARCHAR(20)&lt;br&gt;
SELECT @v = ' '&lt;/p&gt;
	&lt;p&gt;SELECT NULLIF(@v,' ')&lt;/p&gt;
	&lt;p&gt;You can combine NULLIF with COALESCE if you want to test for NULLS and Blanks for example&lt;/p&gt;
	&lt;p&gt;DECLARE @v VARCHAR(20)&lt;br&gt;
SELECT @v = ' '&lt;/p&gt;
	&lt;p&gt;SELECT COALESCE(NULLIF(@v,' '),'N/A')&lt;/p&gt;
	&lt;p&gt;Here is another NULLIF example:&lt;br&gt;
CREATE TABLE Blah (SomeCol VARCHAR(33))&lt;/p&gt;
	&lt;p&gt;INSERT Blah VALUES(NULL)&lt;br&gt;
INSERT Blah VALUES('')&lt;br&gt;
INSERT Blah VALUES(' ')&lt;br&gt;
INSERT Blah VALUES('A')&lt;br&gt;
INSERT Blah VALUES('B B')&lt;/p&gt;
	&lt;p&gt;--Using COALESCE and NULLIF&lt;br&gt;
SELECT COALESCE(NULLIF(RTRIM(SomeCol),' '),'N/A')&lt;br&gt;
FROM Blah&lt;/p&gt;
	&lt;p&gt;--Using CASE&lt;br&gt;
SELECT CASE WHEN RTRIM(SomeCol) = '' THEN 'N/A'&lt;br&gt;
WHEN RTRIM(SomeCol) IS NULL THEN 'N/A'&lt;br&gt;
ELSE SomeCol END SomeCol&lt;br&gt;
FROM Blah&lt;/p&gt;
	&lt;p&gt;Output for both queries&lt;br&gt;
-----------------------&lt;br&gt;
N/A&lt;br&gt;
N/A&lt;br&gt;
N/A&lt;br&gt;
A&lt;br&gt;
B B&lt;/p&gt;
	&lt;p&gt;PARSENAME&lt;br&gt;
PARSENAME retrieves parts of string delimited by dots. It is used to split DataBaseServer, DataBaseName, ObjectOwner and ObjectName but you can use it to split IP addresses, names etc&lt;/p&gt;
	&lt;p&gt;DECLARE @ParseString VARCHAR(100)&lt;br&gt;
SELECT @ParseString = 'DataBaseServer.DataBaseName.ObjectOwner.ObjectName'&lt;/p&gt;
	&lt;p&gt;SELECT PARSENAME(@ParseString,4),&lt;br&gt;
PARSENAME(@ParseString,3),&lt;br&gt;
PARSENAME(@ParseString,2),&lt;br&gt;
PARSENAME(@ParseString,1)&lt;/p&gt;
	&lt;p&gt;CREATE TABLE #Test (&lt;br&gt;
SomeField VARCHAR(49))&lt;/p&gt;
	&lt;p&gt;INSERT INTO #Test&lt;br&gt;
VALUES ('aaa-bbbbb')&lt;/p&gt;
	&lt;p&gt;INSERT INTO #Test&lt;br&gt;
VALUES ('ppppp-bbbbb')&lt;/p&gt;
	&lt;p&gt;INSERT INTO #Test&lt;br&gt;
VALUES ('zzzz-xxxxx')&lt;/p&gt;
	&lt;p&gt;--using PARSENAME&lt;br&gt;
SELECT PARSENAME(REPLACE(SomeField,'-','.'),2)&lt;br&gt;
FROM #Test&lt;/p&gt;
	&lt;p&gt;Another example:&lt;/p&gt;
	&lt;p&gt;CREATE TABLE BadData (FullName varchar(20) NOT NULL);&lt;br&gt;
INSERT INTO BadData (FullName)&lt;br&gt;
SELECT 'Clinton, Bill' UNION ALL&lt;br&gt;
SELECT 'Johnson, Lyndon, B.' UNION ALL&lt;br&gt;
SELECT 'Bush, George, H.W.';&lt;/p&gt;
	&lt;p&gt;Split the names into 3 columns&lt;/p&gt;
	&lt;p&gt;Your output should be this:&lt;br&gt;
LastName FirstName MiddleInitial&lt;br&gt;
Clinton Bill&lt;br&gt;
Johnson Lyndon B.&lt;br&gt;
Bush George H.W.&lt;/p&gt;
	&lt;p&gt;SELECT FullName,PARSENAME(FullName2,NameLen+1) AS LastName,&lt;br&gt;
PARSENAME(FullName2,NameLen) AS FirstName,&lt;br&gt;
COALESCE(REPLACE(PARSENAME(FullName2,NameLen-1),'~','.'),'') AS MiddleInitial&lt;br&gt;
FROM(&lt;br&gt;
SELECT LEN(FullName) -LEN(REPLACE(FullName,',','')) AS NameLen,&lt;br&gt;
REPLACE(REPLACE(FullName,'.','~'),', ','.') AS FullName2,FullName&lt;br&gt;
FROM BadData) x&lt;/p&gt;
	&lt;p&gt;STUFF&lt;br&gt;
STUFF is another function that is hardly used, it is useful if you want to replace or add characters inside data&lt;br&gt;
Take a look at the code below. the first STUFF will replace X with 98765, the second STUFF will place 98765 before the X and the third stuff will replace X- with 98765&lt;br&gt;
DECLARE @v VARCHAR(11)&lt;br&gt;
SELECT @v ='-X-'&lt;/p&gt;
	&lt;p&gt;SELECT STUFF(@v, 2, 1, '98765'),&lt;br&gt;
STUFF(@v, 2, 0, '98765'),&lt;br&gt;
STUFF(@v, 2, 2, '98765')&lt;/p&gt;
	&lt;p&gt;The STUFF function is very handy if you need to insert dashes in a social security. You can accomplish that by using the function STUFF twice instead of using substring,left and right&lt;/p&gt;
	&lt;p&gt;DECLARE @v VARCHAR(11)&lt;br&gt;
SELECT @v ='123456789'&lt;/p&gt;
	&lt;p&gt;SELECT @v,STUFF(STUFF(@v,4,0,'-'),7,0,'-')&lt;/p&gt;
	&lt;p&gt;REVERSE&lt;br&gt;
REVERSE just reverses the value, for example the code below returns CBA&lt;/p&gt;
	&lt;p&gt;SELECT REVERSE('ABC')&lt;/p&gt;
	&lt;p&gt;Reverse is handy if you need to split values, take a look at this example&lt;/p&gt;
	&lt;p&gt;CREATE TABLE #TestCityStateZip (csz CHAR(49))&lt;br&gt;
INSERT INTO #TestCityStateZip VALUES ('city ,st 12223')&lt;br&gt;
INSERT INTO #TestCityStateZip VALUES ('New York City,NY 10028')&lt;br&gt;
INSERT INTO #TestCityStateZip VALUES ('Princeton , NJ 08536')&lt;br&gt;
INSERT INTO #TestCityStateZip VALUES ('Princeton,NJ 08536 ')&lt;br&gt;
INSERT INTO #TestCityStateZip VALUES ('Long Island City, NY 10013')&lt;br&gt;
INSERT INTO #TestCityStateZip VALUES ('Long Island City, NY 10013 ')&lt;br&gt;
INSERT INTO #TestCityStateZip VALUES ('Long Island City , NY 10013')&lt;br&gt;
INSERT INTO #TestCityStateZip VALUES ('Long Island City ,NY 10013 ')&lt;/p&gt;
	&lt;p&gt;SELECT LEFT(csz,CHARINDEX(',',csz)-1)AS City,&lt;br&gt;
LEFT(LTRIM(SUBSTRING(csz,(CHARINDEX(',',csz)+1),4)),2) AS State,&lt;br&gt;
RIGHT(RTRIM(csz),CHARINDEX(' ',REVERSE(RTRIM(csz)))-1) AS Zip&lt;br&gt;
FROM #TestCityStateZip&lt;/p&gt;
	&lt;p&gt;&lt;strong&gt;GETUTCDATE&lt;br&gt;
SELECT GETUTCDATE()&lt;/strong&gt;&lt;/p&gt;
	&lt;p&gt;Returns the datetime value representing the current UTC time (Universal Time Coordinate or Greenwich Mean Time). The current UTC time is derived from the current local time and the time zone setting in the operating system of the computer on which SQL Server is running.&lt;/p&gt;
&lt;p&gt; &lt;small&gt; &lt;a href="http://sscheralbidar.blog.co.uk/2007/09/21/rarely_used_sql_server_functions~3017668/#comments"&gt;Comments&lt;/a&gt; &lt;/small&gt; &lt;/p&gt;</content></entry><entry><id>tag:sscheralbidar.blog.co.uk,2007-08-31:/2007/08/31/formating_number_in_vb_net~2898925/</id><title>Formating Number in VB.NET</title><link rel="alternate" type="text/html" href="http://sscheralbidar.blog.co.uk/2007/08/31/formating_number_in_vb_net~2898925/"/><author><name>sscheral</name></author><published>2007-08-31T05:37:43+02:00</published><updated>2007-08-31T05:37:43+02:00</updated><content type="html">	&lt;p&gt;&lt;strong&gt;Display Formats&lt;/strong&gt;&lt;br&gt;Information stored in variables as various data types may not be in a format for visual presentation on a Web page. Visual Basic provides several built-in functions for presentation of this data.&lt;br&gt;Formatting Currency&lt;br&gt;The FormatCurrency() function formats numeric data for presentation as dollars and cents. Its general format is shown below.&lt;br&gt;FormatCurrency(value [, trailing digits] [, leading digit] [, parentheses] [, group digits]) &lt;br&gt;value is any expression that produces a number;&lt;/p&gt;
	&lt;p&gt;trailing digits is an integer giving the number of digits following the decimal point; the default is rounding to 2 digits;&lt;/p&gt;
	&lt;p&gt;leading digit is True or False to indication whether a leading 0 is to appear before the decimal point for fractional values;&lt;/p&gt;
	&lt;p&gt;parentheses is True or False to indicate whether negative numbers should be displayed inside parentheses;&lt;/p&gt;
	&lt;p&gt;group digits is True or False to indicate whether numbers should be grouped between commas. &lt;br&gt;Format Output&lt;br&gt;FormatCurrency(12345.6789) $12,345.68&lt;br&gt;FormatCurrency(12345.6789, 4) $12,345.6789&lt;br&gt;FormatCurrency(12345.6789,,,,False) $12345.68&lt;br&gt;FormatCurrency(-12345.6789) ($12,345.68)&lt;br&gt;FormatCurrency(-12345.6789,,,False) -$12,345.68&lt;br&gt;FormatCurrency(.6789) $0.68&lt;br&gt;FormatCurrency(.6789,,False) $.68&lt;br&gt;FormatCurrency(-.6789,,False,False) -$.68&lt;br&gt;Formatting Numbers&lt;br&gt;The FormatNumber() function returns a value formatted as a number. Its general format is shown below.&lt;br&gt;FormatNumber(value [, trailing digits] [, leading digit] [, parentheses] [, group digits]) &lt;br&gt;value is any expression that produces a number;&lt;/p&gt;
	&lt;p&gt;trailing digits is an integer giving the number of digits following the decimal point; the default is rounding to 2 digits;&lt;/p&gt;
	&lt;p&gt;leading digit is True or False to indication whether a leading 0 is to appear before the decimal point for fractional values;&lt;/p&gt;
	&lt;p&gt;parentheses is True or False to indicate whether negative numbers should be displayed inside parentheses;&lt;/p&gt;
	&lt;p&gt;group digits is True or False to indicate whether numbers should be grouped between commas. &lt;br&gt;Format Output&lt;br&gt;FormatNumber(12345.6789) 12,345.68&lt;br&gt;FormatNumber(12345.6789,5) 12,345.67890&lt;br&gt;FormatNumber(12345.6789,,,,False) 12345.68&lt;br&gt;FormatNumber(-12345.6789) -12,345.68&lt;br&gt;FormatNumber(-12345.6789,,,True) (12,345.68)&lt;br&gt;FormatNumber(.6789) 0.68&lt;br&gt;FormatNumber(.6789,,False) .68&lt;br&gt;FormatNumber(-.6789,4) -0.6789&lt;br&gt;Formatting Percentages&lt;br&gt;The FormatPercent() function returns a value formatted as a percentage, that is, multiplied by 100 with a trailing % character. Its general format is shown below.&lt;br&gt;FormatPercent(value [, trailing digits] [, leading digit] [, parentheses] [, group digits]) &lt;br&gt;value is any expression that produces a number;&lt;/p&gt;
	&lt;p&gt;trailing digits is an integer giving the number of digits following the decimal point; the default is rounding to 2 digits;&lt;/p&gt;
	&lt;p&gt;leading digit is True or False to indication whether a leading 0 is to appear before the decimal point for fractional values;&lt;/p&gt;
	&lt;p&gt;parentheses is True or False to indicate whether negative numbers should be displayed inside parentheses;&lt;/p&gt;
	&lt;p&gt;group digits is True or False to indicate whether numbers should be grouped between commas. &lt;br&gt;Format Output&lt;br&gt;FormatPercent(.6789) 67.89%&lt;br&gt;FormatPercent(.6789,4) 67.8900%&lt;br&gt;FormatPercent(-.6789) -67.89%&lt;br&gt;FormatPercent(-.6789,,,True) (67.89%)&lt;br&gt;Formatting Dates and Times&lt;br&gt;The FormatDateTime() function returns a string expression representing a date/time value. Its general format is&lt;br&gt;FormatDateTime(value [, DateFormat.format]) &lt;br&gt;where value is a date or time value and format is one of the following values: GeneralDate, LongDate, ShortDate, LongTime, or ShortTime.&lt;br&gt;Format Output&lt;br&gt;FormatDateTime(Now) 8/22/2007 4:50:38 AM&lt;br&gt;FormatDateTime(Today) 8/22/2007&lt;br&gt;FormatDateTime(TimeOfDay) 4:50:38 AM&lt;br&gt;FormatDateTime(Now,DateFormat.LongDate) Wednesday, August 22, 2007&lt;br&gt;FormatDateTime(Today,DateFormat.LongDate) Wednesday, August 22, 2007&lt;br&gt;FormatDateTime(Now,DateFormat.ShortDate) 8/22/2007&lt;br&gt;FormatDateTime(Today,DateFormat.ShortDate) 8/22/2007&lt;br&gt;FormatDateTime(Now,DateFormat.LongTime) 4:50:38 AM&lt;br&gt;FormatDateTime(TimeOfDay,DateFormat.LongTime) 4:50:38 AM&lt;br&gt;FormatDateTime(Now,DateFormat.ShortTime) 04:50&lt;br&gt;FormatDateTime(TimeOfDay,DateFormat.ShortTime) 04:50&lt;br&gt;The Format() Function&lt;br&gt;The Format() function is a general-purpose formatting function that returns a string value formatted according to a format string. The format strings duplicate numeric and date/time formats produced by the specialized formats described above. The general format for applying the Format() function is shown below.&lt;br&gt;Format(value, "format string") &lt;br&gt;Formatting Numbers&lt;br&gt;A format string for numeric values can use one of the predefined string values shown in the following table.&lt;br&gt;String Description&lt;br&gt;General Number|G|g Displays number with no thousand separator.&lt;br&gt;Currency|C|c Displays number with thousand separator, if appropriate; display two digits to the right of the decimal separator.&lt;br&gt;Fixed|F|f Displays at least one digit to the left and two digits to the right of the decimal separator.&lt;br&gt;Standard|N|n Displays number with thousand separator, at least one digit to the left and two digits to the right of the decimal separator.&lt;br&gt;Percent Displays number multiplied by 100 with a percent sign (%) appended immediately to the right; always displays two digits to the right of the decimal separator.&lt;br&gt;P|p Displays number with thousandths separator multiplied by 100 with a percent sign (%) appended to the right and separated by a single space; always displays two digits to the right of the decimal separator.&lt;br&gt;Examples of applying a format string to numeric values are shown in the following table.&lt;br&gt;Format Output&lt;br&gt;Format(12345.6789,"g") 12345.6789&lt;br&gt;Format(12345.6789,"c") $12,345.68&lt;br&gt;Format(12345.6789,"f") 12345.68&lt;br&gt;Format(12345.6789,"n") 12,345.68&lt;br&gt;Format(-12345.6789,"g") -12345.6789&lt;br&gt;Format(-12345.6789,"c") ($12,345.68)&lt;br&gt;Format(-12345.6789,"f") -12345.68&lt;br&gt;Format(-12345.6789,"n") -12,345.68&lt;br&gt;Format(.6789,"Percent") 67.89%&lt;br&gt;Format(.6789,"p") 67.89 %&lt;br&gt;Format(-.6789,"Percent") -67.89%&lt;br&gt;Format(-.6789,"p") -67.89 %&lt;br&gt;Formatting Dates and Times&lt;br&gt;A format string for date/time values can use one of the predefined string values shown in the following table.&lt;br&gt;String Description&lt;br&gt;Long Date|D Displays a date in long date format.&lt;br&gt;Short Date|d Displays a date in short date format.&lt;br&gt;Long Time|T Displays a date in long date format.&lt;br&gt;Short Time|t Displays a date in short date format.&lt;br&gt;F Displays the long date and long time.&lt;br&gt;f Displays the long date and short time.&lt;br&gt;g Displays the short date and short time.&lt;br&gt;M|m Displays the month and the day of a date.&lt;br&gt;Y|y Formats the date as the year and month.&lt;br&gt;Examples of applying a format string to date/time values are shown in the following table.&lt;br&gt;Format Output&lt;br&gt;Format(Now,"D") Wednesday, August 22, 2007&lt;br&gt;Format(Now,"d") 8/22/2007&lt;br&gt;Format(Now,"T") 4:50:38 AM&lt;br&gt;Format(Now,"t") 4:50 AM&lt;br&gt;Format(Now,"F") Wednesday, August 22, 2007 4:50:38 AM&lt;br&gt;Format(Now,"f") Wednesday, August 22, 2007 4:50 AM&lt;br&gt;Format(Now,"g") 8/22/2007 4:50 AM&lt;br&gt;Format(Now,"m") August 22&lt;br&gt;Format(Now,"y") August, 2007&lt;br&gt;User-defined Numeric Formats&lt;br&gt;Formats can be defined for displaying numeric values by composing a string to describe the format. This user-defined string is applied through the Format() function. The characters shown in the following table are used to compose the format string.&lt;br&gt;Character Description&lt;br&gt;0 Digit placeholder. Displays a digit or a zero. If the value has a digit in the position, then it displays; otherwise, a zero is displayed.&lt;br&gt;# Digit placeholder. Displays a digit or a space. If the value has a digit in the position, then it displays; otherwise, a space is displayed.&lt;br&gt;. Decimal placeholder; determines how many digits are displayed to the left and right of the decimal separator.&lt;br&gt;, Thousand separator; separates thousands from hundreds within a number that has four or more places to the left of the decimal separator. Only a single "," is required in the format, between the first set of digit placeholders.&lt;br&gt;% Percent placeholder. Multiplies the expression by 100. The percent character (%) is inserted in the position where it appears in the format string.&lt;br&gt;- + $ ( ) Literal characters; displayed exactly as typed in the format string.&lt;br&gt;Examples of applying a user-defined format string to numeric values are shown in the following table.&lt;br&gt;Format Output&lt;br&gt;Format(012345.6789,"0.00") 12345.68&lt;br&gt;Format(012345.6789,"0,0.000") 12,345.679&lt;br&gt;Format(012345.6789,"00000,0.000000") 012,345.678900&lt;br&gt;Format(012345.6789,"#.&lt;tt&gt;") 12345.68&lt;br&gt;Format(012345.6789,"#,#.&lt;/tt&gt;") 12,345.68&lt;br&gt;Format(012345.6789,"$ #,#.&lt;tt&gt;") $ 12,345.68&lt;br&gt;Format(-012345.6789,"#,#.&lt;/tt&gt;&lt;tt&gt;") -12,345.6789&lt;br&gt;Format(-012345.6789,"$#,#.&lt;/tt&gt;") -$12,345.68&lt;br&gt;Format(.6789,"#,#.##") .68&lt;br&gt;Format(.6789,"0,0.000") 00.679&lt;br&gt;Format(-.6789," 0.0000") - 0.6789&lt;br&gt;Format(.6789,"0.00%") 67.89%&lt;br&gt;User-defined Date/Time Formats&lt;br&gt;Formats can be defined for displaying date and time values by composing a string to describe the format. This user-defined string is applied through the Format() function. The characters shown in the following table are used to compose the date/time format string.&lt;br&gt;Character Description&lt;br&gt;: Time separator.&lt;br&gt;/ - Date separators.&lt;br&gt;% Precedes a single-character format string.&lt;br&gt;d Displays the day as a number without a leading zero.&lt;br&gt;dd Displays the day as a number with a leading zero.&lt;br&gt;ddd Displays the day name as an abbreviation.&lt;br&gt;dddd Displays the day as a full name.&lt;br&gt;M Displays the month as a number without a leading zero.&lt;br&gt;MM Displays the month as a number with a leading zero.&lt;br&gt;MMM Displays the month name as an abbreviation.&lt;br&gt;MMMM Displays the month as a full name.&lt;br&gt;yy Displays the year in two-digit format.&lt;br&gt;yyyy Displays the year in four-digit format.&lt;br&gt;h Displays the hour as a number without leading zeros using the 12-hour clock.&lt;br&gt;hh Displays the hour as a number with leading zeros using the 12-hour clock.&lt;br&gt;H Displays the hour as a number without leading zeros using the 24-hour clock.&lt;br&gt;HH Displays the hour as a number with leading zeros using the 24-hour clock.&lt;br&gt;m Displays the minute as a number without leading zeros.&lt;br&gt;mm Displays the minute as a number with leading zeros.&lt;br&gt;s Displays the seconds as a number without leading zeros.&lt;br&gt;ss Displays the seconds as a number with leading zeros.&lt;br&gt;f... Displays fractions of seconds using up to 7 characters to display fractional digits.&lt;br&gt;tt Uses the 12-hour clock and displays an uppercase AM with any hour before noon; displays an uppercase PM with any hour between noon and 11:59 P.M.&lt;br&gt;Additional characters and punctuation marks can be used within the format string. Characters that match any of the formatting characters must be preceded by "\".&lt;br&gt;Examples of applying a user-defined format string to date/time values are shown in the following table.&lt;br&gt;Format Output&lt;br&gt;Format(Now,"M/d/yy") 8/22/07&lt;br&gt;Format(Now,"M-d-yyyy") 8-22-2007&lt;br&gt;Format(Now,"d-MMMM-yy") 22-August-07&lt;br&gt;Format(Now,"d MMMM, yyyy") 22 August, 2007&lt;br&gt;Format(Now,"MMMM d, yyyy") August 22, 2007&lt;br&gt;Format(Now,"MMMM, yyyy") August, 2007&lt;br&gt;Format(Now,"%d") 22&lt;br&gt;Format(Now,"h:m tt") 4:50 AM&lt;br&gt;Format(Now,"h:m:ss tt") 4:50:38 AM&lt;br&gt;Format(Now,"H:m") 4:50&lt;br&gt;Format(Now,"M/d/yy - h:mtt") 8/22/07 - 4:50AM&lt;br&gt;Format(Now,"H:m:ss.fffffff") 4:50:38.1853725&lt;br&gt;Format(Now,"To\da\y i\s MMMM d, yyyy.") Today is August 22, 2007.&lt;/p&gt;
&lt;p&gt; &lt;small&gt; &lt;a href="http://sscheralbidar.blog.co.uk/2007/08/31/formating_number_in_vb_net~2898925/#comments"&gt;Comments&lt;/a&gt; &lt;/small&gt; &lt;/p&gt;</content></entry><entry><id>tag:sscheralbidar.blog.co.uk,2007-06-29:/2007/06/29/know_your_identity~2541664/</id><title>Know your identity</title><link rel="alternate" type="text/html" href="http://sscheralbidar.blog.co.uk/2007/06/29/know_your_identity~2541664/"/><author><name>sscheral</name></author><published>2007-06-29T12:10:54+02:00</published><updated>2007-06-29T12:10:54+02:00</updated><content type="html">	&lt;p&gt;Know your Identity&lt;/p&gt;
	&lt;p&gt;Often we create tables that have identity columns. An identity column is a special column in SQL Server that can automatically take on values when an insert operation is done on the table. What if you want to find out whether a table contains an identity column?? SQL Server stores this information in the syscolumns table, but you can access this information in many ways. In this article, we will see the various ways to access information about whether a table has an identity column.&lt;/p&gt;
	&lt;p&gt;Before we start off with the methods, let's first create a table that has an identity column, like this:&lt;/p&gt;
	&lt;p&gt;CREATE TABLE samp_test&lt;br&gt;
(&lt;br&gt;
	colA	INT IDENTITY,&lt;br&gt;
	colB	VARCHAR(10)&lt;br&gt;
)&lt;/p&gt;
	&lt;p&gt;Method A&lt;/p&gt;
	&lt;p&gt;In the first method, we will fetch this information from the syscolumns table. This table contains a column called status that contains various values. If the value in this column contains hexadecimal 80, then it means that the corresponding column contains an identity column. Here is how we can check this:&lt;/p&gt;
	&lt;p&gt;IF (EXISTS (SELECT name FROM syscolumns WHERE&lt;br&gt;
	OBJECT_NAME(id) = 'samp_test' AND status = 0x80))&lt;br&gt;
	PRINT 'Table has an identity column'&lt;br&gt;
ELSE&lt;br&gt;
	PRINT 'Table does not have an identity column'&lt;/p&gt;
	&lt;p&gt;Method B&lt;/p&gt;
	&lt;p&gt;In the second method, we will see whether a table has an identity column using the COLUMNPROPERTY function. This function takes an argument IsIdentity that indicates whether the column is an identity column. Here is how we can do the check using this function:&lt;/p&gt;
	&lt;p&gt;IF (EXISTS (SELECT myTemp.IsIdentity FROM&lt;br&gt;
	(SELECT IsIdentity=COLUMNPROPERTY(id, name, 'IsIdentity')&lt;br&gt;
		FROM syscolumns WHERE OBJECT_NAME(id) = 'samp_test') AS myTemp&lt;br&gt;
	WHERE myTemp.IsIdentity = 1))&lt;br&gt;
	PRINT 'Table has an identity column'&lt;br&gt;
ELSE&lt;br&gt;
	PRINT 'Table does not have an identity column'&lt;/p&gt;
	&lt;p&gt;This method is slightly convoluted since the COLUMNPROPERTY function takes the name of the column as the parameter. Thus, if we want to make a generic sort of a call, we need to iterate through each column of a table using the syscolumns table and then make a call to the COLUMNPROPERTY function. Finally we then run an EXISTS check over this derived table to check for identity.&lt;/p&gt;
	&lt;p&gt;Method C&lt;/p&gt;
	&lt;p&gt;In the final method, we will use the OBJECTPROPERTY function to check whether a table has an identity column. Here is how we can do the check using this function:&lt;/p&gt;
	&lt;p&gt;IF (OBJECTPROPERTY(OBJECT_ID('samp_test'), 'TableHasIdentity') = 1)&lt;br&gt;
	PRINT 'Table has an identity column'&lt;br&gt;
ELSE&lt;br&gt;
	PRINT 'Table does not have an identity column'&lt;/p&gt;
	&lt;p&gt;This is the easiest of the lot. The OBJECTPROPERTY function takes as parameter the name of the object to check (a table in this case) and then the name of the property. We use the OBJECT_ID function to get the ID of the object. We then use the TableHasIdentity property to check if the table has an identity column. One difference between this method and the others is that this method does not give the name of the column which is the identity column, which is possible using the other two methods.&lt;/p&gt;
&lt;p&gt; &lt;small&gt; &lt;a href="http://sscheralbidar.blog.co.uk/2007/06/29/know_your_identity~2541664/#comments"&gt;Comments&lt;/a&gt; &lt;/small&gt; &lt;/p&gt;</content></entry><entry><id>tag:sscheralbidar.blog.co.uk,2007-06-29:/2007/06/29/sql_date_time~2541650/</id><title>SQL Date Time</title><link rel="alternate" type="text/html" href="http://sscheralbidar.blog.co.uk/2007/06/29/sql_date_time~2541650/"/><author><name>sscheral</name></author><published>2007-06-29T12:07:50+02:00</published><updated>2007-09-21T12:20:51+02:00</updated><content type="html">	&lt;p&gt;DECLARE @Date datetime&lt;br&gt;
SET @Date = '2001/08/31'&lt;br&gt;
SELECT DATEADD(dd,-(DATEPART(dw, @Date) - 1),@Date) AS 'First day of the week'&lt;br&gt;
SELECT DATEADD(dd,-(DATEPART(dw, @Date) - 7),@Date) AS 'Last day of the week'&lt;br&gt;
SELECT DAY(DATEADD(d, -DAY(DATEADD(m,1,@Date)),DATEADD(m,1,@Date))) AS 'Last day of the month' &lt;/p&gt;
	&lt;p&gt;==============================================================================================================&lt;/p&gt;
	&lt;p&gt;--To get the first day of month and day name by any date&lt;br&gt;
Declare @pInputDate Datetime&lt;br&gt;
set @pInputDate=getdate()- 40&lt;br&gt;
set @pInputDate= CAST(CAST(YEAR(@pInputDate) AS VARCHAR(4)) + '/' +&lt;br&gt;
               CAST(MONTH(@pInputDate) AS VARCHAR(2)) + '/01' AS DATETIME)&lt;br&gt;
--Will give First date of the month&lt;/p&gt;
	&lt;p&gt;--Will give day of the date&lt;br&gt;
select @pInputDate AS First_Day_of_mth, datename(dw,@pInputDate) AS Day_Name&lt;br&gt;
==============================================================================================================&lt;br&gt;
Hi,&lt;/p&gt;
	&lt;p&gt;     here is the Table:&lt;/p&gt;
	&lt;p&gt;Id    Saledate            Sales&lt;br&gt;
1    2 Jan 2006          100&lt;br&gt;
1    13 Jan 2006        200&lt;br&gt;
1    14 Feb 2006        300&lt;br&gt;
2    3 Feb 2006          150&lt;br&gt;
2    4 Feb 2006           200&lt;br&gt;
3    5 Jan 2006           300&lt;br&gt;
3    13 Feb 2006         400&lt;/p&gt;
	&lt;p&gt;         I need the following output?&lt;/p&gt;
	&lt;p&gt;Id       Jan     Feb     Mar    Apr&lt;br&gt;
1        300     300      0        0&lt;br&gt;
2         0         350      0        0&lt;br&gt;
3        300     400      0       0&lt;/p&gt;
	&lt;p&gt;--Here is the result:&lt;/p&gt;
	&lt;p&gt;SELECT ID, SUM(CASE WHEN MONTH(saledate) = 1 THEN sales ELSE 0 END) AS 'Jan',&lt;br&gt;
   SUM(CASE WHEN MONTH(saledate) = 2 THEN ISNULL(sales,0) ELSE 0 END) AS 'Feb',&lt;br&gt;
   SUM(CASE WHEN MONTH(saledate) = 3 THEN ISNULL(sales,0) ELSE 0 END) AS 'Mar',&lt;br&gt;
   SUM(CASE WHEN MONTH(saledate) = 4 THEN ISNULL(sales,0) ELSE 0 END) AS 'Apr'&lt;br&gt;
FROM sale&lt;br&gt;
GROUP BY ID&lt;br&gt;
--Here it is shown only for 4 months. You can extend this query for complete year.&lt;/p&gt;
	&lt;p&gt;-- It gives maximum sales in each month&lt;br&gt;
select id as ID ,&lt;br&gt;
MAX(case when month(saledate) =1 then isnull(sales,0) else 0 end) as 'Jan',&lt;br&gt;
MAX(case when month(saledate) = 2 then isnull(sales,0) else 0 end) as 'Feb',&lt;br&gt;
Max(case when month(saledate) = 3 then isnull(sales,0) else 0 end) as 'Mar',&lt;br&gt;
Max(case when month(saledate) = 4 then isnull(sales,0) else 0 end) as 'Apr',&lt;br&gt;
Max(case when month(saledate) = 5 then isnull(sales,0) else 0 end) as 'May',&lt;br&gt;
Max(case when month(saledate) = 6 then isnull(sales,0) else 0 end) as 'June',&lt;br&gt;
Max(case when month(saledate) = 7 then isnull(sales,0) else 0 end) as 'July',&lt;br&gt;
Max(case when month(saledate) = 8 then isnull(sales,0) else 0 end) as 'Aug',&lt;br&gt;
Max(case when month(saledate) = 9 then isnull(sales,0) else 0 end) as 'Sept',&lt;br&gt;
Max(case when month(saledate) = 10 then isnull(sales,0) else 0 end) as 'Oct',&lt;br&gt;
Max(case when month(saledate) = 11 then isnull(sales,0) else 0 end) as 'Nov',&lt;br&gt;
Max(case when month(saledate) = 12 then isnull(sales,0) else 0 end) as 'Dec'&lt;br&gt;
from sale&lt;br&gt;
group by month(saledate),id&lt;/p&gt;
	&lt;p&gt;SELECT&lt;br&gt;
    DAY('5/5/2007'),&lt;br&gt;
    MONTH('5/1/2007'),&lt;br&gt;
    YEAR('5/1/2007'),&lt;br&gt;
    DATEPART(DAY, '1/5/2007'),&lt;br&gt;
    DATEPART(MONTH, '5/1/2007'),&lt;br&gt;
    DATEPART(YEAR, '1/1/2007')&lt;/p&gt;
	&lt;p&gt;Day         MOnth       year        datepart_Day datepart_MOnth datepart_year&lt;br&gt;
======-- ======-- ======-- ======--- ======----- ======----&lt;br&gt;
5           5           2007        5            5              2007&lt;/p&gt;
	&lt;p&gt;SELECT&lt;br&gt;
    GETDATE() AS local_date,&lt;br&gt;
    GETUTCDATE() AS UTC_date&lt;/p&gt;
	&lt;p&gt;local_date              UTC_date&lt;br&gt;
============----- ============-----&lt;br&gt;
2007-05-08 19:12:50.687 2007-05-08 13:42:50.687&lt;/p&gt;
	&lt;p&gt;SELECT DATEADD(MONTH, 6, GETDATE())AS '6_months_from_now'&lt;/p&gt;
	&lt;p&gt;6_months_from_now&lt;br&gt;
============-----&lt;br&gt;
2007-11-08 19:12:50.687&lt;br&gt;
==============================================================================================================&lt;br&gt;
declare @datevar datetime&lt;br&gt;
select @datevar = getdate()&lt;/p&gt;
	&lt;p&gt;/*Example for getdate() : getting current datetime*/&lt;br&gt;
select getdate() [Current Datetime]&lt;/p&gt;
	&lt;p&gt;/*Example for dateadd : getting date 7 days from current datetime*/&lt;br&gt;
select dateadd(dd, 7, @datevar) [Date 7 days from now]&lt;/p&gt;
	&lt;p&gt;/*Example for datediff : getting no of days passed since 01-01-2004*/&lt;br&gt;
select datediff(dd,'20040101',@datevar) [No of days since 01-01-2004]&lt;/p&gt;
	&lt;p&gt;/*Example for datename : getting month name*/&lt;br&gt;
select datename(mm, @datevar) [Month Name]&lt;/p&gt;
	&lt;p&gt;/*Example for datepart : getting week from date*/&lt;br&gt;
select datepart(wk, @datevar ) [Week No] &lt;/p&gt;
	&lt;p&gt;/*Example for day : getting day part of date*/&lt;br&gt;
select day (@datevar) [Day]&lt;/p&gt;
	&lt;p&gt;/*Example for month : getting month part of date*/&lt;br&gt;
select month(@datevar) [Month]&lt;/p&gt;
	&lt;p&gt;/*Example for year : getting year part of date*/&lt;br&gt;
select year(@datevar) [Year] &lt;/p&gt;
	&lt;p&gt;/* Getting the Day Name like monday tuesday... */&lt;br&gt;
SELECT DATENAME(dw, GETDATE())&lt;br&gt;
-- or&lt;br&gt;
/* 0-monday,1-tuesday,2-wednesday ....7-monday,8-tuesday... */&lt;br&gt;
SELECT DATENAME(dw, 0)&lt;/p&gt;
	&lt;p&gt;==============================================================================================================&lt;/p&gt;
&lt;p&gt; &lt;small&gt; &lt;a href="http://sscheralbidar.blog.co.uk/2007/06/29/sql_date_time~2541650/#comments"&gt;Comments&lt;/a&gt; &lt;/small&gt; &lt;/p&gt;</content></entry><entry><id>tag:sscheralbidar.blog.co.uk,2007-05-04:/2007/05/04/links~2208631/</id><title>Links</title><link rel="alternate" type="text/html" href="http://sscheralbidar.blog.co.uk/2007/05/04/links~2208631/"/><author><name>sscheral</name></author><published>2007-05-04T12:05:09+02:00</published><updated>2007-05-04T12:05:09+02:00</updated><content type="html">	&lt;p&gt;&lt;strong&gt;ASP.NET Resources&lt;/strong&gt;&lt;br&gt;
&lt;a href="http://www.asp.net"&gt;www.asp.net&lt;/a&gt;&lt;br&gt;
msdn-asp.net/&lt;br&gt;
wikipedia&lt;br&gt;
&lt;a href="http://www.w3schools.com"&gt;www.w3schools.com&lt;/a&gt;&lt;br&gt;
forums.asp.net&lt;br&gt;
ajax.asp.net&lt;br&gt;
&lt;a href="http://www.codeproject.com/aspnet/"&gt;www.codeproject.com/aspnet/&lt;/a&gt;&lt;br&gt;
&lt;a href="http://www.411asp.net"&gt;www.411asp.net&lt;/a&gt;&lt;br&gt;
aspnet.4guysfromrolla.com&lt;br&gt;
&lt;a href="http://www.123aspx.com"&gt;www.123aspx.com&lt;/a&gt;&lt;br&gt;
&lt;a href="http://www.gotdotnet.com"&gt;www.gotdotnet.com&lt;/a&gt;&lt;br&gt;
&lt;a href="http://www.hotscripts.com"&gt;www.hotscripts.com&lt;/a&gt;&lt;br&gt;
&lt;a href="http://www.developerfusion.co.uk"&gt;www.developerfusion.co.uk&lt;/a&gt;&lt;br&gt;
&lt;a href="http://www.aspnetpro.com"&gt;www.aspnetpro.com&lt;/a&gt;&lt;br&gt;
&lt;a href="http://www.asp-net.com"&gt;www.asp-net.com&lt;/a&gt;&lt;br&gt;
&lt;a href="http://www.aspnetfaq.com"&gt;www.aspnetfaq.com&lt;/a&gt;&lt;br&gt;
&lt;a href="http://www.aspspider.net"&gt;www.aspspider.net&lt;/a&gt;&lt;br&gt;
&lt;a href="http://www.aspdeveloper.net"&gt;www.aspdeveloper.net&lt;/a&gt;&lt;br&gt;
&lt;a href="http://www.aspcode.net"&gt;www.aspcode.net&lt;/a&gt;&lt;br&gt;
&lt;a href="http://www.programmersheaven.com"&gt;www.programmersheaven.com&lt;/a&gt;&lt;br&gt;
&lt;a href="http://www.15seconds.com"&gt;www.15seconds.com&lt;/a&gt;&lt;br&gt;
&lt;a href="http://www.learnasp.com"&gt;www.learnasp.com&lt;/a&gt;&lt;br&gt;
aspnetpodcast.com&lt;br&gt;
aspnetresources.com&lt;br&gt;
&lt;a href="http://www.devasp.net"&gt;www.devasp.net&lt;/a&gt;&lt;br&gt;
&lt;a href="http://www.codeplex.com"&gt;www.codeplex.com&lt;/a&gt;&lt;br&gt;
&lt;a href="http://www.codeguru.com"&gt;www.codeguru.com&lt;/a&gt;&lt;br&gt;
 &lt;strong&gt; C# Resources&lt;/strong&gt;&lt;br&gt;
Msdn-C#&lt;br&gt;
Msdn&lt;br&gt;
&lt;a href="http://www.gotdotnet.com"&gt;www.gotdotnet.com&lt;/a&gt;&lt;br&gt;
&lt;a href="http://www.c-sharpcorner.com"&gt;www.c-sharpcorner.com&lt;/a&gt;&lt;br&gt;
&lt;a href="http://www.csharphelp.com"&gt;www.csharphelp.com&lt;/a&gt;&lt;br&gt;
en.wikipedia.org/wiki/C_Sharp&lt;br&gt;
&lt;a href="http://www.csharpfriends.com/"&gt;www.csharpfriends.com/&lt;/a&gt;&lt;br&gt;
&lt;a href="http://www.csharp-station.com"&gt;www.csharp-station.com&lt;/a&gt;&lt;br&gt;
&lt;a href="http://www.codeproject.com"&gt;www.codeproject.com&lt;/a&gt;&lt;br&gt;
&lt;a href="http://www.mastercsharp.com"&gt;www.mastercsharp.com&lt;/a&gt;&lt;br&gt;
&lt;a href="http://www.csharp-source.net"&gt;www.csharp-source.net&lt;/a&gt;&lt;br&gt;
&lt;a href="http://www.codehound.com"&gt;www.codehound.com&lt;/a&gt;&lt;br&gt;
&lt;a href="http://www.developerfusion.co.uk"&gt;www.developerfusion.co.uk&lt;/a&gt;&lt;br&gt;
&lt;a href="http://csharpcomputing.com"&gt;http://csharpcomputing.com&lt;/a&gt;&lt;br&gt;
&lt;a href="http://www.developersdex.com"&gt;www.developersdex.com&lt;/a&gt;&lt;br&gt;
&lt;a href="http://www.planet-source.code.com"&gt;www.planet-source.code.com&lt;/a&gt;&lt;br&gt;
&lt;a href="http://www.csharp-home.com"&gt;www.csharp-home.com&lt;/a&gt;&lt;br&gt;
  &lt;strong&gt;SQL Server Resource&lt;/strong&gt;&lt;br&gt;
msdn.microsoft.com/sql/express/&lt;br&gt;
SQL Server 2005&lt;br&gt;
&lt;a href="http://www.sql-server-perforamance.com"&gt;www.sql-server-perforamance.com&lt;/a&gt;&lt;br&gt;
&lt;a href="http://www.sqlskills.com"&gt;www.sqlskills.com&lt;/a&gt;&lt;br&gt;
&lt;a href="http://www.technetevents.com"&gt;www.technetevents.com&lt;/a&gt;&lt;br&gt;
&lt;a href="http://www.sqlspider.net"&gt;www.sqlspider.net&lt;/a&gt;&lt;br&gt;
&lt;a href="http://www.aspspider.net"&gt;www.aspspider.net&lt;/a&gt;&lt;br&gt;
&lt;a href="http://www.sqlservercentral.com"&gt;www.sqlservercentral.com&lt;/a&gt;&lt;br&gt;
&lt;a href="http://www.sswugorg.com"&gt;www.sswugorg.com&lt;/a&gt;&lt;br&gt;
vyaskn.tripod.com&lt;br&gt;
&lt;a href="http://www.databasejournal.com"&gt;www.databasejournal.com&lt;/a&gt;&lt;br&gt;
&lt;a href="http://www.microsoft.com/sql"&gt;www.microsoft.com/sql&lt;/a&gt;&lt;br&gt;
&lt;a href="http://www.dataschenk.com/"&gt;www.dataschenk.com/&lt;/a&gt;&lt;br&gt;
&lt;a href="http://www.mcdbadirectory.com/"&gt;www.mcdbadirectory.com/&lt;/a&gt;&lt;br&gt;
&lt;a href="http://www.programmingmsaccess.com"&gt;www.programmingmsaccess.com&lt;/a&gt;&lt;br&gt;
&lt;a href="http://www.umachandar.com"&gt;www.umachandar.com&lt;/a&gt;&lt;br&gt;
&lt;a href="http://www.asksql.com"&gt;www.asksql.com&lt;/a&gt;&lt;br&gt;
&lt;a href="http://www.dbarecovery.com"&gt;www.dbarecovery.com&lt;/a&gt;&lt;br&gt;
&lt;a href="http://www.karaszi.com"&gt;www.karaszi.com&lt;/a&gt;&lt;br&gt;
&lt;a href="http://www.sqlteam.com"&gt;www.sqlteam.com&lt;/a&gt;&lt;br&gt;
www15.brinkster.com/shakhov/&lt;br&gt;
&lt;a href="http://www.1sqlstreet.com"&gt;www.1sqlstreet.com&lt;/a&gt;&lt;br&gt;
cindygross.tripod.com/&lt;br&gt;
&lt;a href="http://www.programmersheaven.com/"&gt;www.programmersheaven.com/&lt;/a&gt;&lt;br&gt;
&lt;a href="http://www.dbbest.com/"&gt;www.dbbest.com/&lt;/a&gt;&lt;br&gt;
sql.manivesa.com&lt;br&gt;
&lt;a href="http://www.mugh.net/sql"&gt;www.mugh.net/sql&lt;/a&gt;&lt;br&gt;
&lt;a href="http://www.microsoft.com/data"&gt;www.microsoft.com/data&lt;/a&gt;&lt;br&gt;
&lt;a href="http://www.easy-sql-server.com/"&gt;www.easy-sql-server.com/&lt;/a&gt;&lt;br&gt;
&lt;a href="http://www.sqlmag.com/"&gt;www.sqlmag.com/&lt;/a&gt;&lt;br&gt;
&lt;a href="http://www.msde.biz/"&gt;www.msde.biz/&lt;/a&gt;&lt;br&gt;
&lt;a href="http://www.mssqlcity.com/"&gt;www.mssqlcity.com/&lt;/a&gt;&lt;br&gt;
&lt;a href="http://www.sqlserverportal.com/"&gt;www.sqlserverportal.com/&lt;/a&gt;&lt;br&gt;
&lt;a href="http://www.sqlswiss.com"&gt;www.sqlswiss.com&lt;/a&gt;&lt;br&gt;
&lt;a href="http://www.expertexchange.com"&gt;www.expertexchange.com&lt;/a&gt; &lt;/p&gt;
&lt;p&gt; &lt;small&gt; &lt;a href="http://sscheralbidar.blog.co.uk/2007/05/04/links~2208631/#comments"&gt;Comments&lt;/a&gt; &lt;/small&gt; &lt;/p&gt;</content></entry><entry><id>tag:sscheralbidar.blog.co.uk,2007-04-10:/2007/04/10/dynamic_management_views_dmv_a_sql_serve~2062927/</id><title>Dynamic Management Views [DMV] – A SQL Server 2005 Feature</title><link rel="alternate" type="text/html" href="http://sscheralbidar.blog.co.uk/2007/04/10/dynamic_management_views_dmv_a_sql_serve~2062927/"/><author><name>sscheral</name></author><published>2007-04-10T07:58:55+02:00</published><updated>2007-04-10T07:58:55+02:00</updated><content type="html">	&lt;p&gt;The DMV’s; newly introduced in SQL Server 2005 gives database administrator information about the current state of the SQL Server machine. These values will help the administrator to diagnose problems and tune the server for optimal performance. DMV’s are designed to be used instead of system tables and various other functions provided in SQL Server 2000.&lt;/p&gt;
	&lt;p&gt;&lt;strong&gt;Two types of dynamic management views:- &lt;/strong&gt;&lt;/p&gt;
	&lt;p&gt;Server-scoped DMV: Stored in Master Database&lt;br&gt;
Database-scoped DMV: Specific to each database &lt;/p&gt;
	&lt;p&gt;&lt;strong&gt;Permission to Execute DMV [Security]&lt;/strong&gt;&lt;br&gt;
To query a Server scoped DMV the database user must have SELECT privilege on VIEW SERVER STATE and for database scoped DMV the user must have SELECT privilege on VIEW DATABASE STATE.&lt;/p&gt;
	&lt;p&gt;GRANT VIEW SERVER STATE to &lt;Login &gt;&lt;br&gt;
GRANT VIEW DATABASE STATE to &lt;User &gt; &lt;/p&gt;
	&lt;p&gt;If you want to deny a user permission to query certain DMV’s you can use the DENY command to restrict access to specific DMV.&lt;/p&gt;
&lt;p&gt; &lt;small&gt; &lt;a href="http://sscheralbidar.blog.co.uk/2007/04/10/dynamic_management_views_dmv_a_sql_serve~2062927/#comments"&gt;Comments&lt;/a&gt; &lt;/small&gt; &lt;/p&gt;</content></entry><entry><id>tag:sscheralbidar.blog.co.uk,2007-04-09:/2007/04/09/dbcc_dbtable~2057243/</id><title>DBCC DBTABLE</title><link rel="alternate" type="text/html" href="http://sscheralbidar.blog.co.uk/2007/04/09/dbcc_dbtable~2057243/"/><author><name>sscheral</name></author><published>2007-04-09T07:47:22+02:00</published><updated>2007-04-09T07:47:22+02:00</updated><content type="html">	&lt;p&gt;This command prints out the contents of the DBTABLE structure.&lt;/p&gt;
	&lt;p&gt;Syntax:&lt;/p&gt;
	&lt;p&gt;DBCC DBTABLE ({dbid|dbname})&lt;/p&gt;
	&lt;p&gt;where&lt;br&gt;
  dbid|dbname  - database name or database ID&lt;/p&gt;
	&lt;p&gt;This is the example:&lt;/p&gt;
	&lt;p&gt;DBCC TRACEON (3604)&lt;br&gt;
DBCC DBTABLE (master)&lt;/p&gt;
	&lt;p&gt;The DBTABLE structure has an output parameter called dbt_open.&lt;br&gt;
This parameter keeps track of how many users are in the database.&lt;/p&gt;
&lt;p&gt; &lt;small&gt; &lt;a href="http://sscheralbidar.blog.co.uk/2007/04/09/dbcc_dbtable~2057243/#comments"&gt;Comments&lt;/a&gt; &lt;/small&gt; &lt;/p&gt;</content></entry><entry><id>tag:sscheralbidar.blog.co.uk,2007-04-09:/2007/04/09/dbcc_dbinfo~2057241/</id><title>DBCC DBINFO</title><link rel="alternate" type="text/html" href="http://sscheralbidar.blog.co.uk/2007/04/09/dbcc_dbinfo~2057241/"/><author><name>sscheral</name></author><published>2007-04-09T07:46:20+02:00</published><updated>2007-04-09T07:46:20+02:00</updated><content type="html">	&lt;p&gt;Print DBINFO structure for specified database.&lt;/p&gt;
	&lt;p&gt;Syntax:&lt;/p&gt;
	&lt;p&gt;DBCC DBINFO [( dbname )]&lt;/p&gt;
	&lt;p&gt;where&lt;br&gt;
  dbname - is the database name.&lt;/p&gt;
	&lt;p&gt;This is the example:&lt;/p&gt;
	&lt;p&gt;DBCC TRACEON (3604)&lt;br&gt;
DBCC DBINFO (master)&lt;/p&gt;
&lt;p&gt; &lt;small&gt; &lt;a href="http://sscheralbidar.blog.co.uk/2007/04/09/dbcc_dbinfo~2057241/#comments"&gt;Comments&lt;/a&gt; &lt;/small&gt; &lt;/p&gt;</content></entry><entry><id>tag:sscheralbidar.blog.co.uk,2007-04-09:/2007/04/09/dbcc_bytes~2057238/</id><title>DBCC BYTES</title><link rel="alternate" type="text/html" href="http://sscheralbidar.blog.co.uk/2007/04/09/dbcc_bytes~2057238/"/><author><name>sscheral</name></author><published>2007-04-09T07:45:38+02:00</published><updated>2007-04-09T07:45:38+02:00</updated><content type="html">	&lt;p&gt;This command can be used to dump out bytes from a specific address.&lt;/p&gt;
	&lt;p&gt;Syntax:&lt;/p&gt;
	&lt;p&gt;dbcc bytes ( startaddress, length )&lt;/p&gt;
	&lt;p&gt;where&lt;br&gt;
  startaddress  - starting address to dump&lt;br&gt;
  length        - number of bytes to dump&lt;/p&gt;
	&lt;p&gt;This is the example:&lt;/p&gt;
	&lt;p&gt;DBCC TRACEON (3604)&lt;br&gt;
dbcc bytes (10000000, 100)&lt;/p&gt;
&lt;p&gt; &lt;small&gt; &lt;a href="http://sscheralbidar.blog.co.uk/2007/04/09/dbcc_bytes~2057238/#comments"&gt;Comments&lt;/a&gt; &lt;/small&gt; &lt;/p&gt;</content></entry><entry><id>tag:sscheralbidar.blog.co.uk,2007-04-09:/2007/04/09/dbcc_buffer~2057233/</id><title>DBCC BUFFER</title><link rel="alternate" type="text/html" href="http://sscheralbidar.blog.co.uk/2007/04/09/dbcc_buffer~2057233/"/><author><name>sscheral</name></author><published>2007-04-09T07:44:24+02:00</published><updated>2007-04-09T07:44:24+02:00</updated><content type="html">	&lt;p&gt;This command can be used to print buffer headers and pages from the&lt;br&gt;
buffer cache.&lt;/p&gt;
	&lt;p&gt;Syntax:&lt;/p&gt;
	&lt;p&gt;dbcc buffer ([dbid|dbname] [,objid|objname] [,nbufs], [printopt])&lt;/p&gt;
	&lt;p&gt;where&lt;br&gt;
  dbid|dbname   - database id|database name.&lt;br&gt;
  objid|objname - object id|object name&lt;br&gt;
  nbufs         - number of buffers to examine&lt;br&gt;
  printopt      - print option&lt;br&gt;
                  0 - print out only the buffer header and page header&lt;br&gt;
                      (default)&lt;br&gt;
                  1 - print out each row separately and the offset table&lt;br&gt;
                  2 - print out each row as a whole and the offset table&lt;/p&gt;
	&lt;p&gt;This is the example:&lt;/p&gt;
	&lt;p&gt;DBCC TRACEON (3604)&lt;br&gt;
dbcc buffer(master,'sysobjects')&lt;/p&gt;
&lt;p&gt; &lt;small&gt; &lt;a href="http://sscheralbidar.blog.co.uk/2007/04/09/dbcc_buffer~2057233/#comments"&gt;Comments&lt;/a&gt; &lt;/small&gt; &lt;/p&gt;</content></entry></feed>
