Skip to content
Advertisement

SQL Server Convert Varchar to Datetime and Find Max Value

I try to find max date from a DATE column, the column format is YYYY-MM and its of type varchar.

DATE
--------
2020-07     (I try to find this value)
2020-06 
2020-05
2020-04

I tried with the following SQL code:

SELECT 
    CONVERT(datetime, 'DATE') AS Date 
FROM
    DateTable

Advertisement

Answer

You can try the below way –

select max(cast(concat(columnName,'-01') as date))
from yourtable
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement