I am trying to create a script that deletes any rows in my data extension that have a certain field value of null. SSJS and Ampscript seem to be able to do this, but this is my first Marketing Cloud script and I have no Ampscript/little JavaScript experience. Any advice is appreciated!
-
For questions like this, it is usually best to include a good faith attempt or sample code of what you are trying to do, even if you have no experience. The forum is here to help support, not provide development services.– GortoningtonJun 5, 2018 at 12:57
-
1I understand and will do better in the future. It was a little intimidating to post my code. First time poster. Trying to get my feet by tackling some real-world problems after self-practicing with a few languages in the past.– MountainJun 5, 2018 at 16:56
-
Glad to have you join the community and never feel intimidated - we are a forum for solving issues and helping, not for judging or berating.– GortoningtonJun 5, 2018 at 17:08
2 Answers
When dealing with NULL values I usually tackle this with a combination of SQL query and SSJS script. If you are trying to delete a row based on an actual value then you can easily skip the SQL step.
- Create a SQL query to SELECT any record where the certain field IS NULL and set a field on that record to something like 'FlagRemove' on UPDATE.
- Run a SSJS script like
var rows = Platform.Function.DeleteData('Data_extension_name',['FlagRemove'],['true']);
-
Using a "flag" field to delete has worked well, but is there a way for me to delete the field when it is no longer needed? This is only to cleanup a few error records and get familiar with scripting. The script will not be running much, if any, in the future.– MountainJun 5, 2018 at 16:22
The simplest way I have found this is to not even use any AMPSCript or SSJS.
I use 2 SQL statements (and 2 DEs) that would run in an automation.
SQL 1:
SELECT yourColumns FROM yourOriginalDE WHERE notNullColumn IS NOT NULL
Have this target the secondDE and have the ACTION set to overwrite.
SQL 2:
SELECT * FROM secondDE
Have this target yourOriginalDE and have the ACTION set to overwrite.
With the action of overwrite, it will delete all records and then fill in only those that were pulled in the SQL query. Thus, removing all the NULL values without a DELETE function.
By using SQL only I find it usually runs much quicker than using AMPScript or SSJS and it also is much simpler to write/edit.
-
In Marketing Cloud I am using Automation Studio - Activities to access SQL Query. It will not let me use INTO, only query. What should I do?– MountainJun 5, 2018 at 16:50
-
There is no need to use 'INTO' you write the query as shown above (replacing placeholders with your columns and DE names) and then the action radio buttons (not in the SQL) set to overwrite. This should be on the screen where you select the target DE. Jun 5, 2018 at 17:07
-