Venkatesan Prabu MCITP,MCAD,MCTS,CCNA. Worked as a ProjectLead(Senior .Net developer,SQL DBA). Now, Managing Director of KAASHIV INFO TECH, Chennai This Blog aims in serving the community in a better way. This blog is read by developers in 159 countries with average of 400 hits per day. Please post your valuable suggestions and hold my hand to serve the community. Lets make a new world with good thoughts and good minds....... This blog serves the SQL server community all over the world.
29.10.09
Datediff function in sql server
Datediff function is used to differentiate two different dates/month/year.
The syntax is,
Datediff(Month or day or year, Date1, Date2)
If needed, Date1 can be current date to get the desired values. Let's see some examples,
declare @val datetime
declare @val2 int
declare @val3 datetime
declare @val4 int
set @val='11/3/2009'
select @val2 = (datediff(month ,@val,getdate() ))-- + (@val1 *12) )
print @val2 -- The ouptut is -1
set @val3='11/3/2007'
select @val4= (datediff(month ,@val3,getdate() ))-- + (@val1 *12) )
print @val4 -- The ouptut is 23
Let's try with day difference,
declare @val datetime
declare @val2 int
declare @val3 datetime
declare @val4 int
set @val='11/3/2009'
select @val2 = (datediff(day ,@val,getdate() ))-- + (@val1 *12) )
print @val2 -- The ouptut is -4
set @val3='11/3/2007'
select @val4= (datediff(day ,@val3,getdate() ))-- + (@val1 *12) )
print @val4 -- The ouptut is 727
Thanks and Regards,
Venkatesan Prabu .J
28.10.09
Numeric datatype in SQL Server
Numeric datatype is equivalent to the decimal datatype. Decimal (ANSI supported) is recent version of numeric datatype.
Numeric datatype will have be declared with a scale and precision.
Syntax:
variableName numeric(scale, precision)
Let's see some samples,
create table venkat (id numeric(1,3))
On executing the above command, am getting the below error.
Msg 183, Level 15, State 1, Line 1
The scale (3) for column 'id' must be within the range 0 to 1.
Reason for this SQL error:
The scale value should be bigger than the precision value. If we declare numeric (5,2). Then, the value will be 3 digits with 2 decimal points. (Example 456.45, 453.23)
So, the number followed with the numeric keyword should be bigger.
Lets try this,
create table venkat (id numeric(5,3))
Our table is created.
Happy Learning!!!
Thanks and Regards,
Venkatesan Prabu .J
26.10.09
Datatype conversion in SQL server (Float to Int)
Let's see an interesting article on data type conversion,
select 8.5
-- The output is 8.5
Using convert operator to convert a float value into an integer:
select convert(int,8.9)
Floor and ceiling functions in SQL Server:
Floor function is used to get the lowest possible integer value from the given float value
select floor(8.9)
-- The output will be 8
Ceiling function is used to fetch the possible maximum value(next possible integer value) from the given float value.
select ceiling (8.9)
-- The output will be 9
select ceiling (8.4)
-- The output will be 9
Using Cast operator in the conversion process:
select cast (8.9 as int)
The above statement will try to cast the value into a possible integer. An equivalent to floor function.
Thanks and Regards,
Venkatesan Prabu .J
21.10.09
Sys.Index table in sql server
Sys.Indexes table will list down all the indexes available in the database. In detail, it will list down the clustered indexes, Non-clustered indexes and heap.
Clustered indexes will restructure the entire data inside the table where as non clustered index will create a pointed inside the table.
On executing the below query,
select * from sys.indexes
The table will hold the name of the index/type of the index/fill factor/ its working nature /hold primary key/unique key etc..,
select * from sys.indexes where is_disabled = 1
To find, whether the index holds primary key (Index created due to the primary key on the table)
select * from sys.indexes where is_primary_key=1
To find, whether the index holds unique key on the column:
select * from sys.indexes where is_unique_constraint=1
Thanks and Regards,
Venkatesan Prabu .J
Special symbols in the column names in sql server table
It's obvious that, we used to use keywords or special characters in the column name. SQL Server won't permit us to use the same directly. In that case, they will provide an option (Brackets [ ]) to use it.
Let see our example,
create table venkattable(kg/m varchar(10))
Output:
Msg 102, Level 15, State 1, Line 1
Incorrect syntax near '/'.
To resolve this issue, I have used brackets.
create table venkattable([kg/m] varchar(10))
Output:
Command(s) completed successfully.
Thanks and Regards,
Venkatesan Prabu .J
15.10.09
Identity usage across table in SQL Server
As you know, Identity option is used to create unique values in regular intervals. One of the forum person have asked an interview question like, "Is it possiblel to spawn the identity value across the tables. Since identity is table specific item. How can we acheieve it?"
Nice question right!!!! Let' see how to acheive it?
Option 1:
Create a primary key in the table 1 and refer this column in the other tables. Make the columns in other two tables as primary key.
Option 2:
Create a primary key in one table and refer the column in other two tables. Make the other two tabels column as unique non-clustered index column (or) unique clustered index column else add a unique constraints on the columns in the other table.
Thanks and Regards,
Venkatesan Prabu .J
13.10.09
Remote query timeout in SQL Server
Ocassionally, Application will throw Query timeout errors. Do we have analysed why its happening?
Remote query timeout is due to the following reasons,
1. Server performance is too slow due to large number of transactions. So, it couldn't allocate necessary resources for the current session.
2. Network issue.
3. May be a very long complex query which will take more time to execute.
In that case, we can set the option of remote query timout to infinite or we can increase the timings.

