Tuesday, July 31, 2007

STATIC Import, A new feature in JAVA 5.0

Static Import statement is the new addition in JDK 5.0. This statement is used to import the static variables/fields and static members of some class.

Let's take an example, like if we need to use the "PI" static field of Math class in java.lang then we need to write something like :

double a=10;
double res = Math.sin(Math.PI * a);

But if you use the static import in your program then it will be like:

import static java.lang.Math.PI;

double a=10;
double res = Math.sin(PI * a);
you noticed that we have no need to write the fully qualified name of the PI but still we need to write the fully qualified name of the static method "sin" like Math.sin(..).

There is a second form of the static import that imports all of the static fields and members of a class .i.e by using the * notation.


import static java.lang.Math.*;

double a=10;
double res = sin(PI * a);

Friday, July 27, 2007

Visual Studio .NET 2008 Beta Available to Download Now

Visual Studio .NET 2008 Beta Available to Download Now on the Microsoft website:
Here are the Links:

You can download the Visual Studio 2008 product here. You can alternatively download the smaller VS 2008 Express Editions here.


Enjoy!!!!!!

Visual Studio .NET 2008 First Look Video

I was surfing on the net today and came across a link for the first look video of Visual Studio .NET 2008. I would like to share it with you people.

Here is the link:

http://www.xmlforasp.net/codebank/download/blog/video/LinqDataSource.wmv

Enjoy!!!!!!

UDF to Get Day of Week in SQL Server 2005

The following UDF can be used to get the Day of Week by passing it a specific date:

CREATE FUNCTION dbo.udf_Day_Of_Week(@p_Date DATETIME)
RETURNS VARCHAR(10)
AS
BEGIN
DECLARE @return_DayofWeek VARCHAR(10)
SELECT @return_DayofWeek = CASE DATEPART(dw,@p_Date)
WHEN 1 THEN 'SUNDAY'
WHEN 2 THEN 'MONDAY'
WHEN 3 THEN 'TUESDAY'
WHEN 4 THEN 'WEDNESDAY'
WHEN 5 THEN 'THURSDAY'
WHEN 6 THEN 'FRIDAY'
WHEN 7 THEN 'SATURDAY'
END
RETURN (@return_DayofWeek)
END


Wednesday, July 25, 2007

How To Take Database Schema Backup in SQL Server 2005

How To Take Database Schema Backup in SQL Server 2005

  • Go to OBJECT EXPLORER of the SQL Server 2005 and expand the Databases tree node, you can see the available databases in your server.





  • Then Right Click on the specific Database you wish to make the schema backup. And go to TASKSà GENERATE SCRIPTS like shown in the figure below:





  • After selecting the GENERATE SCRIPTS you will see a welcome screen like shown below.




  • Click on NEXT button of the Wizard and you will be asked to select the database you wish to generate schema backup like below in figure(TestDB is the database in this case):





  • If you want to take the full schema back up of the database then check the checkbox “Script all objects in the selected database” like shown here in the figure and click Next button of the wizard.




  • In the next screen you can play with the different properties. Just need to change one of the property if you need to use this schema on SQL Server 2000 version, that is “Script for Server Version” and select the appropriate version you needed.





  • After selecting the appropriate SQL Server version for which you are preparing the schema backup click Next Button and you will reach at the OUTPUT OPTION and then select the appropriate output option. In my case I am saving the script of schema in a File of Unicode Text like:





  • Then Click Next and you will reach on the Script wizard Summary screen, Simply Click Finish to execute your requested job.





  • After clicking the Finish button you will reach at the Generate Script progress Wizard. It will take a little time depending upon the number of objects in your selected database. After completing its job it will show you the success status.




  • If you want to see the Report of the whole process, just select from the options of Report button just up from the Close and select the output option of the report. Otherwise click Close. That’s it and you are Done.

Tuesday, July 24, 2007

A UDF To Get Previous Working Day in SQL Server 2005

It has pretty similar logic as we done with the UDF of Next Working Day. You can read the earlier post on "How to get the Week Day" for better understanding. Here is the self explanatory code.

CREATE FUNCTION dbo.udf_GetPreviousWorkingDay (@p_Date DATETIME )
RETURNS DATETIME
AS
BEGIN
DECLARE @m_WeekDay INT
DECLARE @rt_Prev_Working_Day DATETIME
SET @m_WeekDay = DATEPART(weekday,@p_Date)

