2012-10-30 3 views
0

가능한 복제를 월 이름 변환 : 나는 datename(MM, patientinfo.PatientDOB) 불리는 아래의 스크립트의 영역에 달 번호로 월 이름 변환을 시도하고
Convert Month Name to Month Number Function in SQL월 번호

. 예를 들어, 현재 January으로 돌아오고 있으며 01 또는 1으로 반환하고 싶습니다.

CASE WHEN 'January' THEN 1을 읽어야 할 수도 있지만 삽입 할 곳이나 정확한 구문을 모르겠습니다.

select 
    Update_Log.RecordID as 'Dictation ID', 
    cast(dictations.DOS as DATE) as 'Date of Service', 
    groups.GroupName, 
    rtrim(aspnet_Users.Firstname)+' '+rtrim(aspnet_Users.LastName) as Provider, 
    Update_Log.Updated, 
    Update_XRef.columnName as 'Update Type', 
    "New Value" = 
     case 
      when update_xref.columnname = 'FacilityID' then (select facility.FacilityName+' [gID:'+update_log.NewValue+']' from facility where update_log.NewValue = facility.FacilityID) 
      when update_xref.columnname = 'DOS' then (select left(update_log.NewValue, 11) as 'New Value') 
      when update_xref.columnname = 'DictatorID' then (select rtrim(aspnet_users.Firstname)+' '+rtrim(aspnet_users.lastname) from aspnet_Users where aspnet_users.userid1 = update_log.UpdatedBy) 
      when update_xref.columnname = 'PatientID' then (select rtrim(patientinfo.PatientFirst)+' '+rtrim(patientinfo.PatientLast)+' ['+datename(MM, patientinfo.PatientDOB)+'/'+datename(DD, patientinfo.PatientDOB)+'/'+datename(YY, patientinfo.PatientDOB)+ ', gID:'+rtrim(patientinfo.PatientID)+']' from patientinfo where patientinfo.PatientID = Update_Log.NewValue) 
     end, 
    "Old Value" = 
     case 
      when update_xref.columnname = 'FacilityID' then (select facility.FacilityName+' [gID:'+update_log.OldValue+']' from facility where update_log.OldValue = facility.FacilityID) 
      when update_xref.columnname = 'DOS' then (select left(update_log.OldValue, 11) as 'Old Value') 
      when update_xref.columnname = 'DictatorID' then (select rtrim(aspnet_users.Firstname)+' '+rtrim(aspnet_users.lastname) from aspnet_Users where aspnet_users.userid1 = update_log.UpdatedBy) 
      when update_xref.columnname = 'PatientID' then (select rtrim(patientinfo.PatientFirst)+' '+rtrim(patientinfo.PatientLast)+' ['+left(datename(MM, patientinfo.PatientDOB),3)+'/'+datename(DD, patientinfo.PatientDOB)+'/'+datename(YY, patientinfo.PatientDOB)+ ', gID:'+rtrim(patientinfo.PatientID)+']' from patientinfo where patientinfo.PatientID = Update_Log.OldValue) 
     End 
from 
    Update_Log, 
    Update_XRef, 
    aspnet_Users, 
    groups, 
    Dictations 
where 
    Update_log.XRefID = Update_XRef.xRefID 
    and Update_Log.UpdatedBy = aspnet_Users.userid1 
    and aspnet_users.Groupid = Groups.GroupID 
    and dictations.Dictationid = Update_Log.RecordID 
    and Update_Log.Updated > dictations.DateofSignature 
order by 
    groups.GroupName, 
    aspnet_users.LastName, 
    update_log.RecordID 

답변

3

날짜의 정수 월 부분을 얻기 위해, 전체 & 입력 한 날짜를 patientinfo.PatientDOB을 가정 할;

datepart(MM, patientinfo.PatientDOB) 

또는 문자열에 대한 cast(?? as varchar(2))에 싸여

month(patientinfo.PatientDOB) 

.

+0

답변 해 주셔서 감사합니다. month 또는 datepart를 사용할 때 다음 메시지가 표시됩니다 .L –

+0

nvarchar 값 'JOHN SMITH [October /']을 데이터 유형 int로 변환 할 때 변환이 실패했습니다. –

+0

'datepart'는 정수를 반환합니다. 문자열로 변환하지 않고 문자열에 추가 할 수는 없습니다. '텍스트 또는 문자 필드 선택 '+ cast (datepart (MM, patientinfo.PatientDOB) as varchar (2)) +'XXX ... '' –