DECLARE @list_polis TABLE ( no_polis VARCHAR(12))

INSERT INTO @list_polis ( no_polis ) VALUES ('00.01709.3')
INSERT INTO @list_polis ( no_polis ) VALUES ('00.01710.2')
INSERT INTO @list_polis ( no_polis ) VALUES ('00.04070.4')
INSERT INTO @list_polis ( no_polis ) VALUES ('02.01209.0')
INSERT INTO @list_polis ( no_polis ) VALUES ('02.02877.5')
--INSERT INTO @list_polis ( no_polis ) VALUES ('03.03118.2')
--INSERT INTO @list_polis ( no_polis ) VALUES ('04.00855.1')
--INSERT INTO @list_polis ( no_polis ) VALUES ('04.01916.0')
--INSERT INTO @list_polis ( no_polis ) VALUES ('06.04733.2')
--INSERT INTO @list_polis ( no_polis ) VALUES ('06.04773.6')
--INSERT INTO @list_polis ( no_polis ) VALUES ('07.04246.2')
--INSERT INTO @list_polis ( no_polis ) VALUES ('07.04398.3')
--INSERT INTO @list_polis ( no_polis ) VALUES ('08.04651.3')
--INSERT INTO @list_polis ( no_polis ) VALUES ('08.05061.6')
--INSERT INTO @list_polis ( no_polis ) VALUES ('08.05855.3')
--INSERT INTO @list_polis ( no_polis ) VALUES ('08.06280.3')
--INSERT INTO @list_polis ( no_polis ) VALUES ('08.06479.6')
--INSERT INTO @list_polis ( no_polis ) VALUES ('08.06479.6')
--INSERT INTO @list_polis ( no_polis ) VALUES ('08.06479.6')
--INSERT INTO @list_polis ( no_polis ) VALUES ('08.06479.6')
--INSERT INTO @list_polis ( no_polis ) VALUES ('08.06479.6')
--INSERT INTO @list_polis ( no_polis ) VALUES ('08.06479.6')
--INSERT INTO @list_polis ( no_polis ) VALUES ('08.06479.6')
--INSERT INTO @list_polis ( no_polis ) VALUES ('08.06479.6')
--INSERT INTO @list_polis ( no_polis ) VALUES ('08.06479.6')
--INSERT INTO @list_polis ( no_polis ) VALUES ('08.06479.6')
--INSERT INTO @list_polis ( no_polis ) VALUES ('08.06479.6')
--INSERT INTO @list_polis ( no_polis ) VALUES ('09.00023.0')
--INSERT INTO @list_polis ( no_polis ) VALUES ('09.00869.4')
--INSERT INTO @list_polis ( no_polis ) VALUES ('09.05127.3')
--INSERT INTO @list_polis ( no_polis ) VALUES ('09.06320.6')
--INSERT INTO @list_polis ( no_polis ) VALUES ('09.06718.3')
--INSERT INTO @list_polis ( no_polis ) VALUES ('09.06792.5')
--INSERT INTO @list_polis ( no_polis ) VALUES ('09.06804.6')
--INSERT INTO @list_polis ( no_polis ) VALUES ('09.06823.0')
--INSERT INTO @list_polis ( no_polis ) VALUES ('09.06827.4')
--INSERT INTO @list_polis ( no_polis ) VALUES ('09.06829.6')
--INSERT INTO @list_polis ( no_polis ) VALUES ('09.07048.0')
--INSERT INTO @list_polis ( no_polis ) VALUES ('09.07128.6')
--INSERT INTO @list_polis ( no_polis ) VALUES ('09.07128.6')
--INSERT INTO @list_polis ( no_polis ) VALUES ('09.07159.3')
--INSERT INTO @list_polis ( no_polis ) VALUES ('09.07161.3')
--INSERT INTO @list_polis ( no_polis ) VALUES ('10.00179.4')
--INSERT INTO @list_polis ( no_polis ) VALUES ('10.00179.4')
--INSERT INTO @list_polis ( no_polis ) VALUES ('10.07697.2')
--INSERT INTO @list_polis ( no_polis ) VALUES ('10.07697.2')
--INSERT INTO @list_polis ( no_polis ) VALUES ('10.08766.0')
--INSERT INTO @list_polis ( no_polis ) VALUES ('10.09003.6')
--INSERT INTO @list_polis ( no_polis ) VALUES ('10.09784.1')
--INSERT INTO @list_polis ( no_polis ) VALUES ('10.09827.6')
--INSERT INTO @list_polis ( no_polis ) VALUES ('10.09840.1')
--INSERT INTO @list_polis ( no_polis ) VALUES ('10.09848.2')
--INSERT INTO @list_polis ( no_polis ) VALUES ('10.09851.3')
--INSERT INTO @list_polis ( no_polis ) VALUES ('10.09876.3')
--INSERT INTO @list_polis ( no_polis ) VALUES ('10.09878.5')
--INSERT INTO @list_polis ( no_polis ) VALUES ('10.09885.3')
--INSERT INTO @list_polis ( no_polis ) VALUES ('10.09887.5')
--INSERT INTO @list_polis ( no_polis ) VALUES ('10.09890.6')
--INSERT INTO @list_polis ( no_polis ) VALUES ('10.09892.1')
--INSERT INTO @list_polis ( no_polis ) VALUES ('10.09909.0')
--INSERT INTO @list_polis ( no_polis ) VALUES ('10.10423.4')
--INSERT INTO @list_polis ( no_polis ) VALUES ('10.10423.4')
--INSERT INTO @list_polis ( no_polis ) VALUES ('10.10423.4')
--INSERT INTO @list_polis ( no_polis ) VALUES ('10.10423.4')
--INSERT INTO @list_polis ( no_polis ) VALUES ('10.10423.4')
--INSERT INTO @list_polis ( no_polis ) VALUES ('10.10423.4')
--INSERT INTO @list_polis ( no_polis ) VALUES ('10.10423.4')
--INSERT INTO @list_polis ( no_polis ) VALUES ('10.10423.4')
--INSERT INTO @list_polis ( no_polis ) VALUES ('10.10423.4')
--INSERT INTO @list_polis ( no_polis ) VALUES ('10.10423.4')
--INSERT INTO @list_polis ( no_polis ) VALUES ('11.00004.6')
--INSERT INTO @list_polis ( no_polis ) VALUES ('11.01576.0')
--INSERT INTO @list_polis ( no_polis ) VALUES ('11.03755.1')
--INSERT INTO @list_polis ( no_polis ) VALUES ('11.04901.2')
--INSERT INTO @list_polis ( no_polis ) VALUES ('11.05183.5')
--INSERT INTO @list_polis ( no_polis ) VALUES ('11.06271.4')
--INSERT INTO @list_polis ( no_polis ) VALUES ('11.06484.3')
--INSERT INTO @list_polis ( no_polis ) VALUES ('11.08598.4')
--INSERT INTO @list_polis ( no_polis ) VALUES ('11.08625.2')
--INSERT INTO @list_polis ( no_polis ) VALUES ('11.09139.3')
--INSERT INTO @list_polis ( no_polis ) VALUES ('11.10211.0')
--INSERT INTO @list_polis ( no_polis ) VALUES ('11.10228.1')
--INSERT INTO @list_polis ( no_polis ) VALUES ('11.10228.1')
--INSERT INTO @list_polis ( no_polis ) VALUES ('11.12088.0')
--INSERT INTO @list_polis ( no_polis ) VALUES ('11.12352.1')
--INSERT INTO @list_polis ( no_polis ) VALUES ('11.12356.5')
--INSERT INTO @list_polis ( no_polis ) VALUES ('11.12430.5')
--INSERT INTO @list_polis ( no_polis ) VALUES ('11.12507.3')
--INSERT INTO @list_polis ( no_polis ) VALUES ('11.12532.1')
--INSERT INTO @list_polis ( no_polis ) VALUES ('11.12545.5')
--INSERT INTO @list_polis ( no_polis ) VALUES ('11.12549.2')
--INSERT INTO @list_polis ( no_polis ) VALUES ('11.12631.1')
--INSERT INTO @list_polis ( no_polis ) VALUES ('11.12668.4')
--INSERT INTO @list_polis ( no_polis ) VALUES ('11.12749.4')
--INSERT INTO @list_polis ( no_polis ) VALUES ('11.12753.6')
--INSERT INTO @list_polis ( no_polis ) VALUES ('11.12754.0')
--INSERT INTO @list_polis ( no_polis ) VALUES ('11.12755.1')
--INSERT INTO @list_polis ( no_polis ) VALUES ('11.12771.6')
--INSERT INTO @list_polis ( no_polis ) VALUES ('11.12825.6')
--INSERT INTO @list_polis ( no_polis ) VALUES ('11.12842.5')
--INSERT INTO @list_polis ( no_polis ) VALUES ('11.14380.4')
--INSERT INTO @list_polis ( no_polis ) VALUES ('12.16715.2')
--INSERT INTO @list_polis ( no_polis ) VALUES ('14.04420.1')
--INSERT INTO @list_polis ( no_polis ) VALUES ('14.04743.2')
--INSERT INTO @list_polis ( no_polis ) VALUES ('14.05243.5')
--INSERT INTO @list_polis ( no_polis ) VALUES ('920146')
--INSERT INTO @list_polis ( no_polis ) VALUES ('946595')
--INSERT INTO @list_polis ( no_polis ) VALUES ('946595')
--INSERT INTO @list_polis ( no_polis ) VALUES ('947703')
--INSERT INTO @list_polis ( no_polis ) VALUES ('96.04978.1')
--INSERT INTO @list_polis ( no_polis ) VALUES ('97.03516.3')
--INSERT INTO @list_polis ( no_polis ) VALUES ('97.05157.6')
--INSERT INTO @list_polis ( no_polis ) VALUES ('97.07147.0')
--INSERT INTO @list_polis ( no_polis ) VALUES ('97.07147.0')
--INSERT INTO @list_polis ( no_polis ) VALUES ('97.07454.1')
--INSERT INTO @list_polis ( no_polis ) VALUES ('97.07894.2')
--INSERT INTO @list_polis ( no_polis ) VALUES ('98.00109.6')

