Added sql function from Harkirat