Skip to content
Advertisement

How to extract date from a long string in SQL Server

I a long string like below in my column and I need to extract only the date (2021-07-05) from it.

Could anyone please help?

Advertisement

Answer

We can use PATINDEX with SUBSTRING here:

Demo

The call to PATINDEX above finds the starting position of the date, while SUBSTRING takes 10 characters from that position.

Advertisement