SQL関数

関数一覧

SELECT句で使う関数       

関数名 概要 使用例
convert
cnv
結果タブにデータを出力する際に、プレイビューリストとほぼ同じ値に形式変換する関数。 //☆形式で自分の評価を出力
select name, cnv(rating) from audio

WHERE句で使う関数

関数 概要 使用例
TodayFirst
Today
日付データ型の比較に使う。
TodayFirst()またはToday()は、本日の0時0分0秒を表す。
TodayFirst(-1)またはToday(-1)は、前日の0時0分0秒を表す。
// 本日再生したレコードを検索
where lastplayeddate >= Today()
// ライブラリ追加日が60日以内のレコードを検索
where adddate >= TodayFirst(-60)
TodayLast 日付データ型の比較に使う。
TodayLast()は、本日の23時59分59秒を表す。
TodayLast(-5)は、5日前の23時59分59秒を表す。
// 前日再生したレコードを検索
where lastplayeddate >= TodayFirst(-1)
   and lastplayeddate <= TodayLast(-1)
TodayNow
Now
日付データ型の比較に使う。
TodayNow()またはNow()は、本日の現在時刻を表す。
TodayNow(-7)またはNow(-7)は、7日前の現在時刻を表す。
// 1週間前の現在時刻以降に再生したレコードを検索
where lastplayeddate >= TodayNow(-7)
toHour
toH
length(duration)列の比較に使う。
toH(1)またはtoHour(1)は、1時間を表す。
// 再生時間が1時間以上のレコードを検索
where length >= 3600
where length >= toH(1)
toMinute
toM
length(duration)列の比較に使う。
toM(5)またはtoMinute(5)は、5分を表す。
// 再生時間が5分以上のレコードを検索
where length >= 300
where length >= toM(5)
toKB filesize列の比較に使う。
toKB(300)は、300KByteを表す。
// ファイルサイズが500KByte以上のレコードを検索
where filesize >= 5120
where filesize >= toKB(500)
toMB filesize列の比較に使う。
toMB(50)は、50MByteを表す。
// ファイルサイズが200MB以上のレコードを検索
where filesize >= 209715200
where filesize >= toMB(200)
toGB filesize列の比較に使う。
toGB(0.5)は、512MByteを表す。
// ファイルサイズが1GB以上のレコードを検索
where filesize >= 1073741824
where filesize >= toGB(1)
toStar ratingまたはratingauto列の比較に比較に使う。
toStar(2)は☆☆に該当
0~5の範囲で値を設定する。
【☆と数値の対応表 ※()は代表値。代表値以外の値が入る事はほとんどない】
☆0   0(0)
☆1   1-12(1)
☆2   13-37(25)
☆3   38-62(50)
☆4   63-86(75)
☆5   87-99(99)
// 自分の評価が☆☆☆のレコードを検索
where rating = 50
where rating = toStar(3)
CurrentValue
CV
現在再生中のメディアから該当する属性を取得し、条件として設定する。
※全ての列名で利用可能。
// 現在再生中のメディアのartistをもとにレコードを検索
where artist = CurrentValue()
// 現在再生中のメディアのlengthをもとにレコードを検索
where length > CV()
RandomValue
RV
指定列名のランダムな値を使い条件を設定する。
※列によって指定できないケースもあります。
// ランダムな値をalbum列に設定しレコードを検索
where album = RandomValue()
// ランダムな値をgenre列に設定しレコードを検索
where genre = RV()

option句で使う関数

関数名 概要 使用例
toH
toHour
option totallengthに使う。
toH(1)またはtoHour(1)は、1時間を表す。

// 再生時間が1時間以内のプレイリストを作成option option totallength 3600
option totallength toHour(1)

toM
toMinute
option totallengthに使う。
toM(30)またはtoMinute(30)は、30分を表す。
// 再生時間が30分以内のプレイリストを作成
option totallength 1800
option totallength toMinute(30)

 

convert関数

converまたはcnv関数は「結果タブ出力」機能を利用した場合のみに利用できます。
現在のプレイリストに出力した場合、表示データは見栄えが良い値に加工されています。

例えば再生時間を表すlength列は内部ではミリ秒で管理されています。
(例)曲Aの再生時間(71.234秒)の表記
select length from audio where name='曲A'
プレイリストの表示 00:01:11
結果タブに出力 71.234

convert関数を使うと、結果タブ出力の際に、プレイリスト表示に近い値を出力します
(例)曲Aの再生時間(71.234秒)の表記
select convert(length) from audio where name='曲A'
プレイリストの表示 00:01:11
結果タブに出力 00:01:11

利用できる列名は、「SQL列定義」から確認できます。

 

to~関数について

to~系の関数はwhere句を使う事ができます。
数値を直感的に使うための変換関数です。