IF @m_WeekDay = 1 -- IF SUNDAY then subtract 2 fromsunday to get friday that is working day
SET @rt_Prev_Working_Day = DATEADD(d,-2,@p_Date)
ELSE IF @m_WeekDay = 2 -- IF MONDAY then subtract 3 fromsunday to get friday that is working day
SET @rt_Prev_Working_Day = DATEADD(d,-3,@p_Date)
ELSE -- ELSE subtract one to get the prev date
SET @rt_Prev_Working_Day = DATEADD(d,-1,@p_Date)

RETURN @rt_Prev_Working_Day
END

Here are some of the test cases having the same result:

SELECT dbo.udf_GetPreviousWorkingDay ( '07/21/2007')
SELECT dbo.udf_GetPreviousWorkingDay ( '07/22/2007')
SELECT dbo.udf_GetPreviousWorkingDay ( '07/23/2007')

Result:

2007-07-20 00:00:00.000

A UDF To Get The Next Working Day @ SQL Server 2005

The following scalar valued function will return the next working day of the week. Here is the code:

CREATE FUNCTION dbo.udf_GetNextWorkingDay (@p_Date DATETIME )
RETURNS DATETIME
AS
BEGIN
DECLARE @m_WeekDay INT
DECLARE @rt_Next_Working_Day DATETIME
SET @m_WeekDay = DATEPART(weekday,@p_Date)

IF @m_WeekDay = 6 -- Friday
SET @rt_Next_Working_Day = DATEADD(d,3,@p_Date)
ELSE IF @m_WeekDay = 7 -- Saturday
SET @rt_Next_Working_Day = DATEADD(d,2,@p_Date)
ELSE
SET @rt_Next_Working_Day = DATEADD(d,1,@p_Date)

RETURN @rt_Next_Working_Day
END

To test this UDF you can use the following example:

SELECT dbo.udf_GetNextWorkingDay ( '07/20/2007')
SELECT dbo.udf_GetNextWorkingDay ( '07/21/2007')
SELECT dbo.udf_GetNextWorkingDay ( '07/22/2007')

The result of all of the above test cases will be as following:
2007-07-23 00:00:00.000

How to Get Week-Day and Quarter of year in SQL Server 2005

  • You can get the week-day by using the DATEPART scalar valued function of the SQL Server 2005. For example the following statement returns 1 (one) as it is default value for Sunday.
SELECT DATEPART(weekday,'07/22/2007' )
or
SELECT DATEPART(dw,'07/22/2007' )

Result = 1
-------------------------------------------------------
The following are the default values for the week days:

SUNDAY = 1
MONDAY= 2
TUESDAY = 3
WEDNESDAY=4
THURSDAY= 5
FRIDAY = 6
SATURDAY = 7

  • Similarly you can check the quarter of the year by using one of the following query. SELECT DATEPART(quarter,'07/22/2007' ) or
    SELECT DATEPART(qq,'07/22/2007' ) or
    SELECT DATEPART(q,'07/22/2007' )
Result: 3 ( as this is 3rd quarter of the year)

You can get the details from MSDN online, Here is the reference of the MSDN:
MSDN ONLINE

Friday, July 20, 2007

Hibernate Plugin for Eclipse (HibernateSynchronizer)

Hibernate is used for database object/relational persistence and it is a very cool. It is available for both JAVA and .NET but is most popular for JAVA.

I was working with the Hibernate using eclipse. I was looking for some plugin of Eclipse and I found one with the name of "HibernateSynchronizer"

It is an Eclipse plugin code generation tool which, by default, binds to *.hbm files and automatically generates business objects when your hibernate schema configuration file is modified.

Here is the link to download it:
http://hibernatesynch.sourceforge.net/
http://sourceforge.net/project/showfiles.php?group_id=99370

String Functions @ SQL Server 2005

There are number of scalar function to manipulate the strings in SQL server. I will just discuss the most common used scalar functions available for string manipulation:

1- REVERSE
This scalar function is used to reverse the character string provided. The input parameter may be constant string, variable or column name of character or binary type.
EXAMPLE:- SELECT REVERSE('SABAH') , REVERSE('IRFAN')
RESULT:- HABAS NAFRI

