1

I have a strange request, I thikn. I have a system which I need to retrieve data from - its SQL based. The field in question has a multitude of data in it, but I specifically only need 1 or 2 values.

In the example below, the field contains values contained in "*". I need to retrieve the value for AQISPremIdProcessType_Hidden which is "N2725/D". Is this possible through a SQL script?

AQISEntityIds_Hidden=CN0001MB*AQISPremIdProcessType_Hidden=N2725/D*CPQuestionGenDate_Hidden=20/01/2017
Seb
  • 11
  • 3
  • You should indicate which database platform you are using to enable people to give a definite answer to this question. – Nathan Griffiths Jun 08 '17 at 23:36
  • String processing is not a strength of SQL the language. Yes, you can probably do it, but it depends which flavour of SQL you're using (SQL Server, MySQL, Oracle, etc). Can you add the appropriate tag to your question? – Blorgbeard Jun 08 '17 at 23:36
  • If you are not doing this in the `where` clause, then it's probably easier and cleaner to just extract the data in your client code, rather than in SQL. – Blorgbeard Jun 08 '17 at 23:37
  • hi Guys. Unfortunately, this system is an ERP system used by businsses all over the world so I cant control how the data is stored. My job is to write a report for a client to extract this data for their cleint. – Seb Jun 08 '17 at 23:59
  • Ps.@Nathan. This a SQL server 2016 system – Seb Jun 09 '17 at 00:00

2 Answers2

0

I think that there may be a problem in your database design. Of course, I may be wrong but would there be a way to design a table with the following columns?

AQISEntityIds_Hidden | AQISPremIdProcessType_Hidden | CPQuestionGenDate_Hidden

How do you insert the values into your database? Do you use PHP?

Your string is twice delimited type with '*' and '=' as delimiters. So, if you use PHP, you could explode the string to get the desired values and store them in seperate fields in a table. That way, you could handle the values much easier (and this is the purpose of a database).

Again, I am sorry if the above is not possible in your project. I write this answer, just in case you could fix some things in your whole code approach and database design to make things work easier.

treecon
  • 2,415
  • 2
  • 14
  • 28
  • Hi. I dont have control over the system they are using, so I'm kinda stuck with this data. Is there any way to review the contents of this field and extract it out? – Seb Jun 08 '17 at 23:59
0

You can use charindex(<what>, <where>) function. First, to find index of AQISPremIdProcessType_Hidden= string. Second, to find index of first * after it. Here an example:

declare
    @testString nvarchar(max) = N'AQISEntityIds_Hidden=CN0001MB*AQISPremIdProcessType_Hidden=N2725/D*CPQuestionGenDate_Hidden=20/01/2017',
    @field nvarchar(max) = N'AQISPremIdProcessType_Hidden=';

;with _rawInfo as(
    select @testString as [Str]
)
, _trimmedStart as (
    select
        [Str] as [Origin],
        -- check here the math: 2nd arg = length of the tail, so we take
        -- whole length [StrLen] and subtract index of @field and its length
        right([Str], [StrLen] - FieldInd - FieldLen + 1) as Tail
    from _rawInfo
    cross apply (
        select
            len([Str]) as [StrLen],
            charindex(@field, [Str]) as FieldInd,
            len(@field) as FieldLen
    ) x
), _result as (
    select
        [Origin],
        Tail,
        -- 2nd arg = length of the left cut, so we just take index of '*'
        left(Tail, charindex(N'*', Tail) - 1) as [Value]
    from _trimmedStart
)
select
    *
from _result

UPD. And query adapted for your data:

;with _trimmedStart as (
    select
        jd.*,
        right(AddInfo, AddInfoLen - FieldInd - FieldLen + 1) as AddInfoTail
    from JobDeclaration jd
    cross apply (
        select
            len(AddInfo) as AddInfoLen,
            charindex(@field, AddInfo) as FieldInd,
            len(@field) as FieldLen
    ) x
), _result as (
    select
        t.*,
        left(AddInfoTail, charindex(N'*', AddInfoTail) - 1) as [AQISPremIdProcessType_Hidden]
    from _trimmedStart t
)
select
    *
from _result
pkuderov
  • 3,501
  • 2
  • 28
  • 46
  • well, this query is clearly over my head or pay grade! Thank you. If the table is JobDeclaration and the field I'm searching for is "AdditionalInfo" how would I include these in the query? – Seb Jun 09 '17 at 01:05
  • @Seb updated my answer. You'd have all fields from your original table and the last one would be value of the field you need. – pkuderov Jun 09 '17 at 15:39
  • Hi Pkuderov. This is making more sense now. Thank you for the update. one question though - what is "AdditionalInfo"? the field I have to search through in the JObDeclaration table is the "AddInfo" field, so I'm not quite sure where AdditionalInfo comes into it? – Seb Jun 12 '17 at 11:07
  • @Seb, maaaaan, just reread your first comment :) you said you need AdditionalInfo. Okay, just change it to AddInfo. Sorry, I'm on weekend and answer you from my tablet - I'll edit it tomorrow – pkuderov Jun 12 '17 at 18:08
  • @Seb if my answer solved your problem, it'd be right to accept it. That's how this site works - you asking a help and then 'pay back' accepting or upvoting/downvoting answers. – pkuderov Jul 07 '17 at 17:50