Cheers,
Venkatesan Prabu .J
Decode function in Oracle
Decode function in Oracle:
Decode is an oracle specific function which is equivalent to "IF else" function in SQL Server.
For example:
Am having a query in oracle
SELECT ID, decode(id, 1, 'Venkat', 2,'Arun', 'NoName')NAME FROM VenkatTable
The equivalent function in SQL Server is.
decode(id, 1, 'Venkat', 2,'Arun', 'NoName')NAME
->
IF ID = 1 THEN NAME ='Venkat'
ELSEIF ID=2 THEN NAME ='Arun'
ELSE NAME ='NoName'
Thanks and Regards,
Venkatesan Prabu .J
Script SQL Databases
This is one of an interesting topic which I like to cover it in my blog. How to script a SQL Server database?
Right click on the database -> Goto tasks -> Generate scripts



On clicking next button, you will get the objects details. What are the objects to be considered in generating the script.
After update trigger in SQL Server
Triggers are automatic invocation of statements. It can be written based on the action (or) a response for the action (or) a preliminary step before the action.
Considering a scenario, I need to insert a record while updation happened in the table. In that case, we can write after update trigger (Below is an example). Similarly, we will be having after insert, for insert, for update triggers.
Below is a small code snippet to create a table + trigger,
create table venkat(id int, val varchar(20))
insert into venkat values(1,'Venkat1')
insert into venkat values(2,'Venkat2')
insert into venkat values(3,'Venkat3')
insert into venkat values(4,'Venkat4')
select * from venkat
create trigger upd_venkat on venkat
after updateas
begin
insert into venkat values(5,'Venkat5')
end
update venkat set id=6 where id=4

select * from venkat
6.10.09
Finding text in the stored procedure
Scenario:
I want to fetch all the stored procedure which is using a specific table(Venkat_table). How to find it?
Below is the solution for the same,
-- Creating the table
drop table Venkat_Table
create table Venkat_Table(id int)
insert into Venkat_Table values(10)
insert into Venkat_Table values(20)
insert into Venkat_Table values(30)
select * from Venkat_Table
-- Creating the procedure
create procedure Venkat_proc
as
begin
select * from Venkat_Table
end
-- In SQL Server 2005, we will have sys.procedures which holds all the details of the procedure.-- Defintion of the the specific object will provide you the definition details of the object.
SELECT *
FROM sys.procedures
WHERE OBJECT_DEFINITION(OBJECT_ID) LIKE '%Venkat_Table%'

-- In SQL Server 2000, we will have syscomments and sysobject. syscomments will hold the definition of the object. sysobjects will list downt the objects. Joining syscomments and sysobjects will retrieve the necessary object names.
Below is the query to fetch the same,
SELECT DISTINCT so.name
FROM syscomments sc
INNER JOIN sysobjects so ON sc.id=so.id
WHERE sc.TEXT LIKE '%Venkat_Table%'

This will give the object definition inturn it's an alternative for the sp_help text option in SQL Server.
SELECT OBJECT_DEFINITION (OBJECT_ID(N'Venkat_proc'))
Thanks and Regards,
Venkatesan Prabu .J
Happy Learning!!!
Schema Names in Oracle database
For retrieving the schema names from the oracle database, we need to use dba_objects table.
select * from dba_objects
The above query will retrieve all the objects from the table. Below are some of the high level information.
Owner: Schema name
Object Name: Name of the object
Object_ID : Unique ID generated by the oracle database.
Object_type : Type of the object whether its a table/index/view or synonym
Created : creation time
Valid : Whether this object is valid or not?

Below is the query used to fetch all the schemas located in the oracle database.
select owner,count(owner) cnt from dba_objects group by owner
It includes all the system related schemas. This can be removed by using not in operator. Schema names like Oracle, Common, system, xdb etc.., were related to Oracle system related schemas.
Thanks and Regards,
Venkatesan Prabu .J
Happy Learning!!!