2- LEN
It returns the number of characters in the input parameter provided. Input parameter may be constant char string, variable or column name. Please note it will not count the trailing spaces.
EXAMPLE:- SELECT LEN('SABAH U DIN') , LEN('IRFAN ')
RESULT:- 11 5

3-
SPACE
It returns the number of spaces according to the integer parameter provided as input. It takes integer_expression as input. if the integer expression is negative then it returns null string.
EXAMPLE:- SELECT 'Sabah' +SPACE(10)+'Irfan'
RESULT:- Sabah Irfan

4-
REPLACE
This function has three parameters all of character type. It finds the 2nd parameter_string from 1st_parameter string and replaces all occurrence with the 3rd parameter_string.
EXAMPLE:- SELECT REPLACE('abcdefghicde','cde','xxx')
RESULT:- abxxxfghixxx

5- ASCII
It returns the integer ASCII code of the left most character of the input string.
EXAMPLE:- SELECT ASCII('A'),ASCII('ABCDEF')
RESULT:- 65 65

6- CHAR
It converts the integer ASCII code into char.
EXAMPLE:- SELECT CHAR(65),CHAR(78)
RESULT:- A N

7-
LOWER
It returns the lower case character string after converting the upper case characters present into the input string.

EXAMPLE:- SELECT LOWER('SAbah U Din')
RESULT:- sabah u din

8- UPPER
It returns the upper case character string after converting the lower case characters present into the input string.

EXAMPLE:- SELECT UPPER('SAbah U Din')
RESULT:- SABAH U DIN

9- LEFT
It returns the left part of the string with the number of characters provided.
EXAMPLE:- SELECT LEFT('Sabah-u-Din',7)
RESULT:- Sabah-u

10- RIGHT
It returns the right part of the character string with the number/count of characters provided in its 2nd parameter.
EXAMPLE:- SELECT RIGHT('Sabah-u-Din',7)
RESULT:- h-u-Din

11- RTRIM
It
returns a character string after truncating all trailing blanks(blanks at the end of string).
EXAMPLE:- SELECT RTRIM('Sabah ')
RESULT:- Sabah

12- LTRIM
It
returns a character string after truncating all leading blanks(blanks at the start of string).
EXAMPLE:- SELECT LTRIM(' Sabah')
RESULT:- Sabah


13- CHARINDEX
It returns the starting position of the specified expression in a character string. Its syntax is
CHARINDEX ( expression1 ,expression2 [ , start_location ] )

EXAMPLE:- SELECT CHARINDEX('r','rSaba-u-din-irfan',3)
RESULT:- 14

NOTE: There are a couple of more scalar functions available in SQL Server but the above
are the mostly used ones.

Wednesday, July 11, 2007

Listing Specific Objects From the DB @ SQL Server 2005

There are always situations when you need to check/find the specific object like table/stored procedure from the database and usually for enterprize level applications there are hundreds or even thousands of user tables and stored procedures.

Following are some of the queries that will help to list the complete details of some specific
Object whose type you are providing in the WHERE clause. You can filter out the results as you needed just by adding couple of more conditions.

select * from sys.objects where type = 'D' -- DEFAULT_CONSTRAINT
select * from sys.objects where type = 'F' -- FOREIGN_KEY_CONSTRAINT
select * from sys.objects where type = 'FN'-- SQL_SCALAR_FUNCTION
select * from sys.objects where type = 'IT'-- INTERNAL_TABLE
select * from sys.objects where type = 'P' -- SQL_STORED_PROCEDURE
select * from sys.objects where type = 'PK'-- PRIMARY_KEY_CONSTRAINT
select * from sys.objects where type = 'S' -- SYSTEM_TABLE
select * from sys.objects where type = 'SQ'-- SERVICE_QUEUE
select * from sys.objects where type = 'U' -- USER_TABLE
select * from sys.objects where type = 'UQ'-- UNIQUE_CONSTRAINT

The following query will result you with the available types of Objects of the database:

select distinct(type) from sys.objects order by type asc

Monday, July 9, 2007

UDF to parse the numeric values from a String

This UDF will parse a string and will return the numeric values that are present in the string in the same sequence that they have in string.

