查询子串形成 [英] query for substring formation

查看:105
本文介绍了查询子串形成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用SQL取得字符串 abcd_01 01 应该是什么查询这里,其中 _ 之前的长度不同?也就是说,它可以是 abcde_01 ab_01 。基本上,我想要部分后 _ 。谢谢。

解决方案

这是SQL和各种扩展之间有类似功能的例子之一,您不能保证所有数据库之间的可移植性。



SUBSTRING关键字,使用PostgreSQL语法(没有提到模式匹配)是ANSI-99 。为什么这需要他们这么长,我dunno ...



您的需要的关键是获取现有列值的子字符串,所以你需要知道什么数据库子字符串函数。



Oracle






  SELECT SUBSTR('abcd_01',-2)FROM DUAL 

Oracle没有RIGHT函数,实际上只是一个用于子字符串函数的包装器。但是 Oracle的SUBSTR 允许您指定负数,以便反向处理字符串(结束开头)。



SQL Server






两个选项 - SUBSTRING RIGHT

  SELECT SUBSTRING('abcd_01',LEN('abcd_01') -  1,2)
SELECT RIGHT('abcd_01',2)

为了简洁,RIGHT是理想的。但对于可移植性,SUBSTRING是一个更好的选择...



MySQL





$ b b

与SQL Server一样,有三个选项 - SUBSTR SUBSTRING

  SELECT SUBSTR('abcd_01',LENGTH('abcd_01') -  1,2)
SELECT SUBSTRING('abcd_01',LENGTH('abcd_01') - 1,2)
SELECT RIGHT('abcd_01',2)



PostgreSQL < h2>




PostgreSQL只有 SUBSTRING

  SELECT SUBSTRING('abcd_01'FROM LENGTH abcd_01') -  1 for 2)

...但它支持有限的模式匹配,可在其他位置查看。



SQLite






<只支持 SUBSTR

  SELECT SUBSTR('abcd_01',LENGTH('abcd_01') -  1,2)


$ b b

结论






使用RIGHT(如果可用),而SUBSTR / SUBSTRING会更好,如果有需要端口对其他数据库的查询,所以它显式给别人发生了什么,应该更容易找到等效的功能。


I want to take the 01 part of a string abcd_01 using SQL. What should be the query for this, where the length before the _ varies? That is, it may be abcde_01 or ab_01. Basically, I want part after the _. Thanks.

解决方案

This is one of those examples of how there's similar functionality between SQL and the various extensions, but are just different enough that you can not guarantee portability between all databases.

The SUBSTRING keyword, using PostgreSQL syntax (no mention of pattern matching) is ANSI-99. Why this took them so long, I dunno...

The crux of your need is to obtain a substring of the existing column value, so you need to know what the database substring function(s) are called.

Oracle


SELECT SUBSTR('abcd_01', -2) FROM DUAL

Oracle doesn't have a RIGHT function, with is really just a wrapper for the substring function anyway. But Oracle's SUBSTR does allow you to specify a negative number in order to process the string in reverse (end towards the start).

SQL Server


Two options - SUBSTRING, and RIGHT:

SELECT SUBSTRING('abcd_01', LEN('abcd_01') - 1, 2)
SELECT RIGHT('abcd_01', 2)

For brevity, RIGHT is ideal. But for portability, SUBSTRING is a better choice...

MySQL


Like SQL Server, three options - SUBSTR, SUBSTRING, and RIGHT:

SELECT SUBSTR('abcd_01', LENGTH('abcd_01') - 1, 2)
SELECT SUBSTRING('abcd_01', LENGTH('abcd_01') - 1, 2)
SELECT RIGHT('abcd_01', 2)

PostgreSQL


PostgreSQL only has SUBSTRING:

 SELECT SUBSTRING('abcd_01' FROM LENGTH('abcd_01')-1 for 2)

...but it does support limited pattern matching, which you can see is not supported elsewhere.

SQLite


SQLite only supports SUBSTR:

SELECT SUBSTR('abcd_01', LENGTH('abcd_01') - 1, 2)

Conclusion


Use RIGHT if it's available, while SUBSTR/SUBSTRING would be better if there's a need to port the query to other databases so it's explicit to others what is happening and should be easier to find equivalent functionality.

这篇关于查询子串形成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