Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 SQL

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2006-09-13 : 06:34:24
Luke writes "Hi

I am brand spanking new to SQL and havent much knowledge on how to do simple things.

Is there a way of running a script that would throw up an error if there were more than 10 characters and less than 10 characters?

I am thinking Left count and Right count type of commands?

Many thanks for any help you can offer.

Luke"

harsh_athalye
Master Smack Fu Yak Hacker

5581 Posts

Posted - 2006-09-13 : 06:42:32
Why bother with LEFT() or RIGHT()? Use LEN()

If len(@Param) <> 10 then
RAISERROR('Param must be 10 characters long', 16, 1)



Harsh Athalye
India.
"Nothing is Impossible"
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2006-09-13 : 09:04:49
[code]
USE Northwind
GO


CREATE TABLE dbo.myTable99
(
Col1 char(10) NULL
) ON [PRIMARY]
GO
ALTER TABLE dbo.myTable99 ADD CONSTRAINT
CK_myTable99 CHECK (LEN(Col1)=10)
GO


INSERT INTO myTable99(Col1) SELECT '1234567890'
GO

SELECT * FROM myTable99
GO

INSERT INTO myTable99(Col1) SELECT '1'
GO

DROP TABLE myTable99
GO

[/code]


Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2006-09-13 : 09:08:20
If you send data from front end then validate there

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -