UPDATE with two conditions

Asked

Viewed 271 times

0

I’d like to make a UPDATE where I have two conditions; I would like to join the two in one, if not one is the other.

FIRST :

UPDATE RLT005
SET CtrDatBaixa = '2018-12-15 00:00:00'
WHERE
CtrDatBaixa BETWEEN '2018-12-15 00:00:00' AND '2018-12-15 23:59:59'

SECOND :

UPDATE RLT005
SET CtrDatBaixa = '2018-12-31 00:00:00'
WHERE
CtrDatBaixa BETWEEN '2018-12-31 00:00:00' AND '2018-12-31 23:59:59'
  • Describe what it is to join into one. And why you need to do this?

  • 1

    What you want is just truncate the timestamp field to just date, zeroing the hour, minute and second?

2 answers

2

From what I understand of " join the two in one", would be more or less that:

UPDATE RLT005
SET CtrDatBaixa = 
    CASE WHEN CtrDatBaixa BETWEEN '2018-12-15 00:00:00' AND '2018-12-15 23:59:59' THEN '2018-12-15 00:00:00' 
    CASE WHEN CtrDatBaixa BETWEEN '2018-12-31 00:00:00' AND '2018-12-31 23:59:59' THEN '2018-12-31 00:00:00'
    ELSE CtrDatBaixa END
  • Dough, ball show !!

  • @Marcuscastilho, good to help. But if the answer solved the problem, it is worth marking as accepted (see why).

  • 1

    ah yes, I’m new around here.

0


Explaining better to a friend of mine, he gave me the following suggestion :

UPDATE RLT005 
SET CtrDatBaixa = CONCAT(FORMAT(CtrDatBaixa, 'yyyy-MM-dd'), ' 00:00:00')
WHERE DATEPART(DAY, CtrDatBaixa) in (15, DATEPART(DAY, EOMONTH(CtrDatBaixa)))

Browser other questions tagged

You are not signed in. Login or sign up in order to post.