将月份名称转换为月份编号

时间:2012-10-30 14:56:29

标签: sql

  

可能重复:
  Convert Month Name to Month Number Function in SQL

我正在尝试将月份名称转换为下面名为datename(MM, patientinfo.PatientDOB)的脚本区域中的月份编号。例如,它目前返回January,我希望它返回011

根据我的阅读,我可能需要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

2 个答案:

答案 0 :(得分:3)

假设patientinfo.PatientDOB是完整的&键入日期,以获取日期的整数月份部分;

datepart(MM, patientinfo.PatientDOB)

month(patientinfo.PatientDOB)

cast(?? as varchar(2))中包含一个字符串。

答案 1 :(得分:0)

我之前在这里回答过:

Convert month name to month number in SQL Server

请告诉我这是否适合您!