DECLARE @no_polis VARCHAR(12)

DECLARE list_cursor CURSOR FOR  
SELECT no_polis
FROM @list_polis 

OPEN list_cursor  
FETCH NEXT FROM list_cursor INTO @no_polis  

WHILE @@FETCH_STATUS = 0  
BEGIN  
  DECLARE @tgl_mulai DATETIME
  SELECT DISTINCT @tgl_mulai=tgl_mulai FROM premi_jt WHERE no_polis=@no_polis

  SELECT 
    id_premi,
    no_polis,
    tgl_mulai,
    DATEPART(DAY,tgl_mulai) AS mulai_tgl,
    DATEPART(MONTH,tgl_mulai) AS mulai_bln,
    id_periode_bayar,
    jt_ke,
    jt_thn,
    jt_bln,
    tanggal,
    DATEPART(DAY,tanggal) AS jt_tgl,
    DATEPART(MONTH,tanggal) AS jt_bln,
    CASE
      WHEN DATEPART(DAY,tgl_mulai)<>DATEPART(DAY,tanggal) THEN 'salah'
      ELSE 'betul'
    END AS hasil_cek,
    CASE
      WHEN DATEPART(DAY,tgl_mulai)<>DATEPART(DAY,tanggal) THEN id_premi
      ELSE NULL
    END AS id_premi_perbaiki,
    --CAST(DATEPART(YEAR,tanggal) AS VARCHAR(4))+'-'+CAST(DATEPART(MONTH,tanggal) AS VARCHAR(2))+'-'+CAST(DATEPART(DAY,tgl_mulai) AS VARCHAR(2)) AS tgl_fix_string,
    CASE
      WHEN DATEPART(DAY,tgl_mulai)<>DATEPART(DAY,tanggal) THEN 
        CONVERT(DATETIME, CAST(DATEPART(YEAR,tanggal) AS VARCHAR(4))+'-'+CAST(DATEPART(MONTH,tanggal) AS VARCHAR(2))+'-'+CAST(DATEPART(DAY,tgl_mulai) AS VARCHAR(2)),120)
      ELSE NULL
    END  AS tgl_fix_datetime
  FROM dbo.premi_jt
  WHERE no_polis = @no_polis
  ORDER BY tanggal

  FETCH NEXT FROM list_cursor INTO @no_polis  
END  

CLOSE list_cursor  
DEALLOCATE list_cursor 


--SELECT 
--  id_premi,
--  no_polis,
--  tgl_mulai,
--  id_periode_bayar,
--  jt_ke,
--  jt_thn,
--  jt_bln,
--  tanggal
--FROM dbo.premi_jt AS pj
--WHERE no_polis = '00.01709.3'
--ORDER BY tanggal
