{"id":62,"date":"2013-05-17T13:56:00","date_gmt":"2013-05-17T18:56:00","guid":{"rendered":"https:\/\/www.chrystalsander.com\/blog\/?p=62"},"modified":"2023-07-26T12:34:09","modified_gmt":"2023-07-26T17:34:09","slug":"sql-server-using-partition-function","status":"publish","type":"post","link":"https:\/\/www.chrystalsander.com\/blog\/?p=62","title":{"rendered":"SQL Server : Using $partition Function"},"content":{"rendered":"<div>\n<div>\n<p>Recently, I needed to validate date-range value on each partition on a partitioned table.&nbsp; To do this, I utilized the SQL Server &quot;$Partition&quot; function.&nbsp; Per MSDN, &quot;$Partition <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms188071.aspx\" target=\"_blank\" rel=\"noopener\">returns the partition number into which a set of partitioning column values would be mapped for any specified partition function<\/a>.&quot;<o:p><\/o:p><\/p>\n<p><o:p>&nbsp;<\/o:p><\/p>\n<p>The syntax is:<o:p><\/o:p><\/p>\n<p><o:p>&nbsp;<\/o:p><\/p>\n<p><span style='font-family:\"Courier New\";color:blue'>[ database_name. ] $PARTITION.partition_function_name(expression)<o:p><\/o:p><\/span><\/p>\n<p><span style='font-family:\"Courier New\";color:#002060'><o:p>&nbsp;<\/o:p><\/span><\/p>\n<p>This handy function can also be used to obtain column information, row counts, and other information you might want to know per partition:<span style='font-size:10.0pt;font-family:\"Courier New\";color:green'> <o:p><\/o:p><\/span><\/p>\n<p><span style='font-size:10.0pt;font-family:\"Courier New\";color:green'><o:p>&nbsp;<\/o:p><\/span><\/p>\n<p><span style='font-size:10.0pt;font-family:\"Courier New\";color:green'>&#8211;STEP ONE:<\/span><o:p><\/o:p><\/p>\n<p style='text-autospace:none'><span style='font-size:10.0pt;font-family:\"Courier New\";color:green'>&#8211;Get the name of your partition function:<o:p><\/o:p><\/span><\/p>\n<p><span style='font-size:10.0pt;font-family:\"Courier New\";color:blue'>SELECT<\/span><span style='font-size:10.0pt;font-family:\"Courier New\"'> NAME <span style='color:blue'>FROM<\/span> <span style='color:green'>SYS<\/span><span style='color:gray'>.<\/span><span style='color:green'>partition_functions<\/span><\/span><o:p><\/o:p><\/p>\n<p><o:p>&nbsp;<\/o:p><\/p>\n<table border=0 cellspacing=0 cellpadding=0 width=135 style='width:101.0pt;margin-left:-1.15pt;border-collapse:collapse'>\n<tr style='height:15.0pt'>\n<td width=135 nowrap valign=bottom style='width:101.0pt;border:solid windowtext 1.0pt;padding:0in 5.4pt 0in 5.4pt;height:15.0pt'>\n<p style='line-height:115%'><span style='color:black'>NAME<o:p><\/o:p><\/span><\/p>\n<\/td>\n<\/tr>\n<tr style='height:15.0pt'>\n<td width=135 nowrap valign=bottom style='width:101.0pt;border:solid windowtext 1.0pt;border-top:none;padding:0in 5.4pt 0in 5.4pt;height:15.0pt'>\n<p style='line-height:115%'><span style='color:black'>TableAPartitionFunc<o:p><\/o:p><\/span><\/p>\n<\/td>\n<\/tr>\n<\/table>\n<p><o:p>&nbsp;<\/o:p><\/p>\n<p style='text-autospace:none'><span style='font-size:10.0pt;font-family:\"Courier New\";color:green'>&#8211;STEP TWO:<o:p><\/o:p><\/span><\/p>\n<p style='text-autospace:none'><span style='font-size:10.0pt;font-family:\"Courier New\";color:green'>&#8211;Get the name of the column from which your table is partitioned<o:p><\/o:p><\/span><\/p>\n<p style='text-autospace:none'><span style='font-size:10.0pt;font-family:\"Courier New\";color:blue'>SELECT<\/span><span style='font-size:10.0pt;font-family:\"Courier New\"'> c<span style='color:gray'>.<\/span>name <span style='color:blue'>AS<\/span> PartitionedColumnName<o:p><\/o:p><\/span><\/p>\n<p style='text-autospace:none'><span style='font-size:10.0pt;font-family:\"Courier New\";color:blue'>FROM<\/span><span style='font-size:10.0pt;font-family:\"Courier New\"'>&nbsp; <span style='color:green'>sys<\/span><span style='color:gray'>.<\/span><span style='color:green'>tables<\/span> t<o:p><\/o:p><\/span><\/p>\n<p style='text-autospace:none'><span style='font-size:10.0pt;font-family:\"Courier New\";color:gray'>JOIN<\/span><span style='font-size:10.0pt;font-family:\"Courier New\"'>&nbsp; <span style='color:green'>sys<\/span><span style='color:gray'>.<\/span><span style='color:green'>indexes<\/span>&nbsp; i <o:p><\/o:p><\/span><\/p>\n<p style='text-autospace:none'><span style='font-size:10.0pt;font-family:\"Courier New\";color:blue'>ON <\/span><span style='font-size:10.0pt;font-family:\"Courier New\";color:gray'>(<\/span><span style='font-size:10.0pt;font-family:\"Courier New\"'>i<span style='color:gray'>.<\/span><span style='color:fuchsia'>object_id<\/span> <span style='color:gray'>=<\/span> t<span style='color:gray'>.<\/span><span style='color:fuchsia'>object_id<\/span> <o:p><\/o:p><\/span><\/p>\n<p style='text-autospace:none'><span style='font-size:10.0pt;font-family:\"Courier New\";color:gray'>AND<\/span><span style='font-size:10.0pt;font-family:\"Courier New\"'> i<span style='color:gray'>.<\/span>index_id <span style='color:gray'>&lt;<\/span> 2<span style='color:gray'>)<o:p><\/o:p><\/span><\/span><\/p>\n<p style='text-autospace:none'><span style='font-size:10.0pt;font-family:\"Courier New\";color:gray'>JOIN<\/span><span style='font-size:10.0pt;font-family:\"Courier New\"'>&nbsp; <span style='color:green'>sys<\/span><span style='color:gray'>.<\/span><span style='color:green'>index_columns<\/span>&nbsp; ic <o:p><\/o:p><\/span><\/p>\n<p style='text-autospace:none'><span style='font-size:10.0pt;font-family:\"Courier New\";color:blue'>ON<\/span><span style='font-size:10.0pt;font-family:\"Courier New\";color:gray'>(<\/span><span style='font-size:10.0pt;font-family:\"Courier New\"'>ic<span style='color:gray'>.<\/span>partition_ordinal <span style='color:gray'>&gt;<\/span> 0 <o:p><\/o:p><\/span><\/p>\n<p style='text-autospace:none'><span style='font-size:10.0pt;font-family:\"Courier New\";color:gray'>AND<\/span><span style='font-size:10.0pt;font-family:\"Courier New\"'> ic<span style='color:gray'>.<\/span>index_id <span style='color:gray'>=<\/span> i<span style='color:gray'>.<\/span>index_id <span style='color:gray'>and<\/span> ic<span style='color:gray'>.<\/span><span style='color:fuchsia'>object_id<\/span> <span style='color:gray'>=<\/span> t<span style='color:gray'>.<\/span><span style='color:fuchsia'>object_id<\/span><span style='color:gray'>)<o:p><\/o:p><\/span><\/span><\/p>\n<p style='text-autospace:none'><span style='font-size:10.0pt;font-family:\"Courier New\";color:gray'>JOIN<\/span><span style='font-size:10.0pt;font-family:\"Courier New\"'>&nbsp; <span style='color:green'>sys<\/span><span style='color:gray'>.<\/span><span style='color:green'>columns<\/span> c <o:p><\/o:p><\/span><\/p>\n<p style='text-autospace:none'><span style='font-size:10.0pt;font-family:\"Courier New\";color:blue'>ON <\/span><span style='font-size:10.0pt;font-family:\"Courier New\";color:gray'>(<\/span><span style='font-size:10.0pt;font-family:\"Courier New\"'>c<span style='color:gray'>.<\/span><span style='color:fuchsia'>object_id<\/span> <span style='color:gray'>=<\/span> ic<span style='color:gray'>.<\/span><span style='color:fuchsia'>object_id<\/span> <o:p><\/o:p><\/span><\/p>\n<p style='text-autospace:none'><span style='font-size:10.0pt;font-family:\"Courier New\";color:gray'>AND<\/span><span style='font-size:10.0pt;font-family:\"Courier New\"'> c<span style='color:gray'>.<\/span>column_id <span style='color:gray'>=<\/span> ic<span style='color:gray'>.<\/span>column_id<span style='color:gray'>)<o:p><\/o:p><\/span><\/span><\/p>\n<p><span style='font-size:10.0pt;font-family:\"Courier New\";color:blue'>WHERE<\/span><span style='font-size:10.0pt;font-family:\"Courier New\"'> t<span style='color:gray'>.<\/span><span style='color:fuchsia'>object_id<\/span> <span style='color:gray'>=<\/span> <span style='color:fuchsia'>LOWER<\/span><span style='color:gray'>(<\/span><span style='color:fuchsia'>object_id<\/span><span style='color:gray'>(<\/span><span style='color:red'>&#8216;yourtablename&#8217;<\/span><span style='color:gray'>))<\/span><\/span><o:p><\/o:p><\/p>\n<p><o:p>&nbsp;<\/o:p><\/p>\n<table border=0 cellspacing=0 cellpadding=0 width=165 style='width:124.0pt;margin-left:-1.15pt;border-collapse:collapse'>\n<tr style='height:15.0pt'>\n<td width=165 nowrap valign=bottom style='width:124.0pt;border:solid windowtext 1.0pt;padding:0in 5.4pt 0in 5.4pt;height:15.0pt'>\n<p style='line-height:115%'><span style='color:black'>PartitionedColumnName<o:p><\/o:p><\/span><\/p>\n<\/td>\n<\/tr>\n<tr style='height:15.0pt'>\n<td width=165 nowrap valign=bottom style='width:124.0pt;border:solid windowtext 1.0pt;border-top:none;padding:0in 5.4pt 0in 5.4pt;height:15.0pt'>\n<p style='line-height:115%'><span style='color:black'>DatePort<o:p><\/o:p><\/span><\/p>\n<\/td>\n<\/tr>\n<\/table>\n<p><o:p>&nbsp;<\/o:p><\/p>\n<p style='text-autospace:none'><span style='font-size:10.0pt;font-family:\"Courier New\";color:green'>&#8211;STEP THREE:<o:p><\/o:p><\/span><\/p>\n<p style='text-autospace:none'><span style='font-size:10.0pt;font-family:\"Courier New\";color:green'>&#8211;Use acquired data to query your partition values:<o:p><\/o:p><\/span><\/p>\n<p style='text-autospace:none'><span style='font-size:10.0pt;font-family:\"Courier New\";color:green'><o:p>&nbsp;<\/o:p><\/span><\/p>\n<p style='text-autospace:none'><span style='font-size:10.0pt;font-family:\"Courier New\";color:blue'>SELECT<\/span><span style='font-size:10.0pt;font-family:\"Courier New\"'> <span style='color:blue'>DISTINCT<\/span> <span style='color:fuchsia'>$partition<\/span><span style='color:gray'>.<\/span>TableAPartitionFunc<span style='color:gray'>(<\/span>DatePort<span style='color:gray'>)<\/span> <span style='color:blue'>AS<\/span> PartitionNumber<span style='color:gray'>,<\/span> <o:p><\/o:p><\/span><\/p>\n<p style='text-autospace:none'><span style='font-size:10.0pt;font-family:\"Courier New\";color:fuchsia'>CONVERT<\/span><span style='font-size:10.0pt;font-family:\"Courier New\";color:gray'>(<\/span><span style='font-size:10.0pt;font-family:\"Courier New\";color:blue'>VARCHAR<\/span><span style='font-size:10.0pt;font-family:\"Courier New\";color:gray'>(<\/span><span style='font-size:10.0pt;font-family:\"Courier New\"'>8<span style='color:gray'>),<\/span> DatePort<span style='color:gray'>,<\/span> 1<span style='color:gray'>)<\/span> <span style='color:blue'>AS<\/span> RangedDate<span style='color:gray'>,<\/span> <span style='color:red'>&#8216;yourtablename&#8217;<\/span> <span style='color:blue'>AS<\/span> PartitionedTable<o:p><\/o:p><\/span><\/p>\n<p style='text-autospace:none'><span style='font-size:10.0pt;font-family:\"Courier New\";color:blue'>from<\/span><span style='font-size:10.0pt;font-family:\"Courier New\"'> <span style='color:green'>yourtablename<o:p><\/o:p><\/span><\/span><\/p>\n<p><span style='font-size:10.0pt;font-family:\"Courier New\";color:blue'>ORDER<\/span><span style='font-size:10.0pt;font-family:\"Courier New\"'> <span style='color:blue'>BY<\/span> <span style='color:fuchsia'>$partition<\/span><span style='color:gray'>.<\/span> TableAPartitionFunc<span style='color:gray'>(<\/span>DatePort<span style='color:gray'>)<\/span><\/span><o:p><\/o:p><\/p>\n<p><o:p>&nbsp;<\/o:p><\/p>\n<table border=0 cellspacing=0 cellpadding=0 width=308 style='width:231.0pt;margin-left:-1.15pt;border-collapse:collapse'>\n<tr style='height:15.0pt'>\n<td width=113 nowrap valign=bottom style='width:85.0pt;border:solid windowtext 1.0pt;padding:0in 5.4pt 0in 5.4pt;height:15.0pt'>\n<p style='line-height:115%'><span style='color:black'>PartitionNumber<o:p><\/o:p><\/span><\/p>\n<\/td>\n<td width=83 nowrap valign=bottom style='width:62.0pt;border:solid windowtext 1.0pt;border-left:none;padding:0in 5.4pt 0in 5.4pt;height:15.0pt'>\n<p style='line-height:115%'><span style='color:black'>RangedDate<o:p><\/o:p><\/span><\/p>\n<\/td>\n<td width=112 nowrap valign=bottom style='width:84.0pt;border:solid windowtext 1.0pt;border-left:none;padding:0in 5.4pt 0in 5.4pt;height:15.0pt'>\n<p style='line-height:115%'><span style='color:black'>PartitionedTable<o:p><\/o:p><\/span><\/p>\n<\/td>\n<\/tr>\n<tr style='height:15.0pt'>\n<td width=113 nowrap valign=bottom style='width:85.0pt;border:solid windowtext 1.0pt;border-top:none;padding:0in 5.4pt 0in 5.4pt;height:15.0pt'>\n<p align=right style='text-align:right;line-height:115%'><span style='color:black'>1<o:p><\/o:p><\/span><\/p>\n<\/td>\n<td width=83 nowrap valign=bottom style='width:62.0pt;border-top:none;border-left:none;border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0in 5.4pt 0in 5.4pt;height:15.0pt'>\n<p align=right style='text-align:right;line-height:115%'><span style='color:black'>5\/13\/2013<o:p><\/o:p><\/span><\/p>\n<\/td>\n<td width=112 nowrap valign=bottom style='width:84.0pt;border-top:none;border-left:none;border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0in 5.4pt 0in 5.4pt;height:15.0pt'>\n<p style='line-height:115%'><span style='color:black'>yourtablename<o:p><\/o:p><\/span><\/p>\n<\/td>\n<\/tr>\n<tr style='height:15.0pt'>\n<td width=113 nowrap valign=bottom style='width:85.0pt;border:solid windowtext 1.0pt;border-top:none;padding:0in 5.4pt 0in 5.4pt;height:15.0pt'>\n<p align=right style='text-align:right;line-height:115%'><span style='color:black'>2<o:p><\/o:p><\/span><\/p>\n<\/td>\n<td width=83 nowrap valign=bottom style='width:62.0pt;border-top:none;border-left:none;border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0in 5.4pt 0in 5.4pt;height:15.0pt'>\n<p align=right style='text-align:right;line-height:115%'><span style='color:black'>5\/14\/2013<o:p><\/o:p><\/span><\/p>\n<\/td>\n<td width=112 nowrap valign=bottom style='width:84.0pt;border-top:none;border-left:none;border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0in 5.4pt 0in 5.4pt;height:15.0pt'>\n<p style='line-height:115%'><span style='color:black'>yourtablename<o:p><\/o:p><\/span><\/p>\n<\/td>\n<\/tr>\n<tr style='height:15.0pt'>\n<td width=113 nowrap valign=bottom style='width:85.0pt;border:solid windowtext 1.0pt;border-top:none;padding:0in 5.4pt 0in 5.4pt;height:15.0pt'>\n<p align=right style='text-align:right;line-height:115%'><span style='color:black'>3<o:p><\/o:p><\/span><\/p>\n<\/td>\n<td width=83 nowrap valign=bottom style='width:62.0pt;border-top:none;border-left:none;border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0in 5.4pt 0in 5.4pt;height:15.0pt'>\n<p align=right style='text-align:right;line-height:115%'><span style='color:black'>5\/15\/2013<o:p><\/o:p><\/span><\/p>\n<\/td>\n<td width=112 nowrap valign=bottom style='width:84.0pt;border-top:none;border-left:none;border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0in 5.4pt 0in 5.4pt;height:15.0pt'>\n<p style='line-height:115%'><span style='color:black'>yourtablename<o:p><\/o:p><\/span><\/p>\n<\/td>\n<\/tr>\n<tr style='height:15.0pt'>\n<td width=113 nowrap valign=bottom style='width:85.0pt;border:solid windowtext 1.0pt;border-top:none;padding:0in 5.4pt 0in 5.4pt;height:15.0pt'>\n<p align=right style='text-align:right;line-height:115%'><span style='color:black'>4<o:p><\/o:p><\/span><\/p>\n<\/td>\n<td width=83 nowrap valign=bottom style='width:62.0pt;border-top:none;border-left:none;border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0in 5.4pt 0in 5.4pt;height:15.0pt'>\n<p align=right style='text-align:right;line-height:115%'><span style='color:black'>5\/16\/2013<o:p><\/o:p><\/span><\/p>\n<\/td>\n<td width=112 nowrap valign=bottom style='width:84.0pt;border-top:none;border-left:none;border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0in 5.4pt 0in 5.4pt;height:15.0pt'>\n<p style='line-height:115%'><span style='color:black'>yourtablename<o:p><\/o:p><\/span><\/p>\n<\/td>\n<\/tr>\n<\/table>\n<p><o:p>&nbsp;<\/o:p><\/p>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Recently, I needed to validate date-range value on each partition on a partitioned table.&nbsp; To do this, I utilized the SQL Server &quot;$Partition&quot; function.&nbsp; Per MSDN, &quot;$Partition returns the partition number into which a set of partitioning column values would be mapped for any specified partition function.&quot; &nbsp; The syntax is: &nbsp; [ database_name. ] &hellip; <a href=\"https:\/\/www.chrystalsander.com\/blog\/?p=62\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;SQL Server : Using $partition Function&#8221;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[27],"tags":[52,50],"_links":{"self":[{"href":"https:\/\/www.chrystalsander.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/62"}],"collection":[{"href":"https:\/\/www.chrystalsander.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.chrystalsander.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.chrystalsander.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.chrystalsander.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=62"}],"version-history":[{"count":1,"href":"https:\/\/www.chrystalsander.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/62\/revisions"}],"predecessor-version":[{"id":176,"href":"https:\/\/www.chrystalsander.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/62\/revisions\/176"}],"wp:attachment":[{"href":"https:\/\/www.chrystalsander.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=62"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.chrystalsander.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=62"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.chrystalsander.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=62"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}