如何在sql查询中获取Substring

时间:2016-12-03 05:57:54

标签: sql substring dbeaver

我试图在表名中的int元素中从0-7位置获取子串,所以当我插入此

select substring(varchar(8),column_name,0, 6) as new_name from table_name

我正在

  

SQL错误[42883]:错误:函数pg_catalog.substring(bigint,   整数,整数)不存在

     

提示:没有函数匹配给定的名称和参数类型。您   可能需要添加显式类型转换。

     

职位:8

     

org.postgresql.util.PSQLException:错误:功能   pg_catalog.substring(bigint,integer,integer)不存在

     

提示:没有函数匹配给定的名称和参数类型。您   可能需要添加显式类型转换。职位:8

所以在向varchar(8)添加一些强制转换后

public void startWatching() {
    if (mObservers != null) return;

    mObservers = new ArrayList<SingleFileObserver>();
    Stack<String> stack = new Stack<String>();
    stack.push(mPath);

    while (!stack.isEmpty()) {
        String parent = stack.pop();
        mObservers.add(new SingleFileObserver(parent, mMask));
        File path = new File(parent);
        File[] files = path.listFiles();
        if (null == files) continue;
        for (File f : files)
        {
            if (f.isDirectory() && !f.getName().equals(".") && !f.getName().equals("..")) {
                stack.push(f.getPath());
            }
        }
    }

    for (SingleFileObserver sfo : mObservers) {
        sfo.startWatching();
    }
}

@Override
public void stopWatching() {
    if (mObservers == null) return;

    for (SingleFileObserver sfo : mObservers) {
        sfo.stopWatching();
    }
    mObservers.clear();
    mObservers = null;
}

@Override
public void onEvent(int event, String path) {
    switch (event)
    {
        case FileObserver.ACCESS:
            Log.d("RecursiveFileObserver", "ACCESS: " + path);
            break;
        case FileObserver.ATTRIB:
            Log.i("RecursiveFileObserver", "ATTRIB: " + path);
            break;
        case FileObserver.CLOSE_NOWRITE:
            Log.i("RecursiveFileObserver", "CLOSE_NOWRITE: " + path);
            break;
        case FileObserver.CLOSE_WRITE:
            Log.i("RecursiveFileObserver", "CLOSE_WRITE: " + path);
            break;
        case FileObserver.CREATE:
            Log.i("RecursiveFileObserver", "CREATE: " + path);
            break;
        case FileObserver.DELETE:
            Log.e("Save:", "Save file user delete");

            File dir = new File(Environment.getExternalStorageDirectory() + File.separator + ConstantUtils.FOLDER_RESTORE);
            if (!dir.exists()){
                dir.mkdir();

            }
            File inputFile = new File(path);
            String  str = String.valueOf(inputFile);
            if (!str.isEmpty()){
                String [] result =  str.split("/");
                int i = result.length;
                if (i > 4) {
                    String inputPath = result[i - 1];
                    File outputFile = new File(Environment.getExternalStorageDirectory() + File.separator + ConstantUtils.FOLDER_RESTORE + File.separator + inputPath);
                    String str1 = String.valueOf(inputFile);
                    String str2 = String.valueOf(outputFile);
                    if (str1.compareTo(str2) != 0){
                        ZipHelper.copyFileToFolder(inputFile, outputFile);
                    }
                }
            }
            break;
        case FileObserver.DELETE_SELF:
            Log.i("RecursiveFileObserver", "DELETE_SELF: " + path);
            break;
        case FileObserver.MODIFY:
            Log.i("RecursiveFileObserver", "MODIFY: " + path);
            break;
        case FileObserver.MOVE_SELF:
            Log.i("RecursiveFileObserver", "MOVE_SELF: " + path);
            break;
        case FileObserver.MOVED_FROM:
            Log.i("RecursiveFileObserver", "MOVED_FROM: " + path);
            break;
        case FileObserver.MOVED_TO:
            Log.i("RecursiveFileObserver", "MOVED_TO: " + path);
            break;
        case FileObserver.OPEN:
            Log.i("RecursiveFileObserver", "OPEN: " + path);
            break;
        default:
            Log.i("RecursiveFileObserver", "DEFAULT(" + event + "): " + path);
            break;
    }
}

得到这个

  

SQL错误[42601]:错误:语法错误在或附近&#34;,&#34;职位:28
  org.postgresql.util.PSQLException:错误:语法错误在或附近&#34;,&#34;   职位:28

我无法弄清楚我做错了什么。感谢。

2 个答案:

答案 0 :(得分:2)

直接投放到文字:

select substring(column_name::text, 1, 7) as new_name
from table_name 

请注意,SQL(与C类语言不同)使用基于 1的索引,因此第一个字符是位置1。与C类语言不同,substring的第二个参数是 length (不是独占结束索引)。

答案 1 :(得分:0)

您可cast intvarchar,然后substring17

select substring(cast([your-column] as varchar(100)),1, 7)

示例:

  declare @a int ='1234567890';
  select substring(cast(@a as varchar(100)),1, 7) as res

输出:

  res 
  1234567