簡體   English   中英

SQL Server:創建新表以將描述字段分解為新的有組織表

[英]SQL Server : creating new table to break up description field into a new organized table

在我們的SQL Server數據庫中,我在dbo.tickets表中有一個description列。 這個數據是一個爛攤子,我必須撤出它的數據,即使它有它不是一個真正的串**分離一些文字。

是否有可能將此描述數據拉出來創建一個新表,可能將其與ID綁定並加入新表以查看更好地顯示的數據並實際使用它,因為有人明白將其全部轉儲到description

這是一個票證描述的樣子:

**You are receiving a Documented Verbal Warning ECR.**

**ECR Category:** $100 or more, Over/Short
**Date of ECR Incident:** 2018-04-12
**Date of ECR Discovery:** 2018-04-12
**Location of ECR Incident:** Palma Sola
**Overage or Shortage:** Shortage
**$ Amount Over/Short:** 138.92
**Checkup/Balance Person:** Jennifer Brierton

**This is considered a serious event and has been reviewed by the CSAD Assistant Director and/or Director for appropriate action.**

**This is being considered your 1st Cash Handling Event.**

You must always strive to ensure a structured procedure is followed when counting, collecting, refunding, and depositing cash and/or checks. Cash handling is a critical part of our job; it is one of our Six Standards of Excellence and is taken into consideration during your Annual Performance Evaluation.

During the counseling session to review this ECR in the Manager’s office, you will be provided the opportunity to make comments regarding this ECR. If you do not have any comments during the counseling session, you must respond stating, “I do not have any commentsâ€.

Any comments made by the Associate/Manager will be reviewed by the CSAD Director and an email will be sent to the Associate and Manager.


**Detailed description of incident**: On 04/12/2018 Michelle processed a title and registration transfer.  The total amount of the transaction was $154.35.  Michelle in error, ran the debit card for $15.43, therefore creating a shortage in the amount of $138.92.  The customer was contacted the same day and returned to pay the shortage.

**Actions you can take to avoid this incident in the future:** Stay focused. Maintain a solid routine. Spend a few extra seconds to double check while making change, counting back change, collecting, and depositing monies. Refer to CCARS-05 (Tips & Hints for Proper Cash Handling).

Per CCARS-11 it states the following: Enter the amount of the debit card transaction in the DEBIT CARD field **directly from the debit card receipt for the transaction.**  CCARS and the debit machine do not "talk" over any type of network; this will help ensure the accuracy of the transaction.
**BEST PRACTICE!** Hit the "Debit Card` button on the CCARS screen and (1) the system will display a message reminder to enter the amount from the debit card receipt and (2) automatically place the cursor in the debit field.

By following these steps, they serve as a safety net to ensure the correct amount is collected and entered in CCARS.

我需要弄清楚如何提取數據,如ECR類別,ECR發現日期,ECR事件的位置,名稱......有沒有辦法將其分解為新表? 從中提取數據更容易。 (所有描述都非常適合這種形式的匹配。)

任何有關這方面的幫助將不勝感激! 我不確定從哪里開始。

我試過的

select
    u.Id, u.Name, t.submitter_id, t.description
from 
    Users u 
join
    Tickets t on t.submitter_id = u.Id 
where 
    t.created_at between '2017-11-01' and '2018-08-23' 
    and ',' + t.tags + ',' like '%,' + 'ecr_administered' + ',%'

(從這里)無法弄清楚如何分解描述,以便我可以拉出ECR類別,ECR類別的日期和人名,並將其放入連接表中。

你需要做的就是找到標題中每個字段的索引:

**ECR Category:** $100 or more, Over/Short
**Date of ECR Incident:** 2018-04-12
**Date of ECR Discovery:** 2018-04-12
**Location of ECR Incident:** Palma Sola
**Overage or Shortage:** Shortage
**$ Amount Over/Short:** 138.92
**Checkup/Balance Person:** Jennifer Brierton

所以你可以使用substring函數。

