assigning IS NULL to a string before querying DB - sql

I have an excel spreadsheet with 15 or so fields. Wat I'm doing is, i open it, grab a row of data, then check each row for a value. Then using a few criteria I go look up to see if this Client value is already in the DB. As Example
Some of the fields mind be empty. Basically after checking some of the fields, I use them to check if that record exists already in DB. the problem arises when some fields are empty in which case when I query sql server it looks something like...
Select * from TblA where Company='Apple' and CompanyAdd ='Cupertino' and City=''
Because City = '' - it doesnt not find anything in SQL. The only thing that works is
and City is NULL
How am I able to programmatically assign that to a variable like CITY?
it is a string and the field in SQL is varchar
EDIT:
I want to be able to do something like this..... (as example)
if city = "" then
'I need this here to be so that....
city IS NULL
End if
So that when I query db it looks something like...
Select count(*) from TblA where City is Null
Is somethng like that possible?

You can use COALESCE for this purpose.
SELECT *
FROM TblA
WHERE COALESCE(Company, '')='Apple'
AND COALESCE(CompanyAdd, '') = 'Cupertino'
AND COALESCE(City, '') = ''
Keep in mind that the performance of this query will most likely not be stellar.

Related

SQL query executing but not affecting row

So im having some trouble with my database right now, I can't seem to update a variable, Im just looking to do a simple "UPDATE" query, this is what it looks like:
MY table
So very simple table and all I am looking to do is update the "Sent" column from no to yes.
I've been using UPDATE but for some reason this dosn't work, no syntax error just says 0 rows affected and nothing changes...
UPDATE `numbers` SET `Sent`='True' WHERE `fName`='John';
(Im hoping to change the sent value of all rows containing fName = John.... As you can see in my table John is a value in the database so this should work but for some reason doesn't) Could anyone explain why my statement is wrong and what I am not doing right?
You need to debug this. Start with the select query:
SELECT n.*
FROM numbers n
WHERE fName = 'John';
This should return no rows -- meaning that what you see is not what you got. One common problem are invisible characters at the beginning, end, or both (often just spaces). So you can try:
WHERE fName LIKE '%John'
WHERE fName LIKE 'John%'
WHERE fName LIKE '%John%'
Once you figure out what works, you can figure out what to use in the UPDATE or how to fix the data.

Handling Ignoring Empty Values in Porting SQL Data

I am in the process of porting over some data from a SQL environment to my MongoDB backend. I'm familiar with using a NULL check with your SELECT statement, like so:
SELECT * FROM clients WHERE note is not NULL ORDER BY id_number
... but in this old SQL database table I'm noticing a lot of rows where the value is not null, it's simply empty. It would be pointless to port these across. So what would it look like to prevent pulling those over -- in terms of the SELECT statement syntax?
To clarify "note" values are of type varchar. In JavaScript I would just guard against an empty string " ". Is there a way to do this with a SQL statement?
Something like that :
SELECT * FROM clients
WHERE note is not NULL
AND TRIM(note) <> ''
ORDER BY id_number;

Oracle DB LIKE query on NULL values

This question is about Oracle DB. I want to know how Oracle DB query LIKE clause works on NULL values.
Select * From table_name where column_name like 'text%'
In above query, how the Oracle db treat to rows with null value for column 'column_name'? Also how about 'NOT LIKE'.
Also, I observed that rows having NULL values are not selected for the following query.
Select * From table_name where column_name NOT LIKE 'text%' .
I don't know why rows having NULL values for the column are not in results even though they are null and hence not like 'text%' .
NULL values basically fail all comparisons. The general idea is that NULL means "I don't know what the value is". So, when you use like with the pattern 'text%', the answer is "I don't know what the value is". It is NULL.
And if you use not like, the answer is the same "I don't know what the result is".
That is how NULLs work. Even with like and not like. Even with Oracle.
First one,
When you search for column_name like 'text%', db search for string that starts with "text" not other string, it doesn't matter what will come after the text. It could be anything like text123,text stack etc.
Second one,
When you search for NOT LIKE 'text%', db search for all the columns that should not be started with text, it the column value have text it will not be in the result. it is like "atext", it will be appear in the search results.
So in both condition NULL values never match so they don't come in the results.
Hope it will help.
Try this,
SELECT *
FROM table_name
WHERE NVL(column_name,1) NOT LIKE NVL('',2) -- '' OR NULL you can use
Try this,
SELECT *
FROM table_name
WHERE NVL(column_name,1) NOT LIKE NVL('',2) -- '' OR NULL you can use

SQL WHERE is anything

I'm working on a database query via a search bar and would like it to sometimes yield all results (depending on what is inputted)
I know that for SELECT you can use * in order to select all columns. Is there similar SQL syntax: i.e. WHERE name IS * to essentially always be true?
Edit to clarify:
The nature of the clause is that a variable is used to set the name (I'm actually not able to change the clause, that was made clear). i.e. WHERE name IS [[inputName]] (inputName is the decided by the search bar)
WHERE ISNULL(name, '') = ISNULL(name, '')
(assuming that 'name' is of a string type)
Just make the column reference itself. However, if this is the only goal of your query, why are you against omitting the WHERE clause?
If you want to return all results in a SQL statement, you can simply omit the WHERE clause:
SELECT <* or field names> FROM <table>;
You should use WHERE only when you want to filter your data on a certain field. In your case you just don't want to filter at all.
Actually you don't need WHERE clause at all in this situation. But if you insist then you should write your predicate so it always returns true. This can be done many ways:
Any predicate like:
WHERE 1=1
With column:
WHERE name = name OR name is null
With LIKE:
WHERE name LIKE '%' OR name is null
With passed parameter:
WHERE name = #name OR #name is null
You can think of more of course. But I think you need the last one. Pass NULL from app layer if you want all rows.

SQL: What does NULL as ColumnName imply

I understand that AS is used to create an alias. Therefore, it makes sense to have one long name aliased as a shorter one. However, I am seeing a SQL query NULL as ColumnName
What does this imply?
SELECT *, NULL as aColumn
Aliasing can be used in a number of ways, not just to shorten a long column name.
In this case, your example means you're returning a column that always contains NULL, and it's alias/column name is aColumn.
Aliasing can also be used when you're using computed values, such as Column1 + Column2 AS Column3.
When unioning or joining datasets using a 'Null AS [ColumnA] is a quick way to make sure create a complete dataset that can then be updated later and a new column does not need to be created in any of the source tables.
In the statement result we have a column that has all NULL values. We can refer to that column using alias.
In your case the query selects all records from table, and each result record has additional column containing only NULL values. If we want to refer to this result set and to additional column in other place in the future, we should use alias.
It means that "aColumn" has only Null values. This column could be updated with actual values later but it's an empty one when selected.
---I'm not sure if you know about SSIS, but this mechanism is useful with SSIS to add variable value to the "empty" column.
When using SELECT you can pass a value to the column directly.
So something like :
SELECT ID, Name, 'None' AS Hobbies, 0 AS NumberOfPets, NULL AS Picture, '' AS Adress
Is valid.
It can be used to format nicely a query output when using UNION/UNION ALL.
Query result can have a new column that has all NULL values. In SQL Server we can do it like this
SELECT *, CAST(NULL AS <data-type>) AS as aColumn
e.g.
SELECT *, CAST(NULL AS BIGINT) AS as aColumn
How about without using the the as
SELECT ID
, Name
, 'None' AS Hobbies
, 0 AS NumberOfPets
, NULL Picture
Usually adding NULL as [Column] name at the end of a select all is used when inserting into another table a calculated column based on the table you have just selected.
UPDATE #TempTable SET aColumn = Column1 + Column2 WHERE ...
Then exporting or saving the results to another table.