- 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
2017/02/09
awk サンプル ファイル分割など
- サンプル事例
以下のファイルの文字列コントロールのサンプル1列目(名前)2列目(出身地)3列目(誕生日)4列目(男女 John Osaka 12/05/2000 M Mark Tokyo 12/10/2001 M Emily Akita 01/24/1998 W Sam Osaka 10/01/1988 M Toshi Osaka 11/01/2000 M 1.1列目 だけ抜き出し 2.if 条件付き分岐 大阪出身だったら名前だけ、それ以外は出身地を名前を表示 3.ファイルの分割 $4(男女)の項目をファイル名とし、出力ファイルを分割 - SourceThis 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 characters
-- Input 用 SAMPLEファイル $ cat birth.dat John Osaka 12/05/2000 M Mark Tokyo 12/10/2001 M Emily Akita 01/24/1998 W Sam Osaka 10/01/1988 M Toshi Osaka 11/01/2000 M --$1 = 1列目(名前)$2 = 2列目(出身地)$3 = 3列目(誕生日) $4 = 4列目(男女) -- 名前だけを表示する $ awk '{print $1}' birth.dat John Mark Emily Sam Toshi -- if 条件付き分岐 大阪出身だったら名前だけ、それ以外は出身地を名前を表示 $ awk '{if($2=="Osaka")print $1; else print $1,$2;}' birth.dat John Mark Tokyo Emily Akita Sam Toshi -- ファイルの分割 $4(男女)の項目をファイル名とし、出力ファイルを分割 -- if をつかってファイル名を指定しても当然可能 $ awk '{print $1,$3,$2 > $4".dat"}' birth.dat $ cat W.dat Emily 01/24/1998 Akita $ cat M.dat John 12/05/2000 Osaka Mark 12/10/2001 Tokyo Sam 10/01/1988 Osaka Toshi 11/01/2000 Osaka
2017/02/08
XMLファイルからテーブルへのINSERT
- Goal
XMLファイルからテーブルの要素を抜き出して、INSERTしたい。
SQLServer バージョン
- How
xqueryを使って実施。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 @xml as XML --temp Manual set SET @xml = CAST('<Logs> <LogDateEntry date="01/01/2017"> <Details time="05:16:26" Type="GET" subject="TEST1" details="This is test" value="50" target="SERVER1" /> <Details time="05:16:35" Type="PUT" subject="TESTSE" details="aseasdfasdfa" value="125" target="SERVER3" /> <Details time="05:25:24" Type="PUT" subject="ddddd" details="asdfasdfasdfsafdsafdasdfasfdsadfasdf" value="1524" target="SERVER1" /> <Details time="05:45:23" Type="GET" subject="asdfasdfasdfasdfasfdsafd" details="asdfasfdsafdasfdasdfasdfasdfasdfasdfasdfsa" value="12" target="SERVER1" /> </LogDateEntry> </Logs>' as XML) --Insert --insert into TEST_LOG(DATE,TIME,alertType,subject,details,value,target,creation_date) --select select CONVERT(VARCHAR(50),TLOG.c.query('data(..//@date)')) as DATE , CONVERT(VARCHAR(1000),TLOG.c.query('data(.//@time)')) as TIME , CONVERT(VARCHAR(1000),TLOG.c.query('data(.//@Type)')) as AT , CONVERT(VARCHAR(1000),TLOG.c.query('data(.//@subject)')) as SUB , CONVERT(VARCHAR(1000),TLOG.c.query('data(.//@details)')) as details , CONVERT(NUMERIC(18,3),CONVERT(VARCHAR(1000),TLOG.c.query('data(.//@value)'))) as value , CONVERT(VARCHAR(100),TLOG.c.query('data(.//@target)')) as target , getdate() from @xml.nodes('/Logs/LogDateEntry/Details') as TLOG(c) GO
登録:
投稿 (Atom)