0

共通点のない2つのテーブルからデータをコンパイルする必要があります。

これを行うには、必要なビットを抽出するビューを作成しました。これは相互結合されているため、日付でフィルタリングして不要な行を削除する必要があります。

しかし、日付に問題があることがわかりました。

基本的に、一部のユーザーはシステムにUS形式を設定する必要があります。これにより、日付がデータベースに入力されます(m/d/yyyy)。

日付の大部分は(正しく)英国形式です(dd/mm/yyyy

もちろん、すべての日付を同じ形式にする必要がありますが、CONVERTとCASTは機能していないようです。

そのため、CASEを使用して文字列の長さと内容を決定するというアイデアを思いつきました。

基本的に、長さ10(たとえば、2012年1月15日)を探し、文字4と5が12より大きい場合を除いて、正しい(UK)日付であると想定します。

9文字の場合、最初の「/」の位置を探す必要があります。これにより、d/mmかdd/mかがわかります(年は常に4文字になります)。

最後に、8文字の場合は、日付が正しくない(US)必要があると想定し、数字を逆にして、フィラーとしてゼロを入力します。

ただし、スクリプトを実行しようとすると、次のようになります。

Msg 102, Level 15, State 1, Line 12
Incorrect syntax near '='.
Msg 156, Level 15, State 1, Line 16
Incorrect syntax near the keyword 'THEN'.
Msg 4145, Level 15, State 1, Line 22
An expression of non-boolean type specified in a context where a condition is expected, near 'ELSE'.
Msg 102, Level 15, State 1, Line 24
Incorrect syntax near '@Day'.
Msg 4145, Level 15, State 1, Line 29
An expression of non-boolean type specified in a context where a condition is expected, near 'END'.
Msg 156, Level 15, State 1, Line 39
Incorrect syntax near the keyword 'IF'.
Msg 102, Level 15, State 1, Line 40
Incorrect syntax near ','.
Msg 102, Level 15, State 1, Line 41
Incorrect syntax near ','.
Msg 102, Level 15, State 1, Line 46
Incorrect syntax near ','.
Msg 156, Level 15, State 1, Line 50
Incorrect syntax near the keyword 'THEN'.
Msg 102, Level 15, State 1, Line 51
Incorrect syntax near ','.
Msg 102, Level 15, State 1, Line 52
Incorrect syntax near ','.

失敗しているスクリプトの部分は以下のとおりです。誰かが私がそれをどうする必要があるか(または実際、日付を整理するためのより良い方法)について提案してくれることを願っています。

私が達成しようとしているのは、ソーステーブル(mbrProject)から直接新しいテーブル(またはビュー)の[START_DATE]フィールドにデータを入力することです。ここで、IDが一致します( mbrProjectから)だけでなく、日付形式の変更をその場で適用しようとしています:

declare @length int,
    @Day nchar(3),
    @Month nchar(3),
    @Year nchar(5)

Update ProjectDates
SET [Start_Date] = 
(CASE
    WHEN len(mbrProject.[Start_Date]) = 8 THEN
        @Day = '0' & SUBSTRING(mbrProject.[START_DATE],3,2), @Month = '0' & SUBSTRING(mbrProject.[START_DATE],1,2), @Year = right(mbrProject.[START_DATE],5)
        @Day + @Month + @Year

    WHEN len(mbrProject.[Start_Date]) = 9 THEN
        IF charindex('/',mbrProject.[start_date]) = 2 THEN
        @Day = SUBSTRING(mbrProject.[START_DATE],3,3), @Month = '0' & SUBSTRING(mbrProject.[START_DATE],1,2), @Year = right(mbrProject.[START_DATE],5)
        ELSE @Day = '0' & SUBSTRING(mbrProject.[START_DATE],4,2), @Month = SUBSTRING(mbrProject.[START_DATE],1,3), @Year = right(mbrProject.[START_DATE],5)
        END IF
        @Day + @Month + @Year

    ELSE
        IF SUBSTRING(mbrProject.[START_DATE],4,2) > 12
            @Day = SUBSTRING(mbrProject.[START_DATE],4,3), @Month = SUBSTRING(mbrProject.[START_DATE],1,3), @Year = right(mbrProject.[START_DATE],5)
            ELSE @Day = SUBSTRING(mbrProject.[START_DATE],1,3), @Month = SUBSTRING(mbrProject.[START_DATE],4,3), @Year = right(mbrProject.[START_DATE],5)
        END IF
        @Day + @Month + @Year

    END)

Where ProjectDates.[START_DATE] = mbrProject.[START_DATE]

================================================== =====================

** 編集 **

OK、Kafに感謝します。汎用スクリプトを使用して新しいuodateスクリプトを作成しましたが、もう1つ問題があります。

新しいスクリプトを表示することから始めます。

Update ProjectDates
SET [Start_Date] =

(
select right([Start_Date],4) +  
    case 
        when len([Start_Date])=10 then 
            substring([Start_Date],4,2) + left([Start_Date],2)

        else    right('0' + left([Start_Date],firstIndex-1),2) + 
                right('0' + substring([Start_Date],firstIndex+1,secondIndex - firstIndex-1),2) 
                end

from 
    (
    select mp.[Start_Date], charindex('/',mp.[Start_Date],1) firstIndex,
          charindex('/',mp.[Start_Date],charindex('/',mp.[Start_Date],1)+1) secondIndex

    from mbrProject mp
    join ProjectDates pd
    on mp.ID = pd.Project_ID
    )A

where ProjectDates.Project_ID = mbrProject.ID
)

何が起こるかというと、SELECTステートメントのみを実行すると、たとえば次のすべてが実行されます。

select right([Start_Date],4)

至るまで

on mp.ID = pd.Project_ID
)A

これは機能します。

残念ながら、完全なスクリプトは正しく解析されますが、次のエラーが返されます。

Msg 4104, Level 16, State 1, Line 25
The multi-part identifier "mbrProject.ID" could not be bound.

何かアイデアはありますか?

4

1 に答える 1

0

この一般的なソリューションを試して、ISO日付形式()とは異なる長さのさまざまな形式を取得してくださいyyyymmdd。(日付がdd / mm / yyyyの場合にのみ10文字であると想定)文字列dateをISO形式に取得したら、比較のために日付/日時タイプに変換します。

SQLフィドルはこちら

--create a table for the demo.
create table T(mydate varchar(50))
insert into T (mydate, details)
values ('m/d/yyyy'),('mm/d/yyyy'),('m/dd/yyyy'),('dd/mm/yyyy')


select mydate,
       right(mydate,4) +  case when len(mydate)=10 then 
                               substring(mydate,4,2) + left(mydate,2)
              else right('0' + left(mydate,firstIndex-1),2) + 
       right('0' + substring(mydate,firstIndex+1,secondIndex - firstIndex-1),2) 
                        end ISO_format
from (
select mydate, charindex('/',mydate,1) firstIndex,
              charindex('/',mydate,charindex('/',mydate,1)+1) secondIndex
from T ) A

--Results
MYDATE      ISO_FORMAT
m/d/yyyy    yyyy0m0d
mm/d/yyyy   yyyymm0d
m/dd/yyyy   yyyy0mdd
dd/mm/yyyy  yyyymmdd
于 2013-01-14T17:09:13.000 に答える