Skip to content

Instantly share code, notes, and snippets.

@clintmjohnson
Last active October 13, 2015 17:32
Show Gist options
  • Save clintmjohnson/76b9cf046d2584f488bd to your computer and use it in GitHub Desktop.
Save clintmjohnson/76b9cf046d2584f488bd to your computer and use it in GitHub Desktop.
This CASE Statement checks to see if a string is a Valid UPC Number or Not. - It verifies that the string has only Numbers, and its length is 12
USE DatabaseNameHere
SELECT
-- This CASE Expression Verifies that the Value in the UPC Field is All Numbers and It's Total Length is 12
,CASE WHEN ISNUMERIC(UPC) = 1 AND LEN([UPC]) = 12 THEN [UPC]
WHEN ISNUMERIC(UPC) = 1 AND LEN([UPC]) = 11 THEN '0'+[UPC]
ELSE '' END AS [UPC]
FROM dbo.YourTableNameHere
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment