In case of ancient servers, check the next statement:
_alter table tktproc add acct_period as (cast(YEAR(proc_date) * 100 + MONTH(proc_date) as varchar(max)))_
Show the error messages.
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
I have a sql table called tktproc with the following column proc_date with data type of datetime.
How can i autofill column acct_period from proc_date in the following format YYYYMM?
In case of ancient servers, check the next statement:
_alter table tktproc add acct_period as (cast(YEAR(proc_date) * 100 + MONTH(proc_date) as varchar(max)))_
Show the error messages.
Try executing this statement:
alter table tktproc add acct_period as (FORMAT(proc_date, 'yyyyMM'))
I'm having trouble getting this to run on SQL 2008R2
That worked, thanks. Going forward how do I change the query to update new records.
Hi @William Batte ,
Welcome to the microsoft TSQL Q&A forum!
The FORMAT function has been provided since the 2012 version, so it does not work in sql server 2008 R2. In earlier versions, use this:
ALTER TABLE tktproc ADD acct_period as (REPLACE(LEFT(proc_date,CHARINDEX('-',proc_date)+2),'-',''))
If you have any question, please feel free to let me know.
If the response is helpful, please click "Accept Answer" and upvote it.
Regards
Echo
If the answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.