Category Archives: SQL Server - Page 4

Except and Intersect operators in SQL Server 2005/2008 (from decipherinfosys)

Good Explanantion and usage examples of the Except and Intersect  Operators in SQL 2005 ( 2008).

From decipherinfosys
In SQL Server 2005, MSFT introduced two new operators in their T-SQL language: Except and Intersect. We had briefly touched upon these while discussing the MERGE command in Oracle – you can read more on that post here. EXCEPT returns the distinct data value from the left query (query on left side of the operand) which does not exist in the right query (query on the right side of the operand). INTERSECT returns data value which is common in both queries. In Oracle, the equivalent of the EXCEPT operator is MINUS and INTERSECT is same as INTERSECT. In SQL 2005, using EXCEPT and INTERSECT, one can also simulate the MERGE command in Oracle (see the blog post that is mentioned above).

Read Full Post Here

SSRS – Format() and FormateDateTime()

Found this reference … JUST IN TIME!

Thanks Thavash This is by far my most visited page.

http://thavash.spaces.live.com/blog/cns!CF6232111374DFD2!155.entry

April 10

Working with Dates in Reporting Services

As with any other technology ( eg. SQL , C# ), you always find people running into problems with processing of dates. I was asked today , “How to I format dates when using the Date Picker and sending it through to a stored procedure” ? Let’s have a look ….
1) The FormatDateTime command
This is pretty easy to use, but maybe a bit limiting. You can specify 1 of 4 formats using the command arguments. Let’s say we have selected a date such as 10th April 2007 , our results will be as follows :
Command Result
FormatDateTime(Parameters!Date.Value,1) Tuesday, April 10, 2007
FormatDateTime(Parameters!Date.Value,2) 4/10/2007
FormatDateTime(Parameters!Date.Value,3) 12:00:00 AM
FormatDateTime(Parameters!Date.Value,4) 00:00

…but the better way to do it would be to use …

2) The Format command and specify the exact format you require. For example…

Command Result
Format(Parameters!Date.Value,”dd-MM-yyyy”) 10-04-2007
Format(Parameters!Date.Value,”dd/MM/yyyy”) 10/04/2007
Format(Parameters!Date.Value,”MMM-dd-yyyy”) Apr-10-2007
Format(Parameters!Date.Value,”MMM-dd-yy”) Apr-10-07

So 3 M’s give you “Apr” ….anyway this is quite useful if you’re looking for Day/Month/Year , since the system will default to MM/DD/YYYY.

Using this you should be able to display the date format you want , or send through a particular format to a Stored Proc.

EDITED 22/08/2007 : If the Format Command doesn’t work , try converting the value to a date , eg.

Format(Cdate(Parameters!Date.Value),”dd-MM-yyyy”)

T-SQL – Change SQL Job Owners to SA

I’m a sucker for the Maint Wizards and everytime I touch a SQL Job.. It reverts the ownership to my domain account. ( Not a Good Practice to use my domain account … I know). I manually change the ownership back to sa (actually, dbo as sa is disabled…ssssh) . OR I CAN USE THE BELOW HANDY-DANDY SCRIPT . As always .. Buyer-Beware! Written for  SQL Server 2005..

Here is a script I wrote to do the heavy lifting:

Read more »

Group By and Case Statements or Other SQL Functions

One thing I forget to do often is to include the actual item I’m returning in the Select Statement in the Group By. Meaning , if I use a case statement of some function to change the value that I’m returning, then I should include that and not the base column name in the Group By part of the select statement


/*
If you write something like this and the Col2 has multiple 'Unique' Values
then you will get a row for each value distinct Col2 value that says 'Other'
*/
SELECT [Col1]
,Case When [Col2] = 10 then 'Ten'
Else 'Other' End as [LameExample]
,Count(*) as CNT
FROM [dbo].[lameTableExample]
GROUP BY [Col1], [Col2]

-- This Example will return Only two rows

SELECT [Col1]
,Case When [Col2] = 10 then 'Ten'
Else 'Other' End as [LameExample]
,Count(*) as CNT
FROM [dbo].[lameTableExample]
GROUP BY [Col1]
, Case When [Col2] = 10 then 'Ten'
Else 'Other' End

Select Case has a simple overview of using CASE in a SQL Query with the SUM() function.

For Each … SQL System Stored Procedures

Sql server has some great built-in commands procs that can help you script server or database wide commands. sp_MSForEachTable and Sp_MSForEachDb

When first looking at a database , I sometimes like to run the following:
execute sp_MSforEachTable @command1=‘Print ”?”; Select Count(*) as [? – (count)] from ?;Select Top 10 * from ? ‘

I used the sp_MSforeachDB just the other day to set all the non-system
databases on a test server to Simple Recovery mode.Just made a quick stored procedure that I passed the ? value and filter out the system databases ( Master, Model, Tempdb and MSDB).

For More Information Checkout :

November 30, 2004
SQL Server Undocumented Stored Procedures sp_MSforeachtable and sp_MSforeachdb
By Gregory A. Larsen
http://www.databasejournal.com/features/mssql/article.php/3441031