Adsense

2019年8月27日火曜日

【ACCESS/EXCEL/VBA】末尾全角文字化け回避用 拡張LeftBサンプル



LeftBでは末尾の全角文字列1バイトが切られてしまう場合、末尾の全角文字列が文字化けする。
全角文字列の文字化けを考慮する場合は別途チェックが必要になる。


■文字化け回避用のLeftB拡張モジュールサンプル(末尾切り上げ/切り捨て、全半角混在も対応)
' 引数説明
'      prm:対象文字列
'   length:取得バイト数
'  convert:True時Nullを""に変換/False時Nullをそのまま返す
' truncate:True時末尾切り捨て  /False時末尾切り上げ
Private Function ExLeftB(ByVal prm As Variant, _
                         ByVal length As Integer, _
                         Optional ByVal convert As Boolean = True, _
                         Optional ByVal truncate As Boolean = True) As Variant
    Dim cnvStr As String
    ' Accessの場合
    cnvStr = StrConv(Nz(prm, ""), vbFromUnicode)
    ' Excelの場合
    ' cnvStr = StrConv(IIf(IsNull(prm), "", prm), vbFromUnicode)
    
    If LenB(cnvStr) <= length Then
        ExLeftB = IIf(convert, Nz(prm), prm)
        Exit Function
    End If
    
    Dim leftNormal As Variant
    Dim leftTrunc As Variant
    Dim leftRoundUp As Variant
    leftNormal = StrConv(LeftB(cnvStr, length), vbUnicode)
    leftTrunc = StrConv(LeftB(cnvStr, length - 1), vbUnicode)
    leftRoundUp = StrConv(LeftB(cnvStr, length + 1), vbUnicode)
    
    If Len(leftNormal) <> Len(leftRoundUp) Then
        ExLeftB = leftNormal
        Exit Function
    Else
        ExLeftB = IIf(truncate, leftTrunc, leftRoundUp)
        Exit Function
    End If
End Function



■使用例
Private Sub dammy()
    Debug.Print ("---------------------")
    Debug.Print ("Nullコンバートする")
    Debug.Print (ExLeftB(Null, 3))
    Debug.Print ("---------------------")
    Debug.Print ("Nullコンバートしない")
    Debug.Print (ExLeftB(Null, 3, False))
    Debug.Print ("---------------------")
    Debug.Print ("切り捨て出力")
    Debug.Print (ExLeftB("あああ", 5))
    Debug.Print ("---------------------")
    Debug.Print ("切り上げ出力")
    Debug.Print (ExLeftB("あああ", 5, truncate:=False))
    Debug.Print ("---------------------")
End Sub

■実行結果

2019年8月3日土曜日

【SQL Server】NULLを変換する(ISNULL,COALESCEの違い)


NULL変換を行う場合にはISNULL、COALESCEの2種類の変換方法がある。

①ISNULLを使用する場合
■書き方等
 ISNULL(第一引数, 第二引数)
 ISNULLは第一引数がNULLの場合に第二引数の値を返す。  戻り値型は第一引数と同じ。
■使用例
テーブル名:[TEST_TBL1]
column1column2column3column4
1列をNULLNULL
2NULL成せ汝NULL
3NULLNULL従順の下僕
4NULLNULLNULL

実行するSQL
  SELECT ISNULL(column1,'Big Brother') AS C1
       , ISNULL(column2,'Big Brother') AS C2
       , ISNULL(column3,'Big Brother') AS C3
       , ISNULL(column4,'Big Brother') AS C4
  FROM TEST_TBL1

実行結果
C1C2C3C4
1列をBig BrotherBig Brother
2Big Brother成せ汝Big Brother
3Big BrotherBig Brother従順の下僕
4Big BrotherBig BrotherBig Brother


②COALESCEを使用する場合
■書き方等  COALESCE(第一引数, 第二引数, 第三引数, …)
 COALESCEは第一引数から順番に評価を行い、NULLでない最初の引数を返す。
 戻り値型は引数で使用されている型の中で型の優先度が高いものが使用される。
■使用例
テーブル名:[TEST_TBL1]
column1column2column3column4
1列をNULLNULL
2NULL成せ汝NULL
3NULLNULL従順の下僕
4NULLNULLNULL


・例.1:第二引数まで指定する場合(ISNULL
 実行するSQL
  SELECT COALESCE(column1, 'Big Brother') AS C1
       , COALESCE(column2, 'Big Brother') AS C2
       , COALESCE(column3, 'Big Brother') AS C3
       , COALESCE(column4, 'Big Brother') AS C4
  FROM TEST_TBL1

 実行結果
C1C2C3C4
1列をBig BrotherBig Brother
2Big Brother成せ汝Big Brother
3Big BrotherBig Brother従順の下僕
4Big BrotherBig BrotherBig Brother

・例.2:第三引数以降も指定した場合
 実行するSQL
  SELECT COALESCE(column2 , column3, column4, 'BigBrother') AS C1
  FROM TEST_TBL1

 実行結果
C1
列を
成せ汝
従順の下僕
Big Brother

■ISNULLとCOALESCEの違い
引数に指定できる個数以外は同等にみえるが、
それぞれ変換後の型の扱いに違いがあることに注意が必要。

ISNULLは「第一引数と同じ」
COALESEは「引数で使用されている型の中で型の優先度が高いものが使用される」

例えば以下のようなSQLを実行した場合はCOALESEではエラーが発生することがある。
・ISNULLを使用した場合
 SELECT ISNULL('AAA' , 2) AS C1
C1
AAA

・COALESCEを使用した場合
 SELECT COALESCE('AAA' , 2) AS C1
メッセージ 245、レベル 16、状態 1、行 1
varchar の値 'AAA' をデータ型 int に変換できませんでした。

COALESCEを使用した場合はvarchar型の’AAA’を
型の優先度が高いint型に変換しようとしエラーが発生する。