Friday, December 6, 2013

Match Case sensitive String in SQL Server

How do you compare strings so that the comparison is true only if the cases of each of the strings are equal as well. For example:

Select * from a_table where attribute = 'a'

...will return a row with an attribute of 'A'. I do not want this behavior.
So, we can write.............

Select * from a_table where attribute = 'a' COLLATE Latin1_General_CS_AS

You can also convert that attribute as case sensitive using this syntax :

ALTER TABLE Table1
ALTER COLUMN Column1 VARCHAR(200)
COLLATE SQL_Latin1_General_CP1_CS_AS

Now your search will be case sensitive.
If you want to make that column case insensitive again, then use

ALTER TABLE Table1
ALTER COLUMN Column1 VARCHAR(200)

COLLATE SQL_Latin1_General_CP1_CI_AS

Tuesday, December 3, 2013

Which site I follow...

I follow these sites for VB.Net & SQL Server. You may also follow these...

http://blog.sqlauthority.com/
http://www.crystalreportsbook.com/Forum/forum_posts.asp?TID=181
http://www.katieandemil.com/bi-tutorial-from-beginner-to-expert-microsoft-products
http://ssrstutorials.blogspot.in/2012/10/lesson-19-sqlssrs-interview-questions.html
http://www.programmerinterview.com/
http://www.mssqltips.com/
http://dot-nettuts.blogspot.in/
http://dotnet01.blogspot.in/
http://technet.microsoft.com/en-us/library/
http://sqltutorials.blogspot.in/
http://www.visual-basic-tutorials.com/
https://flipboard.com/section/everything-about-data-by-sqlauthority-burkRy
http://sqltutorials.blogspot.in/
http://sqlguru.in/#
http://blogs.msdn.com/
http://www.sqlcoffee.com/index.htm
http://www.sqlservergeeks.com/articles/


Dynamic Pivot in SQL Server

USE tempdb
GO

CREATE TABLE RESULT
( SID INT,
SUB VARCHAR(10),
MARKS INT
)
GO

INSERT INTO RESULT
SELECT 1,'SUB1',54 UNION ALL SELECT 1,'SUB2',46 UNION ALL SELECT 1,'SUB3',57 UNION ALL SELECT 1,'SUB4',86 UNION ALL
SELECT 2,'SUB1',64 UNION ALL SELECT 2,'SUB3',34 UNION ALL SELECT 2,'SUB5',76 UNION ALL SELECT 2,'SUB6',96 UNION ALL
SELECT 3,'SUB2',54 UNION ALL SELECT 3,'SUB4',46 UNION ALL SELECT 3,'SUB5',57 UNION ALL SELECT 3,'SUB6',86 UNION ALL
SELECT 4,'SUB1',64 UNION ALL SELECT 4,'SUB3',34 UNION ALL SELECT 4,'SUB4',76 UNION ALL SELECT 4,'SUB6',96
GO

SELECT * FROM RESULT

GO

-- NORMAL PIVOT TABLE --
SELECT * FROM RESULT
PIVOT (MAX(MARKS) FOR SUB
IN (SUB1,SUB2,SUB3,SUB4,SUB5,SUB6))AS PVT
WHERE SID = 1

SID SUB1 SUB2 SUB3 SUB4 SUB5 SUB6
1 54 46 57 86 NULL NULL

-- DYNAMIC PIVOT TABLE USING STUFF()—-

DECLARE @SID VARCHAR(8)
DECLARE @QRY VARCHAR(MAX)

SET @SID = '1'
SET @QRY = (SELECT DISTINCT STUFF(( SELECT ', ' + CAST(SUB AS VARCHAR(MAX)) FROM RESULT WHERE (SID = @SID)
                  FOR XML PATH ('')),1,2,'') AS SUB
                  FROM RESULT)

SELECT @QRY = 'SELECT * FROM
                  (
                  SELECT * FROM RESULT WHERE SID = '+@SID+'
                  ) V1
                  PIVOT (MAX(MARKS) FOR SUB IN
                  ('+@QRY+')
                  )AS PVT'

EXECUTE(@QRY)

SID SUB1 SUB2 SUB3 SUB4
1 54 46 57 86

-- DYNAMIC PIVOT USING COALESCE() --

DECLARE @SID VARCHAR(8)
DECLARE @QRY1 VARCHAR(MAX)

SET @SID = '1'
SELECT @QRY1 = COALESCE(@QRY1 +',','') + SUB FROM
                  (SELECT SUB FROM RESULT WHERE SID = @SID) AS X
                 
SELECT @QRY1 = 'SELECT * FROM
                  (
                  SELECT * FROM RESULT WHERE SID = '+ @SID +'
                  ) V1
                  PIVOT (MAX(MARKS) FOR SUB IN
                  ('+ @QRY1 +')
                  )AS PVT'
                 
EXECUTE(@QRY1)

SID SUB1 SUB2 SUB3 SUB4
1 54 46 57 86

DROP TABLE RESULT

Hope, you are understand. Thank You...