Search code examples
oracle-databaseoracle11gsql-like

Oracle procedure to check if value starts from http or https


I'm using IF condition with 'NOT LIKE' in my procedure to check if value starts from HTTP or HTTPS. But if value starts from HTTP/HTTPS or not, then also for both cases it appends base URL.

Need suggestions to fix this.

if lower(l_value) NOT LIKE 'http://%' or lower(l_value) NOT LIKE 'https://%' then
      l_value := l_baseURL || l_value;
end if;

Solution

  • The problem is just the or; that should be and:

    if lower(l_value) NOT LIKE 'http://%' and lower(l_value) NOT LIKE 'https://%' then
          l_value := l_baseURL || l_value;
    end if;
    

    The same value can't match both conditions at the same time, so it if it does match one it still won't match the other; so your original or would always be true, for any value.

    You could get the same effect by using LIKE and then negating the entire or condition:

    if NOT (lower(l_value) LIKE 'http://%' or lower(l_value) LIKE 'https://%') then
          l_value := l_baseURL || l_value;
    end if;
    

    but I don't think that's any clearer really.