<p>If there was no default set beforehand, then your previous elements will have a value of <code>null</code> for that field. You should be able to include those in your "on" queries by querying for that null value <em>as well as</em> the "on" value in your templates:</p>

<pre><code>{% set onEntries = craft.entries.yourLightswitch([null, '1']).find %}
</code></pre>

<p>This won't actually change the value of your previous elements though. One way to do that would be using a SQL query against the <code>craft_content</code> table to update rows (elements) that have <code>null</code> in that column:</p>

<pre><code>UPDATE `craft_content` SET `field_yourLightswitch` = 1 WHERE `field_yourLightswitch` IS NULL;
</code></pre>

CC BY-SA 3.0
2015-09-01T15:27:06.300Z
Thanks for this. I really like the first idea, but looking at the `content` table, it looks like the value has been set to `0` (rather than `null`) for existing entries, so unfortunately the first approach probably won't work for me... However, that SQL query does the job perfectly!
Nick F
CC BY-SA 3.0
2015-10-21T16:09:45.340Z
The SQL as it stands won't affect anything seeing as all the fields have values of `0` to start with. And if you change it to `...WHERE field_yourLightswitch is 0` then you could be setting it to `1` for entries that don't actually have that field in the CMS. I'm a bit wary about running this...
Phil Gyford
CC BY-SA 4.0
2018-08-22T20:52:31.530Z
using NULL no rows affected, but when I change to 0 then I get SQL error. Anyone know how to fix this? `[SQL]UPDATE` `craft_content`` SET ``field_enableAdvertisingBlock`` = 1 WHERE ``field_enableAdvertisingBlock`` IS 0;
[Err] 1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '0' at line 1`  -- ignore the double backticks.. code comments are buggy.
CreateSean
CC BY-SA 4.0
2018-08-24T08:30:10.533Z
@CreateSean You'll want to use an `=` instead of `IS` for that, but that will update every single "off" switch to "on". It may have been that something has changed with the default column value for lightswitches since this answer, if you're not getting anything returned when using `IS NULL`
Mike Pepper
CC BY-SA 3.0
2015-09-01T15:10:06.250Z
2015-09-01T15:10:06.250Z
Mike Pepper
<p>I've created a lightswitch field that defaults to "On", and it works fine for new entries, but for all existing entries it is set to "Off". I'm keen to avoid having to manually go through all existing entries switching it to "On" - is there a way of making existing entries respect the default value of this new field (rather than treating the absence of a value for it from when they were created as "Off")?</p>

<p>(Or alternatively: is there an easy way to set a field to "On" for all existing entries?)</p>

<field><lightswitch><default>
Can I add an enabled-by-default lightswitch field to existing entries?
# Can I add an enabled-by-default lightswitch field to existing entries?

(Asked by: username_0 on 2015-09-01)

I've created a lightswitch field that defaults to "On", and it works fine for new entries, but for all existing entries it is set to "Off". I'm keen to avoid having to manually go through all existing entries switching it to "On" - is there a way of making existing entries respect the default value of this new field (rather than treating the absence of a value for it from when they were created as "Off")?

(Or alternatively: is there an easy way to set a field to "On" for all existing entries?)

**Tags:** field, lightswitch, default


---

## 1 answers

### Answer by username_1 on 2015-09-01. Score: 8

If there was no default set beforehand, then your previous elements will have a value of null for that field. You should be able to include those in your "on" queries by querying for that null value as well as the "on" value in your templates:
{% set onEntries = craft.entries.yourLightswitch([null, '1']).find %}

This won't actually change the value of your previous elements though. One way to do that would be using a SQL query against the craft_content table to update rows (elements) that have null in that column:
UPDATE `craft_content` SET `field_yourLightswitch` = 1 WHERE `field_yourLightswitch` IS NULL;

> **Comments:**
> **username_0**: Thanks for this. I really like the first idea, but looking at the `content` table, it looks like the value has been set to `0` (rather than `null`) for existing entries, so unfortunately the first approach probably won't work for me... However, that SQL query does the job perfectly!  
> **username_2**: The SQL as it stands won't affect anything seeing as all the fields have values of `0` to start with. And if you change it to `...WHERE field_yourLightswitch is 0` then you could be setting it to `1` for entries that don't actually have that field in the CMS. I'm a bit wary about running this...  
> **username_3**: using NULL no rows affected, but when I change to 0 then I get SQL error. Anyone know how to fix this? `[SQL]UPDATE` `craft_content`` SET ``field_enableAdvertisingBlock`` = 1 WHERE ``field_enableAdvertisingBlock`` IS 0; [Err] 1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '0' at line 1`  -- ignore the double backticks.. code comments are buggy.  
> **username_1**: @username_3 You'll want to use an `=` instead of `IS` for that, but that will update every single "off" switch to "on". It may have been that something has changed with the default column value for lightswitches since this answer, if you're not getting anything returned when using `IS NULL`  

---