為了獲得一些性能,我們可以用這種方式刪除不相關的部分:

select substring(description, 1, charindex(char(10), description, charindex('**Checkup/Balance Person', description)) ) dscr
from tickets

簡單地說,我們查找char(10) ,它是標題中最后一個字段后面的SQL中的換行符,因此我們刪除了所有不必要的信息。 我會將它存儲在一些臨時表或表變量中。 現在我們將有大約400個字符的文本列,這不是那么糟糕。

在下面的查詢中我假設#tempTabledscr列:)

要獲得所需的所有數據,只需將charindexsubstring一起使用即可:

select substring(dscr, ectCatStart, charindex(char(10), dscr, ectCatStart) - ectCatStart - 1),
       substring(dscr, dateOfEcr, charindex(char(10), dscr, dateOfEcr) - dateOfEcr - 1),
       substring(dscr, dateOfDiscovery, charindex(char(10), dscr, dateOfDiscovery) - dateOfDiscovery - 1),
       substring(dscr, location, charindex(char(10), dscr, location) - location - 1),
       substring(dscr, overShort, charindex(char(10), dscr, overShort) - overShort - 1),
       substring(dscr, amount, charindex(char(10), dscr, amount) - amount - 1),
       substring(dscr, person, charindex(char(10), dscr, person) - person - 1)
from (
    select charindex('**ECR Category:**', dscr) + len('**ECR Category:**') + 1 ectCatStart,
           charindex('**Date of ECR Incident:**', dscr) + len('**Date of ECR Incident:**') + 1 dateOfEcr,
           charindex('**Date of ECR Discovery:**', dscr) + len('**Date of ECR Discovery:**') + 1 dateOfDiscovery,
           charindex('**Location of ECR Incident:**', dscr) + len('**Location of ECR Incident:**') + 1 location,
           charindex('**Overage or Shortage:**', dscr) + len('**Overage or Shortage:**') + 1 overShort,
           charindex('**$ Amount Over/Short:**', dscr) + len('**$ Amount Over/Short:**') + 1 amount,
           charindex('**Checkup/Balance Person:**', dscr) + len('**Checkup/Balance Person:**') + 1 person,
           dscr
    from #tempTable
) a

您可以通過將字符串轉換為xml,然后使用xpath獲取所需的字段來完成此操作。

你需要注意你的字符串最終有效的xml,所以可能需要一些額外的替換。 你可以看到我有一個嵌入式“&”。

此示例顯示了如何使用描述的前5行提取前兩個字段:

declare @tickets table (x varchar(1000))
insert @tickets values ('**You are receiving a Documented Verbal Warning ECR.**

**ECR Category:** $100 or more, Over/Short
**Date of ECR Incident:** 2018-04-12
**Date of ECR Discovery:** 2018-04-12
**Location of ECR Incident:** Palma Sola')

;with x as (
    select convert(xml,'<row>'+replace(replace(x,'**','</row><row>'),'&','&amp;')+'</row>') x 
    from @tickets
)
select 
    x.value('(/row[. >> (/row[. = "ECR Category:"])[1]])[1]','varchar(100)') as ECRCategory,
    x.value('(/row[. >> (/row[. = "Date of ECR Incident:"])[1]])[1]','varchar(100)') as ECRDate
from x

編輯:以上給出了一個完整的例子。 要使用原始查詢,請執行以下操作:

;with t as (
    select submitter_id, description, convert(xml,'<row>'+replace(replace(tags,'**','</row><row>'),'&','&amp;')+'</row>') x 
    from Tickets
    where created_at between '2017-11-01' and '2018-08-23' 
)
select u.Id, u.Name, t.submitter_id, t.description,
    x.value('(/row[. >> (/row[. = "ECR Category:"])[1]])[1]','varchar(100)') as ECRCategory,
    x.value('(/row[. >> (/row[. = "Date of ECR Incident:"])[1]])[1]','varchar(100)') as ECRDate
from Users u 
join t on t.submitter_id = u.Id 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM