简体   繁体   中英

Dynamics NAV SQL Lead Time format conversion

I have a small issue getting data, one field in particular, from table replicated from Microsoft Dynamics NAV.

There is a field storing lead time in strange format and as a newbie I thought to ask smarter from me for advice.

There is a number followed by a character, which as I assume indicates "units".

示例数据

What I got so far is character indicating days is used in row 2 & 4 and one for weeks is used in rows 1 & 3

Could someone tell is it some "standard" way of storing this kind of data?

Tell me how to convert those into days or point me in right direction?

I believe I found a solution, but it seems to be very ugly!

SOLUTION

SELECT
  [ItmCode] AS [ItemCode],
  [ItmDesc] AS [ItemDesc],
  [ItmLead] AS [LeadTime_Orginal],
  CASE WHEN ISNULL(ItmLead, '') ='' THEN 0 ELSE CHOOSE(ASCII(RIGHT(ItmLead, 1)), 0,1,0,7,0,0,0,0,0,0,0,0,0,0,0) * CAST(LEFT(ItmLead, LEN(ItmLead)-1) AS int) AS [LeadTimeInDays],
FROM dbo.nav_items;

Any better solution would be very appreciated!

I believe I found a solution, but it seems to be very ugly!

After using SQL ASCII on extra on last character it returned numbers 2 and 4 then I used CHOOSE to assing to those values numbers 1 and 7.

Having those allowed me to multiply it by first part of the field and achieve my objective.

SOLUTION

SELECT
  [ItmCode] AS [ItemCode],
  [ItmDesc] AS [ItemDesc],
  [ItmLead] AS [LeadTime_Orginal],
  CASE WHEN ISNULL(ItmLead, '') ='' 
  THEN 0 
  ELSE CHOOSE(ASCII(RIGHT(ItmLead, 1)), 0,1,0,7,0,0) * CAST(LEFT(ItmLead, LEN(ItmLead)-1) AS int) AS [LeadTimeInDays],
FROM dbo.nav_items;

Any better solution would be very appreciated!

I know this post is old, but I've seen others asking for help with this.

The code below will only work for formats with D, W, M or Y since I don't use C, WD or Q. Edit to match your database names.

Original code is taken from somewhere else on the inte.net.

Lead time date format is as following:

1 = C - Current (Last char can't end on this, and code below wont work for fields with this char)

2 = D - Day

3 = WD - WorkDay

4 = W - Week

5 = M - Month

6 = Q - Quarter

7 = Y - Year

In your select, do a case as following:

,   Case
        When right([Vendor Lead Time],1) = char(2)  Then concat(left([Vendor Lead Time],len([Vendor Lead Time])-1),'D')
        When right([Vendor Lead Time],1) = char(3)  Then concat(left([Vendor Lead Time],len([Vendor Lead Time])-1),'WD')
        When right([Vendor Lead Time],1) = char(4)  Then concat(left([Vendor Lead Time],len([Vendor Lead Time])-1),'W')
        When right([Vendor Lead Time],1) = char(5)  Then concat(left([Vendor Lead Time],len([Vendor Lead Time])-1),'M')
        When right([Vendor Lead Time],1) = char(6)  Then concat(left([Vendor Lead Time],len([Vendor Lead Time])-1),'Q')
        When right([Vendor Lead Time],1) = char(7)  Then concat(left([Vendor Lead Time],len([Vendor Lead Time])-1),'Y')
                                            End             as 'Vendor Lead Time'
,   Case
        When [Vendor Lead Time] is null             Then 0
        When [Vendor Lead Time] like ''             Then 0
        When right([Vendor Lead Time],1) = char(2)  Then cast(left([Vendor Lead Time],len([Vendor Lead Time])-1)as int)
        When right([Vendor Lead Time],1) = char(4)  Then cast(left([Vendor Lead Time],len([Vendor Lead Time])-1)*7 as int)
        When right([Vendor Lead Time],1) = char(5)  Then cast(left([Vendor Lead Time],len([Vendor Lead Time])-1)*30 as int)
        When right([Vendor Lead Time],1) = char(7)  Then cast(left([Vendor Lead Time],len([Vendor Lead Time])-1)*365 as int)
                                            End             as  'Vendor Lead Time Days'
,   Case
        When right([Transport Time],1) = char(2)    Then concat(left([Transport Time],len([Transport Time])-1),'D')
        When right([Transport Time],1) = char(3)    Then concat(left([Transport Time],len([Transport Time])-1),'WD')
        When right([Transport Time],1) = char(4)    Then concat(left([Transport Time],len([Transport Time])-1),'W')
        When right([Transport Time],1) = char(5)    Then concat(left([Transport Time],len([Transport Time])-1),'M')
        When right([Transport Time],1) = char(6)    Then concat(left([Transport Time],len([Transport Time])-1),'Q')
        When right([Transport Time],1) = char(7)    Then concat(left([Transport Time],len([Transport Time])-1),'Y')
                                            End             as 'Vendor Transport Time'
,   Case
        When [Transport Time] is null               Then 0
        When [Transport Time] like ''               Then 0
        When right([Transport Time],1) = char(2)    Then cast(left([Transport Time],len([Transport Time])-1)as int)
        When right([Transport Time],1) = char(4)    Then cast(left([Transport Time],len([Transport Time])-1)*7 as int)
        When right([Transport Time],1) = char(5)    Then cast(left([Transport Time],len([Transport Time])-1)*30 as int)
        When right([Transport Time],1) = char(7)    Then cast(left([Transport Time],len([Transport Time])-1)*365 as int)
                                            End             as 'Vendor Transport Time Days'

For total lead time, do a select for:

([Vendor Lead Time Days]+[Vendor Transport Time Days])  as 'Total Vendor Lead Time'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM