0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

忘れやすいSQL関連のメモ

Posted at

Oracle

全角又は半角に統一する

select
  , 項目名
  , UTL_I18N.TRANSLITERATE(to_single_byte(項目名), 'fwkatakana_hwkatakana') as 半角
  , UTL_I18N.TRANSLITERATE(to_multi_byte(項目名), 'hwkatakana_fwkatakana') as 全角 
from
  テーブル名

表示件数指定

select * from テーブル名 fetch first 100 rows only

当日日付を8桁の文字列で表示する

select TO_CHAR(SYSDATE, 'YYYYMMDD') as today from dual

SQLserver

当日日付を8桁の文字列で表示する

select SUBSTRING(CONVERT(VARCHAR, GETDATE(), 112),1,8) as today

テーブルを丸ごとコピー

select * into コピー先テーブル名 from 参照元テーブル名

データを別テーブルに挿入

insert into 挿入先テーブル名 select * from 参照元テーブル名

ピボット

CREATE TABLE 成績
(
     名前   varchar(50)
    ,科目   varchar(20)
    ,得点   int
);

INSERT INTO 成績 VALUES ('1号', '英語', 100 ) ;
INSERT INTO 成績 VALUES ('1号', '国語',  90 ) ;
INSERT INTO 成績 VALUES ('1号', '数学',  80 ) ;
INSERT INTO 成績 VALUES ('1号', '理科',  70 ) ;
INSERT INTO 成績 VALUES ('1号', '社会',  60 ) ;
INSERT INTO 成績 VALUES ('2号', '英語',  50 ) ;
INSERT INTO 成績 VALUES ('2号', '国語',  40 ) ;
INSERT INTO 成績 VALUES ('2号', '数学',  30 ) ;
INSERT INTO 成績 VALUES ('2号', '理科',  20 ) ;
INSERT INTO 成績 VALUES ('2号', '社会',  10 ) ;
INSERT INTO 成績 VALUES ('3号', '英語',  20 ) ;
INSERT INTO 成績 VALUES ('3号', '国語',  30 ) ;
INSERT INTO 成績 VALUES ('3号', '数学',  40 ) ;
INSERT INTO 成績 VALUES ('3号', '理科',  50 ) ;
INSERT INTO 成績 VALUES ('3号', '社会',  60 ) ;

SELECT
     名前
    ,英語
    ,国語
    ,数学
    ,理科
    ,社会
FROM
    成績
PIVOT ( SUM( 得点 ) FOR 科目 IN ( 英語, 国語, 数学, 理科,社会)) AS ピボットテーブル
ORDER BY 名前;
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?