{"id":220,"date":"2013-04-02T21:26:30","date_gmt":"2013-04-02T15:26:30","guid":{"rendered":"http:\/\/markwheeler.com\/wordpress\/?p=220"},"modified":"2013-04-02T21:26:30","modified_gmt":"2013-04-02T15:26:30","slug":"ssrs-query-for-nulls-and-order-by-nulls-last","status":"publish","type":"post","link":"https:\/\/www.markwheeler.com\/?p=220","title":{"rendered":"SSRS &#8211; Query for NULLs and Order by NULLs last"},"content":{"rendered":"<p>Had a request to create a report for Project Server milestones. There was a custom enterprise column added for tasks which contained a location code. My report had to return all values including where location was NULL.<\/p>\n<p>The first step was to get a valid NULL parameter in my Site parameter drop-down menu.<\/p>\n<p>SELECT DISTINCT(LT.MemberValue) as Site<br \/>\nFROM MSP_EpmTask_UserView AS T<br \/>\nINNER JOIN MSP_EpmProject_UserView AS P ON P.ProjectUID=T.ProjectUID<br \/>\nleft join MSP_EpmCustomFieldAssociation on EntityUID = T.TaskUID<br \/>\nleft join MSP_EPMLookupTable LT on lt.memberuid = lookupmemberuid<br \/>\nWHERE T.TaskIsMilestone=1<br \/>\nAND TaskFinishDate &gt; GETDATE()<br \/>\nAND LT.MemberValue IS NOT NULL<br \/>\n&#8212; Here&#8217;s where we add NULL for the menu.<br \/>\nUNION<br \/>\nSELECT &#8216;(NULL)&#8217; AS Expr1<br \/>\nORDER BY LT.MemberValue<br \/>\n<img loading=\"lazy\" decoding=\"async\" class=\"alignnone\" alt=\"\" src=\"http:\/\/www.markwheeler.com\/images\/sites.png\" width=\"517\" height=\"220\" \/><\/p>\n<p>Second step was to create the dataset query to accept all values including NULLs.<\/p>\n<p>SELECT ISNULL(Lt.MemberValue,'(NULL)&#8217;) as Site, P.ProjectName AS Project, P.PM AS Author,\u00a0P.ProjectStartDate AS Start, P.ProjectFinishDate AS Finish,\u00a0T.TaskName, TaskStartDate, TaskFinishDate<br \/>\nFROM MSP_EpmTask_UserView AS T<br \/>\nINNER JOIN MSP_EpmProject_UserView AS P ON P.ProjectUID=T.ProjectUID<br \/>\nleft join MSP_EpmCustomFieldAssociation on EntityUID = T.TaskUID<br \/>\nleft join MSP_EPMLookupTable LT on lt.memberuid = lookupmemberuid<br \/>\nWHERE T.TaskIsMilestone=1<br \/>\nAND TaskFinishDate &gt; GETDATE()<br \/>\nAND P.ProjectName in (@Project)<br \/>\nAND TaskStartDate &gt;= @TasksStart<br \/>\nAND TaskFinishDate &lt;= @TasksEnd<br \/>\nGROUP BY Lt.MemberValue, P.ProjectName, P.PM, P.ProjectStartDate, P.ProjectFinishDate, T.TaskName, TaskStartDate, TaskFinishDate<br \/>\norder by project, ISNULL(Lt.MemberValue, &#8216;(NULL)&#8217;)<\/p>\n<p>I then added a Filter on this dataset for the @Site parameter.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone\" alt=\"\" src=\"http:\/\/www.markwheeler.com\/images\/sites2.png\" width=\"623\" height=\"293\" \/><\/p>\n<p>Third: There was a requirement that when sorting the table, Sites with NULL values should be LAST in the list.<br \/>\nThe dataset query works in SQL Management Studio, however, not so much in SSRS. I had to put an expression in the Row group sorting field.<\/p>\n<p>=IIF(Fields!Site.Value = &#8220;(NULL)&#8221;, &#8220;ZZZ&#8221;, Fields!Site.Value)<\/p>\n<p><a href=\"http:\/\/www.markwheeler.com\/images\/sites3.png\"><img decoding=\"async\" class=\"alignnone\" alt=\"\" src=\"http:\/\/www.markwheeler.com\/images\/sites3.png\" width=\"700\" \/><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Had a request to create a report for Project Server milestones. There was a custom enterprise column added for tasks which contained a location code. My report had to return all values including where location was NULL. The first step was to get a valid NULL parameter in my Site [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"footnotes":""},"categories":[2,10,11],"tags":[85,106,111],"class_list":["post-220","post","type-post","status-publish","format-standard","hentry","category-computer","category-sharepoint","category-software","tag-project-server","tag-sql-reporting-services","tag-ssrs"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/www.markwheeler.com\/index.php?rest_route=\/wp\/v2\/posts\/220","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.markwheeler.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.markwheeler.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.markwheeler.com\/index.php?rest_route=\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/www.markwheeler.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=220"}],"version-history":[{"count":0,"href":"https:\/\/www.markwheeler.com\/index.php?rest_route=\/wp\/v2\/posts\/220\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.markwheeler.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=220"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.markwheeler.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=220"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.markwheeler.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=220"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}