i have a data that has a transaction Date [DATETIME] and i'm trying to have a query that go to each transaction row and get the month and year based on the transaction_Date and update the month column for big number of tranasction.
example
ID Trans_Date Month
01 01/01/2020 012020
02 02/01/2020 012020
03 01/05/2020 052020
any suggestions?
Do this using a computed column:
alter table t add month as (format(trans_date, 'MMyyyy'));
You can do something like this using an update
. But that is silly. The values will not be correct on new rows or on updates to existing rows. With a computed column, the values are always correct.
You can, of course, express this as an update
:
update t
set month = format(trans_date, 'MMyyyy');
If some of the values are already correct, then add a where
clause: where month <> format(trans_date, 'MMyyyy')
.
Hi just managed to do it this way:
UPDATE [DB].[dbo].[TABLE] SET [MONTH]= FORMAT(TRANS_DATETIME,'yyyy0MM');
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.