Click here to Skip to main content
15,884,298 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
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
Updated 26-Jul-21 22:57pm
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?

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.
 
Share this answer
 
This can be done much more concisely and is potentially better in a function (depending on solution requirements):

CREATE FUNCTION dbo.MobileNumber_isValid (@MobileNo VARCHAR(50))
RETURNS bit
AS
BEGIN
RETURN CASE WHEN @MobileNo LIKE '[1-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]' THEN 1 ELSE 0 END;
END
 
Share this answer
 

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