Create string of month year in order by date

Amit Singh

My existing query is:

SELECT @str = coalesce(@str + ']' + ', ', '') + '[' + monthyear
FROM (SELECT DISTINCT ( monthyear), ExpectedDate
      FROM table) s
order by ExpectedDate

I need to bring unique monthyear string in order by its date

ExpectedDate    monthyear
2015-11-01       Nov-15
2015-11-01       Nov-15
2015-11-01       Nov-15
2015-11-01       Nov-15
2015-11-01       Nov-15
2015-11-01       Nov-15
2015-11-01       Nov-15
2015-11-01       Nov-15
2015-11-01       Nov-15
2015-11-01       Nov-15
2015-12-01       Dec-15
2015-12-01       Dec-15
2015-12-01       Dec-15
2015-12-01       Dec-15
2015-12-01       Dec-15
2015-12-01       Dec-15
2015-12-01       Dec-15
2015-12-01       Dec-15
2015-12-02       Dec-15
2015-12-30       Dec-15
2016-01-01       Jan-16
2016-01-01       Jan-16
2016-01-01       Jan-16
2016-01-01       Jan-16
2016-01-01       Jan-16
2016-01-01       Jan-16
2016-01-01       Jan-16
2016-01-01       Jan-16
2016-01-01       Jan-16
2016-01-01       Jan-16
2016-01-01       Jan-16
2016-01-01       Jan-16
2016-01-01       Jan-16
2016-01-01       Jan-16
2016-01-04       Jan-16
2016-01-04       Jan-16
2016-01-04       Jan-16
2016-01-04       Jan-16
2016-02-01       Feb-16
2016-02-01       Feb-16
2016-02-01       Feb-16
2016-02-01       Feb-16
2016-02-01       Feb-16
2016-02-01       Feb-16
2016-02-01       Feb-16
2016-02-01       Feb-16
2016-02-01       Feb-16
2016-03-01       Mar-16
2016-03-01       Mar-16
2016-03-01       Mar-16
2016-03-01       Mar-16
2016-03-01       Mar-16
2016-03-01       Mar-16
2016-03-01       Mar-16
2016-04-01       Apr-16
2016-04-01       Apr-16
2016-04-01       Apr-16
2016-04-01       Apr-16
2016-04-01       Apr-16
2016-06-01       Jun-16
2016-06-01       Jun-16
2016-07-01       Jul-16
2016-10-01       Oct-16
2016-10-01       Oct-16
2016-12-01       Dec-16
2016-12-30       Dec-16
t-clausen.dk

Use XML PATH

DECLARE @t table(ExpectedDate date, monthdate char(8))
INSERT @t values
('2015-11-01', 'Nov-2015'),
('2015-11-01', 'Nov-2015'),
('2015-12-01', 'Dec-2015'),
('2015-12-01', 'Dec-2015'),
('2016-01-01', 'Jan-2016')

SELECT 
    STUFF(( 
        SELECT ',[' + STUFF(monthdate,4,1, '') + ']'
        FROM @t t1 
        GROUP BY ExpectedDate, monthdate
        ORDER BY ExpectedDate
        for xml path(''), type 
    ).value('.', 'varchar(max)'), 1, 1, '') [values] 

Result:

[Nov2015],[Dec2015],[Jan2016]

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

Get date, month and year value from Date string

Order by Month and Year in a merge query

Convert month year to a date in r

Convert date to format Day, date month year

How to change json string of date and format it to only show the day without, year, month, or time

convert date in php day/month name, year

select month and year from Date of birth IN sql

Matching value on date (year, month, day) but not time

R: date format with just year and month

Get rows with same date and month for each year

How do i get first date and Last date from month and year |Excel |Used Spinner for Month and Year|

Is a date in same week, month, year of another date in swift

convert year week string to date

How to sort my data according to month order? I want my month order to be april to april (that is the financial year)

Find the closest month-day to a date, no matter the year

Searching records between today's date and a specified month of this year

ggplot using grouped date variables (such as year_month)

Display employee anniversary dates within the next month or year of current date

GSP date picker: Add label for each field (day, month, year)

How to return month and year only except days from date?

How to Add a date at the beginning of month-year in a loop in Python/ colab?

Create sequence of date on every last day of month

How to get values of Year,Month and Day from String with help of Regex

SQL Server: Convert a month name (string) to a date

Pandas convert string to end of month date

django- concatenate(year、month)_range = [start_date、end_date]エラー

django- concatenate(year,month)_range=[start_date,end_date] error

Consecutive observations by Month and Year

How to create a date range on specific dates for each month in Python?

TOP 一覧

  1. 1

    モーダルダイアログを自動的に閉じる-サーバーコードが完了したら、Googleスプレッドシートのダイアログを閉じます

  2. 2

    セレンのモデルダイアログからテキストを抽出するにはどうすればよいですか?

  3. 3

    CSSのみを使用して三角形のアニメーションを作成する方法

  4. 4

    ドロップダウンリストで選択したアイテムのQComboBoxスタイル

  5. 5

    ZScalerと証明書の問題により、Dockerを使用できません

  6. 6

    PyCharmリモートインタープリターはプロジェクトタブにサイトパッケージのコンテンツを表示しません

  7. 7

    Windows 10でのUSB入力デバイスの挿入/取り外しの検出

  8. 8

    Excel - count multiple words per cell in a range of cells

  9. 9

    PictureBoxで画像のブレンドを無効にする

  10. 10

    Windows 10 Pro 1709を1803、1809、または1903に更新しますか?

  11. 11

    スタート画面にシャットダウンタイルを追加するにはどうすればよいですか?

  12. 12

    Python / SciPyのピーク検出アルゴリズム

  13. 13

    Luaの文字列から特定の特殊文字を削除するにはどうすればよいですか?

  14. 14

    Pythonを使用して、リストからデータを読み取り、特定の値をElasticsearchにインデックス付けするにはどうすればよいですか?

  15. 15

    LinuxでPySide2(Qt for Python)をインストールするQt Designerはどこにありますか?

  16. 16

    goormIDEは、ターミナルがロードするデフォルトプロジェクトを変更します

  17. 17

    QGISとPostGIS(マップポイント(米国の地図上にraduisを使用した緯度と経度)

  18. 18

    MLでのデータ前処理の背後にある直感

  19. 19

    ターミナルから「入力ソースの変更」ショートカットを設定する

  20. 20

    パンダは異なる名前の列に追加します

  21. 21

    同じクラスの異なるバージョンを使用したクラスローディング:java.lang.LinkageError:名前の重複クラス定義を試行しました

ホットタグ

アーカイブ