SQL case fixes and 1 proc fix