Update SQL: Need to add conditions

I have a field called 'Accessrights', it holds a string of 11 digits, being 1 or 0.
I have the script below, which takes the first digit of the string and copies it to another field, then the second and so on. It all works great. Problem is that some of the strings have numbers '2' or they are null or the string is too short  (Issues from data import).

I need to add some code to the sql so that if the digit selected is NOT 1 or 0 then to make it a 0 , then I can copy the value to the appropriate field.  

Help is appreciated.  Here is the current SQL:

UPDATE  [dbo].[UserLogin]
SET     [client_personalprofile] = SUBSTRING([AccessRights], 1, 1) ,
        [client_case_geninfo] = SUBSTRING([AccessRights], 2, 1) ,
        [client_case_atty] = SUBSTRING([AccessRights], 3, 1) ,
        [client_case_comments] = SUBSTRING([AccessRights], 5, 1) ,
        [client_case_docs] = SUBSTRING([AccessRights], 6, 1) ,
        [client_case_billing] = SUBSTRING([AccessRights], 7, 1) ,
        [client_case_steps] = SUBSTRING([AccessRights], 8, 1) ,
        [client_relcases] = SUBSTRING([AccessRights], 9, 1) ,
        [client_maincase] = SUBSTRING([AccessRights], 10, 1) ,
        [client_case_forms] = SUBSTRING([AccessRights], 11, 1);

Open in new window

LVL 1
AleksAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jim HornMicrosoft SQL Server Data DudeCommented:
>I need to add some code to the sql so that if the digit selected is NOT 1 or 0 then to make it a 0 , then I can copy the value to the appropriate field.  

[column name] = CASE WHEN SUBSTRING([AccessRights], 9, 1) NOT IN (0, 1) THEN 0 ELSE SUBSTRING([AccessRights], 9, 1)  END,
-- repeat for other columns
AleksAuthor Commented:
I did a select to test and they show exactly the same values:

        
                  SELECT    CASE WHEN SUBSTRING([AccessRights], 9, 1) NOT IN (
                                      0, 1 ) THEN 0
                                 ELSE SUBSTRING([AccessRights], 1, 1)
                            END AS client_profilemodified ,
                            SUBSTRING([AccessRights], 1, 1) AS [client_personalprofile]
                  FROM      dbo.UserLogin

Open in new window


It still shows null values, numbers 2, etc.
Jim HornMicrosoft SQL Server Data DudeCommented:
Make sure the 2nd parameter (9 in above code) is the same in lines 2 and 4 for each character/column.
The 7 Worst Nightmares of a Sysadmin

Fear not! To defend your business’ IT systems we’re going to shine a light on the seven most sinister terrors that haunt sysadmins. That way you can be sure there’s nothing in your stack waiting to go bump in the night.

AleksAuthor Commented:
I apologize, you are right. The only thing missing is that if the field is null I also need to set it to '0'
Vitor MontalvãoMSSQL Senior EngineerCommented:
I would create a function to solve this problem:
CREATE FUNCTION dbo.fn_AccessRight(@AccessList varchar(MAX),@Position smallint)
RETURNS CHAR(1)
AS
BEGIN
	DECLARE @Right CHAR(1)

	IF LEN(@AccessList) < @Position
		SET @Right = '0'
	ELSE
		BEGIN
			SET @Right = SUBSTRING(@AccessList, @Position, 1)
			IF @Right NOT IN ('0', '1')
				SET @Right = '0'
		END
	RETURN @Right
END
GO


UPDATE  [dbo].[UserLogin]
SET  [client_personalprofile] = dbo.fn_AccessRight([AccessRights], 1) ,
        [client_case_geninfo] = dbo.fn_AccessRight([AccessRights], 2) ,
        [client_case_atty] = dbo.fn_AccessRight([AccessRights], 3) ,
        [client_case_comments] = dbo.fn_AccessRight([AccessRights], 5) ,
        [client_case_docs] = dbo.fn_AccessRight([AccessRights], 6) ,
        [client_case_billing] = dbo.fn_AccessRight([AccessRights], 7) ,
        [client_case_steps] = dbo.fn_AccessRight([AccessRights], 8) ,
        [client_relcases] = dbo.fn_AccessRight([AccessRights], 9) ,
        [client_maincase] = dbo.fn_AccessRight([AccessRights], 10) ,
        [client_case_forms] = dbo.fn_AccessRight([AccessRights], 11);

Open in new window

Jim HornMicrosoft SQL Server Data DudeCommented:
^^^ okay, add this for NULL handling for each column

[column name] = CASE WHEN [AccessRights] IS NULL OR SUBSTRING([AccessRights], 9, 1) NOT IN (0, 1) THEN 0 ELSE SUBSTRING([AccessRights], 9, 1)  END,
-- repeat for other columns

Or perhaps a better idea is to have two UPDATE statements, one to catch NULL and assign values for every column, and the previous one for the non-nulls.  Whatever floats your boat.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
AleksAuthor Commented:
Yeah ... Ill use the code you had before and then simply run an update
Thanks Jim.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Query Syntax

From novice to tech pro — start learning today.