Skip to content

Tag: sql-server

How to use IS NOT NULL on a case when?

I have the below SQL query in SSMS where I want to exclude NULL rows that show up in the ‘intercompany budget’ columns. I’ve tried to search for answers and putting a WHERE clause for ‘Intercompany Budget’ IS NOT NULL, like in the query below, but NULL rows still show up. Any sug…

SQL Select One Record over another based on column value

What I am trying to do is select rows based off of a ‘priority’. Say I have this: I want to choose one row each for the control ids (the whole row), which would be the third row, because there is no priority, and the 2nd row becuase it has priority. So if I have two control IDs that are

How to sum and pivot a table in SQL?

I have the following table: col_a col_b col_c 1 0 1 0 1 0 I want to sum up each column and assign a value to it based on the output, with the output like-so below: column sum string col_a 1 ‘string one’ col_b 1 ‘string two’ col_b 1 ‘string three’ I’ve tried the follow…

SQL Server 2017 Convert Varchar to Date

I have a date stored as nvarchar which I cannot seem to convert to a date. I have tried the following but get the error message Conversion failed when converting date and/or time from character string Any ideas here? Answer Find the values that are causing the problem using try_convert(): Once you see what va…