<秒の変換>
length列に利用できます。
※lengthは直接指定の場合は、秒単位で指定します。
秒→分 :toMinute(分)またはtoM(分) ※両方ともに同じ機能です。
秒→時間:toHour(時間)またはtoH(時間) ※両方ともに同じ機能です。

(例)6分(6×60=360秒、0.1時間)以上の音楽ファイルを取得
select name, length from audio where length >= 360 order by length
select name, length from audio where length >= toM(6) order by length
select name, length from audio where length >= toH(0.1) order by length

(例)1時間(60×60=3600)以上の動画ファイルを取得
select name, length from video where length >= 3600 order by length
select name, length from video where length >= toH(1) order by length

<byteの変換>
filesize列に利用できます。
※filesizeは直接指定の場合は、byte単位で指定します。
byte→KB:toKB(バイトサイズ)
byte→MB:toMB(バイトサイズ)
byte→GB:toGB(バイトサイズ)

(例)50MB(50×1024×1024=52428800)以上の動画ファイルを取得
select name, filesize from video where filesize >= 52428800 order by filesize
select name, filesize from video where filesize >= toMB(50) order by filesize

<星の変換>
ratingまたはratingautoに使用できます
ratingは直接指定の場合は、0~99の数値で指定します。
※rating系の値の詳細は、「SQL列定義」を参照してください。

(例)評価☆3の音楽ファイルを取得します。
select name, rating from audio where rating = 50
select name, rating from audio where rating = toStar(3)

 

today系関数について

today系の関数はwhere句の列が日付型の際に使う事ができます。
TodayFirst()、TodayLast()、TodayNow()の3種類があります。

<TodayFirst(整数値[省略可])>
整数値がマイナス値 :本日からn日前の日付の0時0分0秒を意味します。
整数値がプラス値 :本日からn日後の日付の0時0分0秒を意味します。
整数値が0 または省略:本日の0時0分0秒を意味します;

<TodayLast(整数値[省略可]))>
整数値がマイナス値 :本日からn日前の日付の23時59分59秒を意味します。
整数値がプラス値 :本日からn日後の日付の23時59分59秒を意味します。
整数値が0 または省略:本日の23時59分59秒を意味します;

<TodayNow(整数値[省略可]))>
整数値がマイナス値 :本日からn日前の日付の現在時刻を意味します。
整数値がプラス値 :本日からn日後の日付の現在時刻を意味します。
整数値が0 または省略:本日の現在時刻を意味します;

(例)ライブラリ追加日が30日以内の音楽ファイルを取得
select name, adddate from audio where adddate >= TodayFirst(-30) order by adddate

(例)本日0時0分0秒以降に再生した音楽ファイルを取得
select name, lastplayeddate from audio where lastplayeddate >= TodayFirst() order by lastplayeddate

(例)前日に再生した音楽ファイルを取得
select name, lastplayeddate from audio where lastplayeddate >= TodayFirst(-1) and lastplayeddate <= TodayLast(-1) order by lastplayeddate

(例)1週間前の現在時刻以降に再生した音楽ファイルを取得
select name, lastplayeddate from audio where lastplayeddate >= TodayNow(-7) order by lastplayeddate

 

CurrentValue関数について

CurrentValue()またはCV()関数を使うと、現在再生中のメディアから該当する属性を取得し、条件に設定できます。
CurrentValue()は、基本的にどの列にも指定できます。
※CurrentValue()およびCV()は両方ともに同じ機能です。
※複数の値が登録できる属性については先頭1件目だけが使われます。

(例)現在再生中のメディアのアルバムに設定されている曲を指定
select name, album from audio where album = CurrentValue()
select name, album from audio where album = CV()


(例)現在再生中のメディアの再生時間よりも長い曲を指定
select name, length from audio where length >= CurrentValue()

 

RandomValue関数について

RandomValue()またはRV()関数を使うと、指定列名のランダムな値を使った条件設定ができます。
※RandomValue()およびRV()は両方ともに同じ機能です。
※列によって指定できないケースもあります。

(例)ランダムなアルバムを指定
select name, album from audio where album = RandomValue()
select name, album from audio where album = RV()


option randomselectfromとRandomValue()関数の違い。

  • option randomselectfromは、whereなどで条件を絞り込んだ結果に対して更に絞り込みを行います。一方、RandomValue()は、whereで絞り込む前にランダムな値を作り出します。
  • expandを指定している時に、option randomselectfromは利用可能ですが、RandomValue()は利用不可です。
  • 現在の仕様ではoption句は1つだけしか設定できません。この為、option randomselectfromを使うと別のoptionが指定ができなくなります。一方、RandomValue()はoptionと組み合わせて利用できます。

 

最終更新:2015年05月21日 22:59