How to create a backup table from a table with current date & time in sql server.
The spaces in date & time should be replaced by underscore.
For Backup I am doing this :-
select * into BKP_TABLE_STUDENT
from TABLE_STUDENT
And for fetching DateTime I am using this :-
select convert(varchar, getdate(), 0)
Above gives this format -> Mar 2 2022 4:02PM
So, I need to combine the above datetime and the table name.
E.g. Table name will be BKP_TABLE_STUDENT_Mar_2_2022_4_02PM
2条答案
按热度按时间mzaanser1#
You can conactenate your table name with a formatted string.
I would prefer a 24 hour clock than AM / PM but it's your call
returns
w1e3prcc2#
Try this....
SELECT CONCAT('BKP_TABLE_STUDENT_',FORMAT(getdate(),'MMM_dd_yyyy_hh_mm_tt')) as backup_name
SELECT * into backup_name from TABLE_STUDENT