简体   繁体   English

从SQL中的列中选择子字符串

[英]Select substring from a column in SQL

How do I match a substring on another table? 如何匹配另一个表上的子字符串?

Table 1: 表格1:

Reference | Value
----------+-------
1         | 02.02.2011 07:07:00 498-123456-741
5         | 123-789654-100
5         | 123-789654-100

Table 2: 表2:

Reference | Code
----------+-------
5         | 123-789654-700
1         | 498-123456-100

I want to count the value from table one on table 2 我想计算表2中表一的值

select count(value) as count
from table 1 join table 2 on substring(value,0,12)=substring(code,0,12)
where reference='5' 

If it the value is present in Table 2 it gives me a count of 2. 如果该值存在于表2中,那么我的计数为2。

select count(value) as count
from table 1 join table 2 on substring(value,20,12)=substring(code,0,12)
where reference='1'

So the first query works fine the second query when a value comes in like 02.02.2011 07:07:00 498-123456-741 it doesn't compare it to table to even though that value is there, in Table 2 it will always be a substring of (0,12). 因此,第一个查询在第二个查询工作正常时会出现一个值,如02.02.2011 07:07:00 498-123456-741 ,即使该值存在,它也不会将它与表进行比较,在表2中它将始终是(0,12)的子字符串。

The syntax is like this : SUBSTRING ( expression ,start , length ) 语法如下: SUBSTRING ( expression ,start , length )

For example : 例如 :

SELECT x = SUBSTRING('abcdef', 2, 3);

Here is the result set: 这是结果集:

x
----------
bcd

You should do like this : 您应该这样做:

select count(*) as count from 
table 1 join table 2
on substring(value,20,12)=substring(code,0,12)

Do this 做这个

SELECT SUBSTRING('w3resource', startingIndex, lengthForRequiredString);

Example

SELECT SUBSTRING(column_name, 4, 3);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM