- Goal
日付と時間の別々の文字列を、Datetime型に変換する。
”曜日, 月 日, 年” と ”時間:分:秒 AM/PM” をDatetime型に変換
EX)
As is
Wednesday, February 15, 2017 と 1:25:22 PM
To be
2017-02-15 13:25:22.000
文字列分解して無理矢理変換している。。。
良い方法があったら教えて欲しい。
- Source
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersDECLARE @tmp TABLE (datechar VARCHAR(200), timechar VARCHAR(200)) --sample dataset --Insert INSERT INTO @tmp (datechar,timechar) VALUES ('Wednesday, February 15, 2017','1:25:22 PM') , ('Monday, May 15, 2017','12:35:16 AM') --select select datechar , timechar --CONVERT from Char to Datetime.... , CONVERT(DATETIME, -- 無理矢理書式を整える -- Year 4 digit Right(datechar,4) -- Month 2 digit + (CASE substring(datechar, CHARINDEX(' ',datechar)+1,--一つ目の空白の後ろから CHARINDEX(' ',datechar,CHARINDEX(' ',datechar)+1)-CHARINDEX(' ',datechar))--次の空白の前 WHEN 'January' THEN '01' WHEN 'February' THEN '02' WHEN 'March' THEN '03' WHEN 'April' THEN '04' WHEN 'May' THEN '05' WHEN 'June' THEN '06' WHEN 'July' THEN '07' WHEN 'August' THEN '08' WHEN 'September' THEN '09' WHEN 'October' THEN '10' WHEN 'November' THEN '11' WHEN 'December' THEn '12' ELSE 'error' END) -- Day 2 digit + substring(datechar,CHARINDEX(' ',datechar,CHARINDEX(' ',datechar)+1)+1,2) + ' ' -- time + timechar) as datetime_type from @tmp GO
Oracle Application Express Notes | Apps development Notes | Google Cloud Platform | Python | apps test | Cool Beans | English | Books
2017/02/17
文字列から日付へ変換 - SQL Server
登録:
コメントの投稿 (Atom)
0 件のコメント:
コメントを投稿