Querying on length of data
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-21-2015 09:51 AM
I would like a query to find those rows in a database table where the length of the data in the field meets a certain condition. For example, I want all users where the length of the phone number is greater than 15 - this could indicate a data entry problem, for example.
How can this be done using a GlideRecord?
(I have a similar question regarding doing such queries on a List view in the UI.)
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-21-2015 10:19 AM
This example is with email, but the same can be done with phone.
var user = new GlideRecord('sys_user');
user.query();
while(user.next()){
var email = user.email.toString();
var emailLength = email.length;
if(emailLength > 25){
//do something about it
}
}
Just put it in a background script. You could also just use gs.print to print it out,.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-22-2015 03:04 AM
Thanks, Mike. I thought of what you suggested. I should have been more explicit that I don't want to do a walk through the entire table in Javascript. In SQL I could write a WHERE clause such as "where length(email)>15)" but I can't see how to do this with the GlideRecord. I seem to recall something that flashed past a while ago while I was browsing this forum but I can't find it now.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-22-2015 05:09 AM
Have you used gs.sql() before? You can put a T-SQL statement as an argument in a background script and get the output.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-22-2015 09:40 AM
Yes, I've used gs.sql to get what I want but I wanted a solution that is in a scheduled job or fix script instead. So I'm specifically after whether this can be done with a GlideRecord (its an API question), rather than a solution to the problem.