First a meta-discussion: you have tagged your post with sql-server-general
, azure-sql-database
and azure-database-mysql
. Of these, only the last pertains to MySQL. The other two relates to Microsoft SQL Server, which is a different product, and what applies to SQL Server may not apply to MySQL.
I don't work with MySQL, only SQL Server, so I can only give you an outline.
In the best of worlds it may as simple as:
ALTER TABLE tbl ALTER COLUMN col date [NOT] NULL
This assumes three things:
- The engine actually supports this conversion. (True for SQL Server.)
- The engine's default interpretation of the string actually is dd/mm/yyyy. (True for SQL Server if you issue SET DATEFORMAT command first, or if your language setting is for instance, British, French or German - but not us_english.)
- All values in the column adheres to the format, and there are no junk values like
30/02/2022
,05/27/2022
orKilroy was here
. (Not at all unlikely.)
If the latter proves to be true, the best may be (I've left out things I don't know how to do in MySQL):
ALTER TABLE tbl ADD newcol date
UPDATE tbl
SET newcol = cast(col AS date)
WHERE <check for date being a legal date>
ALTER TABLE tbl DROP COLUMN col
-- Rename newcol to col