Click here to Skip to main content
14,734,133 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i want to validate three conditions in mobile number using sql server as follows;


The mobile number should not start with number zero.
The mobile number should not be null
The mobile number should not exceed 10 digits


for that how can i write the sql query.
please help me

Rgds,
Narasiman P.
Posted
Comments
Siddhesh Patankar 2-Jul-13 3:54am
   
Please tell the datatype you are using to in database to save save mobile no?? Is it varchar or bigint
[no name] 2-Jul-13 4:14am
   
it is varchar
Pheonyx 2-Jul-13 4:27am
   
Why do you want to do this validation in SQL? and not in the program (application/webpage) where the data is initially entered?

1 solution

You can use in the stored procedure used to INSERT,UPDATE during insertion to check the validity

CREATE PROCEDURE sproc_InsertMobNumber
@mobnumber varchar(10)
BEGIN
DECLARE @isvalid bit
SET @isvalid=0
IF(@mobnumber IS NOT NULL AND LEN(@mobnumer) = 10 AND SUBSTRING(@mobnumber,0,1) <> 0 )
BEGIN

--INSERT STATEMENT HERE
SET @isvalid=1
END
SELECT @isvalid
END

If you want to give different error messages just use the conditions in IF to do so.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900