2013-04-30 1 views
1

병원의 한 데이터베이스에서 다른 데이터베이스로 데이터를 마이그레이션하는 중입니다. 이전 데이터베이스에서 의사의 전문 ID는 모두 하나의 열 (swvar_specialties)에 있으며 쉼표로 구분됩니다. 새 데이터베이스에서 각 전문 ID에는 고유 한 열 (예 : Specialty1_PrimaryID, Specialty2_PrimaryID, Specialty3_PrimaryID 등)이 있습니다. 이전 데이터베이스에서 데이터를 내보내고 이들을 별도의 열로 구분하려고합니다. 나는 이것을 수행하기 위해 indexofsubstring을 사용할 수 있다는 것을 안다 - 나는 단지 구문에 대한 도움이 필요하다.IndexOf 및/또는 하위 문자열을 사용하여 데이터를 개별 열로 구문 분석

그래서이 쿼리 :

Select swvar_specialties as Specialty1_PrimaryID 
From PhysDirectory 

는 39,52,16과 유사한 결과를 반환 할 수 있습니다. 결과에 Specialty1_PrimaryID = 39, Specialty2_PrimaryID = 52Specialty3_PrimaryID = 16을 표시하려면이 쿼리가 필요합니다. 아래는 지금까지 나의 질문입니다. 나는 결국 명물 테이블에서 명물을 가져 오기 위해 합류 할 것이다. 나는 이것을 먼저 해결해야합니다.

Select pd.ref as PrimaryID, pd.swvar_name_first as FirstName, pd.swvar_name_middle as MiddleName, 
pd.swvar_name_last as LastName, pd.swvar_name_suffix + ' ' + pd.swvar_name_degree as NameSuffix, 
pd.swvar_birthdate as DateOfBirth,pd.swvar_notes as AdditionalInformation, 'images/' + '' + pd.swvar_photo as ImageURL, 
pd.swvar_philosophy as PhilosophyOfCare, pd.swvar_gender as Gender, pd.swvar_specialties as Specialty1_PrimaryID, pd.swvar_languages as Language1_Name 
From PhysDirectory as pd 
+0

전문 분야의 의견 만 있지만 여러 개의 열만 제 3 정규형이 아닙니다. DrID, SpecialtyID가있는 테이블을 가져야 만 Dr는 0 개 또는 다수의 전문 분야를 가질 수 있습니다. – Paparazzi

답변

0

기사 Split function equivalent in T-SQL?은 쉼표로 구분 된 문자열을 분할하는 분할 기능을 사용하는 방법에 대한 몇 가지 세부 사항을 제공합니다.

/* 
    Splits string into parts delimitered with specified character. 
*/ 
CREATE FUNCTION [dbo].[SDF_SplitString] 
(
    @sString nvarchar(2048), 
    @cDelimiter nchar(1) 
) 
RETURNS @tParts TABLE (id bigint IDENTITY, part nvarchar(2048)) 
AS 
BEGIN 
    if @sString is null return 
    declare @iStart int, 
     @iPos int 
    if substring(@sString, 1, 1) = @cDelimiter 
    begin 
     set @iStart = 2 
     insert into @tParts 
     values(null) 
    end 
    else 
     set @iStart = 1 
     while 1=1 
     begin 
      set @iPos = charindex(@cDelimiter, @sString, @iStart) 
      if @iPos = 0 
      set @iPos = len(@sString)+1 
      if @iPos - @iStart > 0   
      insert into @tParts 
       values (substring(@sString, @iStart, @[email protected])) 
      else 
      insert into @tParts 
       values(null) 
      set @iStart = @iPos+1 
      if @iStart > len(@sString) 
      break 
     end 
    RETURN 
END 

은이 분할 기능을 활용할 수 있습니다 귀하의 질의는 다음과 같이 : 우리는 Specialty1_PrimaryID 같은 특정 행을 타겟팅 할 수 있습니다 ID 열을 제공하기 위해이 문서에 제시된 테이블 반환 함수를 수정하여

Select 
    pd.ref as PrimaryID, 
    pd.swvar_name_first as FirstName, 
    pd.swvar_name_middle as MiddleName, 
    pd.swvar_name_last as LastName, 
    pd.swvar_name_suffix + ' ' + pd.swvar_name_degree as LastName, 
    pd.swvar_birthdate as DateOfBirth,pd.swvar_notes as AdditionalInformation, 
    'images/' + '' + pd.swvar_photo as ImageURL, 
    pd.swvar_philosophy as PhilosophyOfCare, pd.swvar_gender as Gender, 
    (Select part from SDF_SplitString(pd.swvar_specialties, ',') where id=1) as Specialty1_PrimaryID, 
    (Select part from SDF_SplitString(pd.swvar_specialties, ',') where id=2) as Specialty2_PrimaryID, 
    pd.swvar_languages as Language1_Name 
From PhysDirectory as pd