473,657 Members | 2,953 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

check constraint on char field

I have a field in my pgsql 7.4.2 table that is char(6) type. This holds
an ID that contains only numbers, but must be six characters in length.
First two chars are the year followed by a sequential number. For
instance, a number entered for the time this year would be '040001'. I
was trying to setup a check constraint to make sure only number were
used, this is what I have, but it is allowing alphanumerics:

projectno >= '000000' AND projectno <= '999999'

Or maybe just a trigger to create the number itself would be better?

--
Robert
---------------------------(end of broadcast)---------------------------
TIP 7: don't forget to increase your free space map settings

Nov 23 '05 #1
2 3317
On Wed, 2004-06-09 at 10:42, Robert Fitzpatrick wrote:
I have a field in my pgsql 7.4.2 table that is char(6) type. This holds
an ID that contains only numbers, but must be six characters in length.
First two chars are the year followed by a sequential number. For
instance, a number entered for the time this year would be '040001'. I
was trying to setup a check constraint to make sure only number were
used, this is what I have, but it is allowing alphanumerics:

projectno >= '000000' AND projectno <= '999999'

Or maybe just a trigger to create the number itself would be better?


You've chosen the wrong type. char(6) will ALWAYS be 6 characters long,
no matter how short a string you try to put it in. I.e. if you insert
'abc' it will become 'abc ' on insert. You also need to cast. Try
this:

smarlowe=> create table test (id varchar(6) check (id::text::int> 0 and
id::text::int<1 000000 and length(id)=6));
CREATE TABLE
smarlowe=> insert into test values ('6');
ERROR: new row for relation "test" violates check constraint "test_id"
smarlowe=> insert into test values ('601');
ERROR: new row for relation "test" violates check constraint "test_id"
smarlowe=> insert into test values ('abc123');
ERROR: invalid input syntax for integer: "abc123"
smarlowe=> insert into test values ('000123');
INSERT 17263 1

---------------------------(end of broadcast)---------------------------
TIP 4: Don't 'kill -9' the postmaster

Nov 23 '05 #2
On Wed, 2004-06-09 at 10:42, Robert Fitzpatrick wrote:
I have a field in my pgsql 7.4.2 table that is char(6) type. This holds
an ID that contains only numbers, but must be six characters in length.
First two chars are the year followed by a sequential number. For
instance, a number entered for the time this year would be '040001'. I
was trying to setup a check constraint to make sure only number were
used, this is what I have, but it is allowing alphanumerics:

projectno >= '000000' AND projectno <= '999999'

Or maybe just a trigger to create the number itself would be better?


You've chosen the wrong type. char(6) will ALWAYS be 6 characters long,
no matter how short a string you try to put it in. I.e. if you insert
'abc' it will become 'abc ' on insert. You also need to cast. Try
this:

smarlowe=> create table test (id varchar(6) check (id::text::int> 0 and
id::text::int<1 000000 and length(id)=6));
CREATE TABLE
smarlowe=> insert into test values ('6');
ERROR: new row for relation "test" violates check constraint "test_id"
smarlowe=> insert into test values ('601');
ERROR: new row for relation "test" violates check constraint "test_id"
smarlowe=> insert into test values ('abc123');
ERROR: invalid input syntax for integer: "abc123"
smarlowe=> insert into test values ('000123');
INSERT 17263 1

---------------------------(end of broadcast)---------------------------
TIP 4: Don't 'kill -9' the postmaster

Nov 23 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
9061
by: RAD | last post by:
I am working with an evaluation copy of SQL Server 2000 for the first time; my DB experience lies with MS Access. I have a simple table in SQL Server (tblCompany) that has a field called "Ticker." When new company stock tickers (i.e., MSFT for Microsoft) are entered into the field, I'd like them in all caps--whether the user types msft, Msft, MsFt, etc. In Access, this was easy--simply set the Format to ">" in table design view. In...
3
1704
by: Mary Walker | last post by:
Hi, I am new to database development and am writing a database as part of a university course I have created a table as below called CableWire - the table is created ok. CREATE TABLE CableWire (CableWireID CHAR(7), BSstandard CHAR(16),
1
2858
by: Dmitry Martynov | last post by:
Hi I have a question whether XmlValidatingReader doesn't check keyref constrain (the same with key constraint) or I do smth wrong. I have the following schema <?xml version="1.0" encoding="utf-8"?> <xs:schema id="module" targetNamespace="http://tempuri.org/module.xsd"
2
8236
by: kaming | last post by:
Dear all, Is it possible to define a check constraint on a field that the field value must exist in another table non-unique field? how to write the create table DDL? i.e TABLE A TYPE, REGION Apple, A
0
418
by: Robert Fitzpatrick | last post by:
I have a field in my pgsql 7.4.2 table that is char(6) type. This holds an ID that contains only numbers, but must be six characters in length. First two chars are the year followed by a sequential number. For instance, a number entered for the time this year would be '040001'. I was trying to setup a check constraint to make sure only number were used, this is what I have, but it is allowing alphanumerics: projectno >= '000000' AND...
3
6316
by: ferg | last post by:
I have a Customer table. The table has two different CHECK constraints. Then there is the Customer details dialog, which provides the user with an UI for changing users. I have some UPDATE sql, which is called once the user clicks the OK button on this dialog. try { int rows = cmd.ExecuteNonQuery(); } catch(SqlException se)
5
2468
by: new | last post by:
Sorry to bug you with what is a simple question but if you could point me in the right direction I can follow up. I have a sql table field we can call XXX it is the least significant of a four field primary key (aaa bbb ccc). When XXX is of the form A99 (i.e. the letter A followed by two digits) I want to ensure that it is the only aaa bbb ccc of that form. If I had a function ISof FormA99(XXX) that returned either "T" when it was
2
4952
by: vidhyat | last post by:
Hi, I need to create a table in db2 with 3 columns say col1,col2,col3 in db2. For col1 i need to add a check constraint to check whether the first 15 characters are numbers & remaining 4 characters are blank spaces. i tried it in the following way,but its not working. create table sample ( col2 TIMESTAMP NOT NULL, col1 CHAR(19) NOT NULL CONSTRAINT Acc_Num_Frmt LIKE '* ', col3 CHAR(1) NOT NULL,
1
3630
by: thesti | last post by:
hi, i have a field named 'nim', in my table which is a char(10) field. i want to create a constraint that checks the field so the field must be 10 characters long and each character is a number. i can only think of nim like ' .... ' . i can't use isnumeric() because it may still accept dot character right? anyway i've tried to use the isnumeric function but i get this " error Incorrect syntax near ')' ". i don't know what's wrong...
0
8384
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8302
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8499
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8601
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7314
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5630
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4300
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2726
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1937
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.