Thursday, 12 March 2015

How to find number of rows in each partition

How to get partition Information including no of rows in each partition.

SELECT
     SCHEMA_NAME(t.schema_id) AS [Schema]
    ,OBJECT_NAME(i.object_id) AS [ObjectName]
    ,p.partition_number AS [Partition_Number]
    ,fg.name AS [Name_Of_FileGroup]
    ,rows AS 'Number_Of_Rows'
    ,au.total_pages AS 'Number_Of_Pages'
    ,CASE boundary_value_on_right
        WHEN 1 THEN '<'
        ELSE '<='
     END AS 'Compare'
    ,value AS 'Compare_Value'
    ,p.data_compression_desc AS 'Data'
    ,p.partition_id AS [Partition ID]
FROM sys.partitions p
    JOIN sys.indexes i ON p.object_id = i.object_id AND p.index_id = i.index_id
    JOIN sys.partition_schemes ps ON ps.data_space_id = i.data_space_id
    JOIN sys.partition_functions f ON f.function_id = ps.function_id
    LEFT JOIN sys.partition_range_values rv ON f.function_id = rv.function_id AND p.partition_number = rv.boundary_id
    JOIN sys.destination_data_spaces dds ON dds.partition_scheme_id = ps.data_space_id AND dds.destination_id = p.partition_number
    JOIN sys.filegroups fg ON dds.data_space_id = fg.data_space_id
    JOIN (SELECT container_id, sum(total_pages) as total_pages
            FROM sys.allocation_units
            GROUP BY container_id) AS au ON au.container_id = p.partition_id
    JOIN sys.tables t ON p.object_id = t.object_id
WHERE i.index_id < 2
ORDER BY 5 Desc

GO

Thursday, 5 March 2015

How we can perform a limited character’s search using LIKE?

How do I perform a limited character’s search using LIKE?


Declare @T TABLE (Id Int, Name Varchar(100))
Insert Into @T Values (1,'abc1'),(2,'def2'), (3,'ghi'), (4,'zyx'),(5,'123')

--Select * from @T
--WE WILL GET RESULTS FOR ONLY a TO b
Select * from @T
where Name like '%[a-b]%' COLLATE Latin1_General_CS_AS

--WE WILL GET RESULTS FOR ONLY d TO f
Select * from @T
where Name like '%[d-f]%' COLLATE Latin1_General_CS_AS

--WE WILL GET RESULTS FOR ONLY a TO z
Select * from @T
where Name like '%[a-z]%' COLLATE Latin1_General_CS_AS

--WE WILL GET RESULTS FOR BOTH COLUMNS WITH NUMERIC AND ALPHABETS
Select * from @T
where Name like '%[a-b0-9]%' COLLATE Latin1_General_CS_AS