简体   繁体   English

从字符串SQL中提取数字

[英]Extracting Number From String SQL

I have a normal SQL statement: 我有一个普通的SQL语句:

SELECT VALUE_ID, UF_CRM_TASK FROM b_uts_tasks_task

Now this returns aa different field everytime but they take the form of the following: 现在,这每次都会返回一个不同的字段,但是它们采用以下形式:

a:1:{i:0;s:7:"CO_2012";} or a:1:{i:0;s:5:"CO_12";} or a:1:{i:0;s:7:"CO_2017";}

Basically they're different everytime. 基本上,每次都不同。 What I need is to just get the number after the CO_ part. 我需要的只是在CO_部分后得到数字。 I have tried TRIM but because everything changes in the leading and trailing section I don't think this would work. 我尝试过TRIM,但是因为前导和尾随部分中的所有内容都发生了变化,所以我认为这不起作用。

I have looked on Stack Overflow for a while and cannot find it. 我已经查看了一段时间的Stack Overflow,但找不到它。 I know how to do it in PHP: 我知道如何在PHP中执行此操作:

$data = $row['UF_CRM_TASK'];    
$companyID = substr($data, strpos($data, "CO_") + 1);
$newCompanyID = preg_replace('/[^0-9.]+/', '', $companyID);

But not SQL. 但不是SQL。 Thanks in advance 提前致谢

In MYSQL is a bit ugly: 在MYSQL中有点难看:

/*SUBSTRING_INDEX BASED ON CO_ AND THE LAST " - in 2 SUBSTRINGS*/
SELECT `VALUE_ID`, SUBSTRING_INDEX(SUBSTRING_INDEX(`UF_CRM_TASK`, 'CO_', -1), '"', 1) AS `COMPANY_ID` FROM `b_uts_tasks_task`

In PHP you can just unserialize() : 在PHP中,您可以只unserialize()

$data = unserialize($row['UF_CRM_TASK']);
$companyID = str_replace('CO_', '', $data[0]);

eg: 例如:

$data = unserialize('a:1:{i:0;s:5:"CO_12";}');
echo str_replace('CO_', '', $data[0]);
//==> 12

You need to use CharIndex and SubString (Microsoft SQL) or 您需要使用CharIndex和SubString(Microsoft SQL)或

This is the sample code I made for my Microsoft SQL server: 这是我为Microsoft SQL Server创建的示例代码:

declare @companyIdString varchar(50) = 'a:1:{i:0;s:7:"CO_2012";}'

print 'Company ID in a string: ' + @companyIdString
print 'Find first position: ' + Cast(charindex('"CO_', @companyIdString) as varchar(2))
print 'Locate the second position (the last "): ' + Cast(charindex('"', @companyIdString, charindex('"CO_', @companyIdString)+4) as varchar(2))
print 'Extracted Company Id: ' + substring(@companyIdString,charindex('"CO_', @companyIdString)+4, charindex('"', @companyIdString, charindex('"CO_', @companyIdString)+4) - charindex('"CO_', @companyIdString) - 4)

select 
@companyIdString as CompanyIdString,
substring(@companyIdString,charindex('"CO_', @companyIdString)+4, charindex('"', @companyIdString, charindex('"CO_', @companyIdString)+4) - charindex('"CO_', @companyIdString) - 4) as CompanyId

I also made the same code on a mySQL server: 我还在mySQL服务器上做了同样的代码:

set @companyIdString := 'a:1:{i:0;s:7:"CO_2012";}';

select 
@companyIdString as CompanyIdString, 
substring_index(substring_index(substring_index(@companyIdString, '"', 2), '"', -1), '_', -1) as CompanyId

The substring_index starts by locating the second " (string is now a:1:{i:0;s:7:"CO_2012), then it searches backward with the -1 to locate the first " (string is now CO_2012). And then it searches backward for the underscore (string is now 2012). substring_index首先找到第二个“(字符串现在是a:1:{i:0; s:7:” CO_2012),然后以-1向后搜索以找到第一个“(字符串现在为CO_2012)。然后向后搜索下划线(字符串现在为2012)。

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

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