Clearing blank entries in database using WHERE - mysql

When trying the following command below, my database still shows null values. Could anyone please assist?
Update AmazingAmazonians.Product set URL= "http://www.google.com" where URL='';

There's a world of difference between an empty string and actual NULL values. You may need:
WHERE URL IS NULL
Remember, NULL is not comparable with = or !=, you need to use the IS or IS NOT operators.

Related

Convert NULL into meaningful value with MAX function - mySQL

Can anybody please help me out with an incredibly frustrating line of code in mySQL.
This is a continuation of this case: "Pivoting" a key|value table using case returns blank columns... mySQL
The code provided in that sample works, but now I'm moving to production, there's one line I cannot get to work correctly. I suspect this is due to NULL values within the max clause, but I'm not sure of how to workaround.
I would like the troublesome line to be:
MAX(case when `meta_key`='key1' and (`meta_value`='Validated' or `meta_value` is null) then 'Validated' else 'Not Validated' end) as `key1`
Ie, NULL = validated; Validated = Validated; Anything else = Not Validated.
Updated
It seems the issue is that the null is not caused by a corresponding key|value with the value being null, but by there not being a corresponding key|value pair for that user. This is creating odd results.
See http://sqlfiddle.com/#!9/90f2d9/1 for example.
The following seems to work as you would like:
IFNULL(MAX(CASE WHEN `meta_key`='key1' AND `meta_value`<>'Validated' THEN'Not Validated' END),
'Validated') as `key1`
I've reversed your logic here, so anything that has a value for key1 that is not validated will return Not Validated, anything else will return NULL. I then use IFNULL() to replace all NULLs with Validated
Example on SQL Fiddle

Simple query condition in sql

I have a Query - - < This Is a valid Query >
'SELECT *
FROM MyTable
WHERE city= ?
ORDER BY name ' [Keyname]
I am using this queried condition :: i am passing the Keyname as params from client to this sql query
This works & i get the required result BUT
If i pass nothing say null comes from client as param value for Keyname......... this query fails
how can i make the better query ... so that even if null comes ....
ORDER BY condition is satisfied
Or
R there other solution i need to look for
If so ... what is it ?
Hope i am clear
[EDIT]
CASE1:: for the url
http://54.218.73.244:7005/DescriptionSortedSearchRating/?Key=Pune
my told query satisfies::
But
http://54.218.73.244:7005/DescriptionSortedSearchRating/?Key=
my query fails, my sql query is expecting a Key for http://54.218.73.244:7005/DescriptionSortedSearchRating/ ..... if i pass nothing my query dosent get me a result..
.
what i am trying to see is even if i get nothing as key ORDER BY condition must be met ...
IF I PASS A KEY VALUE
IF I DONT PASS A KEY VALUE
You can clearly see i am not able to fetch results from database (Empty JSON)
This question doesn't have anything to do with MySQL. This is 100% your high level language. The null value the [Keyname] has is a null value in the language you're using to create the string that will be the final query.
The simplest solution will be not to assign null to your [Keyname] variable but rather an empty string.
You may use this:
ORDER BY name CASE WHEN Keyname IS NULL THEN '' ELSE CONCAT(',', Keyname) END
I am not sure whether the syntax is fine or not. But what I expect here to append empty string when Keyname is null and to append the Keyname with a comma (,). Please try it.
Other option is using function ISNULL
ISNULL(Keyname, '');

How do you properly update a mysql field with NULL?

How does one properly update a mysql field with a NULL value when using a variable in the sql query?
I have a variable called $timestamp. When it's set to date( Y-m-d h:i:s ) I have to wrap it in quotes because I'm passing a string in my mysql query. When $timestamp is set to NULL, the database query contains '' as the value for $timestamp and the field updates to 0000-00-00 00:00:00. It's important to keep this field as NULL to show that the process has never been run before.
I don't want to use now() because then my sql statement is not in sync with my class variable $timestamp.
I don't want to set $timestamp to 'NULL' because then that variable is not accurate. It's no longer NULL, it's set to a string that contains the word NULL.
What am I missing here?
The correct SQL syntax to set a column to NULL is:
UPDATE Table SET Column = NULL WHERE . . .
(note the lack of quotes around the literal NULL).
Are you performing this UPDATE using SQL or using some kind of framework? If a framework, it should recognize NULL values and pass them to the database correctly for you.
After a lot of research, I've found that this is a well known problem with no good solution if you are writing your sql queries outright.
The correct solution is to use a database abstraction layer like PDO ( for PHP ), or Active Record ( used in frameworks like Codeignitor and Ruby on Rails ).

Filter null, empty values in a datasheet

In an MS Access datasheet, is it possible to filter those records whose value is null or empty in a specific field in my table?
Yes.
WHERE myCol IS NULL OR LEN(myCol) = 0
In your SQL statement:
WHERE Nz(CompanyName, '') = ''
In your form's filter property:
Nz(CompanyName, '') = ''
The solution posted by Mitch Wheat will also work. I'm not certain which one will offer the best performance. If you are using Mitch's solution and you are also using other conditions in your Where clause, don't forget to insert parenthesis like this:
WHERE (myCol IS NULL OR LEN(myCol) = 0) AND myCol2 = True
Using a function in the criterion means you can't use the indexes. #Mitch Wheat's solution will use the index for the Is Null test, but not for the Len() test.
A sargable WHERE clause would be this:
WHERE myCol Is Null OR myCol = ""
But I'd recommend turning off "allow zero-length strings" and then you need only do the Null test, which on an indexed field will be extremely fast.
For what it's worth, I find it extremely annoying that MS changed the defaults for new text fields to have Allow ZLS turned on. It means that I have to change every one of them when I'm using the table designer to create new fields.

MS Access default value

I have a text column in one of my MS Access tables that is empty be default when a new record is inserted. The problem I have is that I can't seem to check whether this field is empty with EITHER equals Null or equals "" . For example, neither of these "reads" the field as being empty:
If [Field] = "" Or [Field] = Null Then
I've always used Access 2007 to develop this database, and I recently opened it with Access 2003, which is when I think this problem started. Could that be the cause? If so, would simply opening and saving it again with Access 2007 solve it?
When you compare against null, you need to use the IsNull function. With traditional ANSI SQL logic, Null <> Null, so you need a special function to test for null.
If [Field] = "" Or IsNull([Field])
First off, I would suggest that you do one of two things:
set your fields to disallow zero-length strings. That way you'd have to test only Is Null.
if you feel you must allow storage of ZLS, then set the default value to a ZLS. It's still possible for the field to end up Null, though, so this may or may not be helpful.
I don't see storage of ZLS's as having any utility whatsoever. It's almost always a shortcut to accomodate data being appended from sources that return ZLS's instead of Nulls for empty fields.
You should also read up on Nulls. Allen Browne has a number of pages that explain it all quite well:
Nulls: Do I need them?
Common Errors with Null
Aspects of working with Nulls in VBA code:
Nothing? Empty? Missing? Null?
The articles are Access-oriented, but could be valuable to those using any database, particularly relative novices because of the conversational style of the writing.
You should try:
If Nz([Field], "") = "" Then
Because:
If [Field] = ""
in:
If [Field] = "" Or IsNull([Field])
Would still throw a Null Error!