简体   繁体   中英

SQL server: alter column from nvarchar to int

This table has filedate(nvarchar) and Filename. The format needs to be in MMDDYYYY for the filedate.

在此处输入图像描述 I used

 replace(cast(filedate as varchar),'-','') 

and changed the datatype to int, which worked, but not in the format that I need. 在此处输入图像描述

Do I need to update the Fname and then try this or is there another way to do this?

Thank you in advance.

Any column that is expected to have leading zeros is not a number. So, you want a string. My recommendation is to convert the value to a date and then format it in the way you want:

select format(convert(date, '4-17-2020', 110), 'MMddyyyy')

Or, better yet, simply convert the value to date. It is a date. So you should store and retrieve it as a date.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM