SQL String Conversion MMM.YY to date

how do I convert / distinguish a column that contains rows, for example. Jan.08, Feb.08 .. in date format so that I can sort them?

Greatest thanks!

+3
source share
2 answers

I would just format it as a convertible string for the first corresponding month, and then discard it in datetime, for example.

CAST('1.' + YourMonthAndYearColumnName AS DATETIME)

... is an expression that will give datetime time, which should be sortable, therefore:

SELECT
  YourMonthAndYearColumnName
FROM
  YourTable
ORDER BY
  CAST('1.' + YourMonthAndYearColumnName AS DATETIME)

... should do what you are looking for.

+6
source

If you can assume that all dates will be in the last ten years, you can use the following code:

select convert(datetime, replace('Jan.08', '.', ' 20'))
select convert(datetime, replace('Dec.08', '.', ' 20'))

" 2008", . "Dec.08" "8 " " 2008 ".

"1." . , SQL- (.. 50 - 1950 49 - 2049).

select convert(datetime, '1.' + 'Jan.08')
select convert(datetime, '1.' + 'Dec.49')
select convert(datetime, '1.' + 'Jan.50')
0

Source: https://habr.com/ru/post/1752227/


All Articles