30.8.08

Count data value in a group from result set

Count data value in a group from result set:
I have come up with a new scenario like, I need to add the data value in the result set.
Using the below technique, we can achieve the same.
create table venkattab (Number int,name1 varchar(10))
insert into venkattab values(1,'Venkat')
insert into venkattab values(2,'Venkat2')
Declare @SQLvar int
SET @SQLvar = 0
Select @SQLvar = @SQLvar + TempValue FROM
(Select Number as TempValue from venkattab where name1='Venkat'
Union All Select Number as TempValue from venkattab where name1='Venkat2')
AS A
Select @SQLvar -- Output is 3
Another Example,
Declare @SQLvar int
SET @SQLvar = 0
Select @SQLvar = @SQLvar + TempValue FROM ( Select 2 TempValue Union All Select 3 Union All Select 4) AS A
Select @SQLvar -- Output is 9
The query,
Select 2 TempValue Union All Select 3 Union All Select 4
will yield a result set 2,3,4 and the variable will fetch the summation of these values.
Happy Learning!!!
Regards,
Venkatesan Prabu . J

XML data type in SQL Server 2005- Part I

XML Data type in SQL Server 2005:
XML is the new data type introduced in SQL Server 2005. XML stands for Extended Markup langugage. A world wide accepted standard to pass the data through network. I can remember my experience on using XML data in legacy systems of SQL server, we used to store those data in the form of varchar data types. But, with the introduction of sql server 2005 XML data type, we can store the data in the form of XMLs.
Let create a table Venkattable,
create table Venkattable(id int, Name varchar(10),age int)
Am inserting some data into this table,
insert into Venkattable values(1,'Venkat',16)
insert into Venkattable values(2,'Arun',16)
insert into Venkattable values(3,'Suba',16)
insert into Venkattable values(4,'Karthi',16)
insert into Venkattable(id,age) values(4,16)
select * from Venkattable

XML Raw:
XML raw statement is used to display your result set in the form of XML but the format of display is quite intersting.It will show up the row by row data with a tag "Row"
Let see a sample for the XML RAW, am trying to display the result set in XML RAW mode.

SELECT * FROM Venkattable FOR XML RAW
The output resembles,

In the above XML Raw method, we wont get a proper segregation of datas like, id in seperate tag, name in seperate and age in seperate tag. To achieve the same, we can go for specifying an additional tag named Elements.
Below is the syntax to acheieve the same.
SELECT * FROM Venkattable FOR XML RAW, ELEMENTS
The output resembles as below,
In the XML RAW method, we lose the identity about the table VenkatTable. Suppose, we need to fetch the table identity in our XML output. We can use XML AUTO method.
The syntax for XML Auto is,
SELECT * FROM Venkattable FOR XML AUTO
The output resembles as below,

Happy Learning!!!
Regards,
Venkatesan Prabu .J

23.8.08

charindex in sql server

charindex in sql server:
Charindex method is used to identify a particular character position in the string.
Thh syntax is Charindex(character or string to find , parent string).
This function will return an integer value.
Create Table #VenkatTable (fname varchar(50))
Insert into #VenkatTable values('1.doc')
Insert into #VenkatTable values('2.doc')
select charindex('.',fname) from #VenkatTable
Output is 2
Happy Learning!!!
Regards,
Venkatesan Prabu .J

Distinct filename or distinct items in SQL Server

Distinct filename or distinct items in SQL Server
Let see some interesting facts like identifying the unique file types available in the database.
Considering, I have stored lot of file types in my database and I need to find, what are the filetypes available
in my database.
---Created a sample table
Create Table VenkatTable (fname varchar(50))
Insert into VenkatTable values('1.doc')
Insert into VenkatTable values('2.doc')
Insert into VenkatTable values('3.doc')
Insert into VenkatTable values('1.xls')
Insert into VenkatTable values('2.xls')
Insert into VenkatTable values('3.xls')
Insert into VenkatTable values('4.xls')
Method 1:
Select Count(*) as [Count],right(fname,3) as FileType from VenkatTable
group by right(fname,3) Order by [Count] desc

Here am trying to use "right" method to fetch the file types availabel in my db. But, this method will fail
if the length of the file extension is more than or less than 3. In that case, we can prefer the second method.
If we are pretty much sure that the extension is of length 3. Method 2:
select
distinct substring (fname, charindex('.',fname)+1,len(fname)) as filetype,
count(*) as count from VenkatTable
group by substring (fname, charindex('.',fname)+1,len(fname))
Its a very generic method to identify all filetypes available in our db.
Happy Learning!!!
Regards,
Venkatesan Prabu .J

20.8.08

SQL Teaser - Between Operator in SQL Server

Let see some interesting facts of SQL Server's between operator:

create table VenkatTable(id int identity(1,1) primary key clustered, MyColumn int)
insert VenkatTable(MyColumn) select 1
union all select 2
union all select 3
union all select 4
union all select 5
union all select 6
union all select 7
union all select 8

--Statement 1
select count(*) from VenkatTable where MyColumn between 3 and 5

Ouput is 3

--Statement 2
select count(*) from VenkatTable where MyColumn between 5 and 3

Ouput is 0
The reason behind the different output is,
Generally, Between indicates the data between the left value and right value. But it's not the case in SQL Server.
This compiler will check for the syntax and
1. It will check the left value should be less than the right value.
2. If its the case, it will return "True" else it will return "False"
3. If the result is true then it will fetch the actual data from the database.
4. If the result is false then it wont fetch any data and give the data as null.

Happy Learning!!!
Regards,
Venkatesan Prabu .J

16.8.08

Compatibility Levels in SQL Server

Compatiblity Level is a nice feature in sql server. It's used to enable your SQL Server in different version. Below is the query used to check the compatibility level of your current database.
EXEC SP_DBCMPTLEVEL 'master'

On executing the above statement you will get the outpu,
The current compatibility level is 90.

90 indicates that, its SQL Server 2005 database.

EXEC SP_DBCMPTLEVEL 'master',80
The above statement is used to enable your database to work as a SQL Server 2000 database.
We can achieve the same using inbuilt dialog box option in SSMS.
right click database ->Properties ->Options->Compatibility Level
Providing You should be a sysadmin role or the database owner to achieve this task.
Happy Learning!!!
Regards,
Venkatesan Prabu .J

Null value handling in Count function

Let's see some interesting facts of NULL value in SQL Server.Am trying to Insert some Null values in my table and check the count of the records available in my table.

Code Snippet
DECLARE @t table(
id int identity(1,1), value varchar(50))
INSERT INTO @t values(NULL)
INSERT INTO @t values(NULL)
INSERT INTO @t values(NULL)
INSERT INTO @t values('1')
INSERT INTO @t values('2')
INSERT INTO @t values('3')
SELECT count(id), count(value) FROM @t

with out executing the code, Can you tell me the output?

OOps, Its 6,3.... How? Its because, SQL Server will discard the null values and considered only the valid values for count function.

Happy Learning!!!

Regards,

Venkatesan Prabu .J

15.8.08

String Pattern matching in SQL Server

Lets see some interesting facts in SQL Server. Today we will see pattern matching concept in SQL Server.
I have tried to create a table name VenkatTable and inserted some rows into it,



Code Snippet
create table VenkatTable( Col1 varchar(5) )
insert into VenkatTable values('_Sas')
insert into VenkatTable values('Sas_')
insert into VenkatTable values('S_us')
insert into VenkatTable values('Sas')
insert into VenkatTable values('Sa_s')
Now I need to fetch the data as per the below condition,



Real Query
select * from VenkatTable where Col1 Like '%Sa_%'


Without executing the above code. Can you tell the number of rows retrieved by the above query?


In the above query, a small trick is embedded, SQL Server wont check for the string having "Sa_"
Because, the character "_" is nothing but a single character matching.
The above query will fetch all the strings matching the word Sa, starting with 0 or any number of characters
and should have 1 character after Sa and its followed by 0 or any number of characters.

Happy Learning!!!

Regards,

Venkatesan Prabu .J