Skip to content

Tag: sql-null

SQL CASE that matches value or uses LIKE ‘%’

I’m trying to do a wildcard retrieve if a defined variable is blank. But I can’t figure out the correct syntax. Answer It is as simple as this: The expression NULLIF(x, ”) will convert ” to null. Then we take advantage of how null comparison works: If @customerID is not null then first…

How to copy data from one column to another?

I am trying to add Property Address in columns that have a missing value. I use the below to identify common parcel IDs with corresponding property address since the same parcelIDs have the same PropertyAddress as well. I get this result: Now I want to add the data in column IFNULL(n.PropertyAddress,n2.Proper…

Getting values based on other column

I have the following data in SQL. Is there a way using SELECT QUERY that we can replace the NULL values in DATE column based on the REF values? Like replace the NULL values with the first available date for matching REF value, without making any change to the database. Expected Result Answer You can do it wit…

SQL Server – current inventory with physical count

I feel as though I’m missing something extremely fundamental that should be obvious. I’m basically trying to take the below data and calculate a fifth column: Inventory_Current. Example data: DateStamp ProductID Inventory_Change Inventory_Count 2021-07-01 100 -300 100000 2021-07-01 200 -700 50000 …

SQLBolt.com – Alternative Answer to Lesson 8, Q2

I have an alternative answer to the 2nd question from Lesson 8 from https://sqlbolt.com/lesson/select_queries_with_nulls Find the names of the buildings that hold no employees My attempt is: SELECT building_name FROM buildings WHERE building_name NOT IN(SELECT DISTINCT(building) FROM employees); It’s no…

Postgresql – Compare a string with a null value

I’m a bit puzzled because I believe the answer to this question is fairly simple but I’ve searched and tried several options and couldn’t find the right answer. The database is a PostgreSQL 13.1 I am using an API which sends a JSON object to a stored function in the database as follows: The …

select conditionally with conditional joins in mysql

I am trying to join two tables, people and sales, and displaying results based on a where condition which should be used to join the tables. My current attempt is showing only one result but I want all the rows in the people to be shown regardless of whether they have a record in the sales table, if they have