﻿CREATE TABLE #rbDatum (ID int PRIMARY KEY, Datum datetime, TanevId int)
INSERT INTO #rbDatum
SELECT 
   rb.ID
  ,CAST(JSON_VALUE(rb.C_ALAPERTEK, '$.Date') AS datetime)
  ,rb.C_TANEVID
FROM T_RENDSZERBEALLITAS_OSSZES rb 
WHERE rb.C_ERTEKTIPUS = 1696
  AND rb.TOROLT = 'F'

UPDATE rb 
  SET rb.C_ALAPERTEK = JSON_MODIFY(rb.C_ALAPERTEK, '$.Date', CONVERT(nvarchar, nn.C_NAPDATUMA, 126))
    ,rb.SERIAL = rb.SERIAL + 1
    ,rb.MODIFIER = 0
    ,rb.LASTCHANGED = GETDATE()
FROM T_RENDSZERBEALLITAS_OSSZES rb
INNER JOIN #rbDatum rbAlap ON rbAlap.ID = rb.ID AND rbAlap.TanevId = rb.C_TANEVID
INNER JOIN T_TANEV_OSSZES t ON t.ID = rb.C_TANEVID AND t.TOROLT = 'F'
INNER JOIN T_NAPTARINAP_OSSZES nn ON nn.C_TANEVID = t.ID  AND nn.TOROLT = 'F'
  AND DATEPART(month, nn.C_NAPDATUMA) = DATEPART(month, Datum)
  AND DATEPART(day, nn.C_NAPDATUMA) = DATEPART(day, Datum)
WHERE rb.TOROLT = 'F'
  AND Datum <> nn.C_NAPDATUMA

DROP TABLE #rbDatum
GO