CREATE FUNCTION dbo.UDF_ParseNumeric
(
@string VARCHAR(8000)
)
RETURNS VARCHAR(8000)
AS
BEGIN
DECLARE @IncorrectCharLoc SMALLINT
SET @IncorrectCharLoc = PATINDEX('%[^0-9]%', @string)
WHILE @IncorrectCharLoc > 0
BEGIN
SET @string = STUFF(@string, @IncorrectCharLoc, 1, '')
SET @IncorrectCharLoc = PATINDEX('%[^0-9]%', @string)
END
SET @string = @string
RETURN @string
END

TEST:
SELECT dbo.UDF_ParseNumeric('san1as2,.?"< \{4}[]6>:')
Result:
1246

PATINDEX and STUFF @ SQL Server 2005

1- PATINDEX:-

It returns the starting index/position of the first occurrence of the pattern provided. If no match is found then it will return zero.
Syntax:
PATINDEX( '%pattern%', expression )

where
pattern:
is the pattern to find.
expression: is the string or usually the column name that will be
searched to find the specified pattern.
--------------------------------------------------------

2- STUFF:-

Deletes a specified length of characters and inserts another set of characters
at a specified starting point.

Syntax:

STUFF ( character_expression , start , length ,
character_expression )

where:
character_expression: can be a constant, variable, or column
of either character or binary data.
start: is the start location for the deletion and insertion.
It is an integer value. If its value is negative or
longer than character_expression then null string is
returned.
length: is the length of the character to be deleted.

Example:

SELECT STUFF('abcdefgh', 2, 3, 'SABAH')

Result: aSABAHefgh





Thursday, July 5, 2007

New Features in C# .NET 2.0

There are couple of new features/concepts that are introduced in the version 2.0 of C#.NET. These are listed below:
  1. Generics
  2. Partial Classes
  3. Iterators
  4. NULLABLE Value Types
  5. Anonymous Methods
  6. Coalesce operator: (??)
  7. Anonymous delegates providing closure functionality
  8. A yield return similar to yield in Python
Will explain one by one in different blogs for simplicity.

SQL Server @ WHERE vs HAVING Clause

WHERE vs HAVING Clause

Both are used to filter out the records but have coupe of differences:
  • HAVING can only be used with SELECT statement, while WHERE can be used for SELECT, UPDATE and DELETE.
  • HAVING specifies a search condition on group or aggregate function, while WHERE specifies the search condition on the individual rows.
  • HAVING is typically used with GROUP BY clause. If GROUP BY is not used then HAVING behaves like WHERE clause.

Tuesday, July 3, 2007

sp_rename @ SQL Server 2005

It Changes the name of a user-created object in the current database. This object can be a table, index, column, alias data type, or Microsoft .NET Framework common language runtime (CLR) user-defined type.

Syntax:

sp_rename @ObjectName, @ObjectNewName, @ObjectType

WHERE:

@ObjectName: is the name of the user object etc.
@ObjectNewName:
is the new name of the user object. It has no defaults.
@ObjectType:
defines the type of the user object to be renamed. The following types are supported:
  • A column
  • A database
  • An Index
  • UserDataType ( Alias type, objects created using CREATE statement and CLR user defined types)

Monday, July 2, 2007

CREATE STORED PROCEDURE And UDF WITH ENCRYPTION

You can create encrypted stored procedures, so that no one can see the source code of the stored procedure. Even if you will try to see its text using sp_helptext procedure; it will show the following message

The text for object '[sptst_Test]' is encrypted.

Syntax:
Just add WITH ENCRYPTION Clause after the parameter's list of the stored procedure or UDF.

Example:

CREATE PROCEDURE [dbo].[sptst_Test]
-- Add the parameters for the stored procedure here
WITH ENCRYPTION
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;

-- Insert statements for procedure here
SELECT * FROM sys.tables

END

NOTE:
  • You can not decrypt the Encrypted stored procedure or UDF. So it is good practice not to use this security. Rather use the user permissions if you need to restrict some one.
  • CLR UDF and Stored Procedures can not be Encrypted.


Replicate function in SQL Server 2005

The replicate function in SQL Server 2005 is used to replicate a specific value for a provided number of times. For example the following query,

select replicate('Sabah ',10) as [Replicate Test]

Results:

Sabah Sabah Sabah Sabah Sabah Sabah Sabah Sabah Sabah Sabah