0

Good day, I'm trying to update my table. Because there was an error(s) in my website. first Please check my table. (Penilaian_Header)

IdPenilaian | KodePenilaian       |   Nip  | PositionCode | Total
    1613           -----             1603405    P028          0 
    1618           -----             1602999    P028          0 
    1641          PE0001568           603060    P040         35
    1640          PE0001567          1411862    P007         35

as you can see. There are two rows that KodePenilaian empty. So is there any chance to fill it ? so the result will be like this.

IdPenilaian | KodePenilaian       |   Nip  | PositionCode | Total
    1613          PE0001570         1603405     P028          0 
    1618          PE0001569         1602999     P028          0 
    1641          PE0001568           603060    P040         35
    1640          PE0001567          1411862    P007         35

This how i generate KodePenilaian

select case 
    when right(max(KodePenilaian),7) is null then 'PE0000001' 
    else ('PE' + RIGHT('0000000' + cast(right(max(KodePenilaian),7) + 1 as nvarchar),7)) 
    end KodePenilaian from Penilaian_Header

and here is there result when i run it

KodePenilaian
  PE0001569

Thanks, Sorry for my bad english.

2 Answers 2

1

Not really used to sql server 2008 Try something like this:

update Penilaian_Header pen 
   set pen.KodePenilaian = 
             (select case 
                     when right(max(newKode.KodePenilaian),7) is null then 'PE0000001' 
                     else ('PE' + RIGHT('0000000' + cast(right(max(newKode.KodePenilaian),7) + 1 as nvarchar),7)) 
                     end KodePenilaian
                from Penilaian_Header newKode)
 where pen.KodePenilain = NULL

if ----- is NULL in your table

Sign up to request clarification or add additional context in comments.

1 Comment

its working thanks. Just need to remove pen (alias)
0

You can try this way...

;WITH cte
AS (SELECT *,
  MAX(CONVERT(int, REPLACE(KodePenilaian, 'PE000', ''))) OVER () AS MaxNum,
  ROW_NUMBER() OVER (ORDER BY kodePenilaian) AS rn
FROM YourTable)
UPDATE cte SET KodePenilaian = concat('PE000', maxnum + rn)
    WHERE KodePenilaian IS NULL

Your table

create table YourTable (
IdPenilaian int, KodePenilaian varchar(20),  Nip  int, PositionCode varchar(10), Total INT)

insert into YourTable
(IdPenilaian , KodePenilaian       ,   Nip  , PositionCode , Total) values
   (  1613      ,     NULL         ,      1603405   ,'P028',          0  )
  ,(  1618      ,     NULL         ,      1602999   ,'P028',          0  )
  ,(  1641      ,    'PE0001568'   ,       603060   ,'P040',         35  )
  ,(  1640      ,    'PE0001567'   ,      1411862   ,'P007',         35  )

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.