使用SUBSTRING和LEN的SQL问题

时间:2015-05-05 16:57:51

标签: sql sql-server tsql substring special-characters

SELECT [Code], [Due Date Calculation],

--original string that is causing the error
--SUBSTRING([Due Date Calculation], 1, LEN([Due Date Calculation]) - 1) as OG,

--here are the tests that run fine by themselves
LEN([Due Date Calculation]) - 1 as Test1,
SUBSTRING([Due Date Calculation], 1, 2) AS Test2, 
SUBSTRING([Due Date Calculation], 1, LEN([Due Date Calculation])) AS Test3 

FROM [TEST]

以下是我遇到的错误:

  

Msg 537,Level 16,State 2,Line 1   传递给LEFT或SUBSTRING函数的长度参数无效。

我知道它与SQL呈现数据的方式有关。数据显示一个小的颠倒' L'当我使用SQL查询它时,它只是显示了一个' D'在前端。我没有足够的声誉来包含图像。典型代码的示例包括30D60D120D365D等。

我需要删除尾随D并显示剩下的内容。

感谢您的帮助。

以下是SQL Query的结果: [https://drive.google.com/file/d/0B1cL-bzbZ4IzU2ctYlRNZnJhZjg/view?usp=sharing][1]

3 个答案:

答案 0 :(得分:1)

如果我理解你的问题,你正在寻找这个

    public static void main(String args[]) {
    /* Set the Nimbus look and feel */
    //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
    /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
     * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
     */
    try {
        for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
            if ("Nimbus".equals(info.getName())) {
                javax.swing.UIManager.setLookAndFeel(info.getClassName());
                break;
            }
        }
    } catch (ClassNotFoundException ex) {
        java.util.logging.Logger.getLogger(Login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
        java.util.logging.Logger.getLogger(Login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
        java.util.logging.Logger.getLogger(Login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
        java.util.logging.Logger.getLogger(Login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }
    //</editor-fold>

    /* Create and display the form */
    java.awt.EventQueue.invokeLater(() -> {
        Login dialog = new Login(new javax.swing.JFrame(), true); 
        dialog.addWindowListener(new java.awt.event.WindowAdapter() {

            @Override
           public void windowClosing(java.awt.event.WindowEvent e) {
               System.exit(0);
           } 
        });
        dialog.setVisible(true);

    });
}

SQLFIDDLE

答案 1 :(得分:0)

这是我的样本

declare @tbl table
(
    c1 nvarchar(9)
)
insert into @tbl values ('30D')
insert into @tbl values ('120D')
insert into @tbl values ('365D')

select  IIF(RIGHT(c1, 1) = 'D', LEFT(C1, LEN(C1)-1), C1)
from    @tbl

<强>结果
30个
120个
365

答案 2 :(得分:0)

如果你想切断最后一个角色,就这样做。

DECLARE @String VARCHAR(100) = '123D'

SELECT SUBSTRING(@String,0,LEN(@String))

或者如果你最后有多个字符,那么尝试这个,这将抓住直到数字停止。

DECLARE @String VARCHAR(100) = '123D'

SELECT SUBSTRING(@String,0,PATINDEX('%[^0-9]%',@String))

两者都有相同的结果:

123