Answered by:
How Do I Allow Particular Letters in a VARCHAR Column and Disallow others

Question
-
I have a table where figures are entered for certain goods, these figures are used in a calculation. The problem is I need to be able to put NA (Not applicable) in these same columns. I have been able to do it by making the table Varchar and using CAST to turn this VARCHAR table to Float. My syntax is as follows
SELECT BID, SUM(CAST(Bikes AS FLOAT)) AS TotalBikes, Year
FROM dbo.dept
WHERE (Bikes <> 'NA')
GROUP BY BID, YearThe Problem is as follows I would like person to enter numbers and only 'NA' how do I prevent them from entering other letters besides NA. The table is already set
THANKS
Thursday, November 24, 2011 10:55 PM
Answers
-
You can do it as demonstrated in the following T-SQL example:
USE tempdb; GO CREATE TABLE Product (ID INT IDENTITY(1,1) PRIMARY KEY, Bikes varchar(64) NOT NULL); GO ALTER TABLE dbo.Product ADD CONSTRAINT CK_Product CHECK (ISNUMERIC([Bikes]) = 1 OR [Bikes]='NA') GO INSERT Product(Bikes) VALUES ('AB') -- fail INSERT Product(Bikes) VALUES ('NA') -- ok INSERT Product(Bikes) VALUES ('123') -- ok GO DROP TABLE tempdb.dbo.Product
You may want to use a different check than ISNUMERIC.
Kalman Toth, SQL Server & Business Intelligence Training; SQL 2008 GRAND SLAM- Marked as answer by Longly6000 Tuesday, November 29, 2011 5:39 PM
Thursday, November 24, 2011 11:15 PM
All replies
-
Why not simply make the column nullable, and use NULL to represent "Not applicable"? That is exactly what NULL values are for. You can use coalesce to display N/A when needed:
coaalesce(ltrim(str(Bikes)), 'N/A')
You need to convert the number to a string for presentation, since else you will get an error since varchar has lower data-type precedence than int.
An even better alternative is to put the N/A thing in the presentation layer only.
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se- Proposed as answer by Naomi N Friday, November 25, 2011 2:11 AM
- Marked as answer by Kalman Toth Tuesday, November 29, 2011 12:32 PM
- Unmarked as answer by Kalman Toth Tuesday, November 29, 2011 12:33 PM
- Unproposed as answer by Kalman Toth Tuesday, November 29, 2011 12:34 PM
- Proposed as answer by Naomi N Tuesday, November 29, 2011 3:55 PM
Thursday, November 24, 2011 11:14 PM -
You can do it as demonstrated in the following T-SQL example:
USE tempdb; GO CREATE TABLE Product (ID INT IDENTITY(1,1) PRIMARY KEY, Bikes varchar(64) NOT NULL); GO ALTER TABLE dbo.Product ADD CONSTRAINT CK_Product CHECK (ISNUMERIC([Bikes]) = 1 OR [Bikes]='NA') GO INSERT Product(Bikes) VALUES ('AB') -- fail INSERT Product(Bikes) VALUES ('NA') -- ok INSERT Product(Bikes) VALUES ('123') -- ok GO DROP TABLE tempdb.dbo.Product
You may want to use a different check than ISNUMERIC.
Kalman Toth, SQL Server & Business Intelligence Training; SQL 2008 GRAND SLAM- Marked as answer by Longly6000 Tuesday, November 29, 2011 5:39 PM
Thursday, November 24, 2011 11:15 PM -
Thanks I just have to Tweek it to meet my complete requirements but any suggestion on an alternative to ISNUMERICTuesday, November 29, 2011 5:39 PM
-
The alternative is
This ensures that the field only allows digits. We can also add an extra check for Col LIKE '[1-9]%', so it will disallow 0 as the first digit.Col NOT LIKE '%[^0-9]%'
For every expert, there is an equal and opposite expert. - Becker's Law
My blogTuesday, November 29, 2011 6:55 PM