Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1052087
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T17:02:14+00:00 2026-05-16T17:02:14+00:00

In SQL Server assuming one has columns that need to have the same data

  • 0

In SQL Server assuming one has columns that need to have the same data type is it possible to define a check constraint at the table (or database level) and apply that to a column when you define it?

As in this (contrived) example:

ALTER TABLE dbo.tblAuditTrail
ADD CONSTRAINT CK_DecimalNumber
CHECK (DecimalColumn LIKE '^\-?\d+\.\d+$')
GO

How now can you associate that with one or more columns having created it at the table level or is the answer to this to use a RULE viz.

CREATE RULE RU_Decimal
AS
@value LIKE '^\-?\d+\.\d+$'
GO

I know that the example is contrived and one would use a decimal column for decimal values but assume, because of a poor design choice, that this was an nchar column and you wanted to enforce some constraints on it.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T17:02:15+00:00Added an answer on May 16, 2026 at 5:02 pm

    Although rules do meet your requirements, they are now deprecated in favour of plain old check constraints. It wouldn’t be a good idea to use rules if you think the database may need to be moved to future versions of SQL Server. See the “important” message at the top of the MSDN documentation: http://msdn.microsoft.com/en-us/library/ms188064.aspx.

    You can get some reuse by using a user defined function and call it in all the check constraints but you will still need to explicitly define the check constraints on all columns where it applies.

    CREATE FUNCTION IsDecimal(@input varchar(max))
    RETURNS bit
    AS 
    BEGIN
        IF @value LIKE '^\-?\d+\.\d+$'
            RETURN 1
        ELSE
            RETURN 0
    END
    GO 
    
    ALTER TABLE dbo.tblAuditTrail     
        ADD CONSTRAINT CK_DecimalNumber     
        CHECK (dbo.IsDecimal(DecimalColumn) = 1)     
    GO
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have a client environment that has MSDE. We're looking to install SQL Server
System is: SQL Server 2008-R2 I have a table that one of its column
I currently have one server with nginx that reverse_proxy to apache (same server) for
I am missing this DLL reference in c:\program files\SQL Server\90\Tools\Binn. I'm assuming that this
I have a SQL DTS package running on a SQL Server 2005 database that,
SQL Server (2005/2008) Each of the below statements have the same result. Does anyone
I have stored procedures in SQL Server T-SQL that are called from .NET within
I have an application which uses Sql Server Express Edition to store data. I
I have a task of converting, time stored on one of my DB(Sql Server)'s
I have SQL Server 2008 Developer edition installed on my laptop. It has many

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.