web
You’re offline. This is a read only version of the page.
close
Skip to main content
Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Answered

compare enum with string on query

(0) ShareShare
ReportReport
Posted on by 2,050

greetings everyone

how can i compare enum label with string?

in my example i want compare label days with date but i got error because there's day name spacial enum 

select StartTime from jmgProfileSpec
                where jmgProfileSpec.ProfileType==jmgProfileTypeTable.RecId
                && jmgProfileSpec.DayId==dayName(dayOfWk( jmgStampJournalTrans.StartDate))
                && jmgProfileSpec.Profile==jmgStampJournalTable.Profile;

how can i get and compare label of jmgProfileSpec.DayId  ?

i did try this 

select StartTime from jmgProfileSpec
                where jmgProfileSpec.ProfileType==jmgProfileTypeTable.RecId
                && jmgProfileSpec.DayId==dayOfWk( jmgStampJournalTrans.StartDate)
                && jmgProfileSpec.Profile==jmgStampJournalTable.Profile;

but i still geting compare with wrong day because jmgProfileSpec.DayId still give me wrong day

any idea to solve that?

I have the same question (0)
  • Suggested answer
    Martin Dráb Profile Picture
    236,353 Most Valuable Professional on at
    RE: compare enum with string on query

    Enum are actually named integer values; the database contains these numbers and not enum value labels.

    If DayId is an enum, trying to compare it with a string is simply wrong. Use one of the values of the enum.

  • Suggested answer
    nmaenpaa Profile Picture
    101,160 Moderator on at
    RE: compare enum with string on query

    function dayOfWk returns an integer between 1-7. Monday is 1 and so on.

    JmgProfileSpec.DayId is an enum where values 0-6 correspond to days of the week, and Monday equals 0.

    So we can change your statement just a little bit:

    select StartTime from jmgProfileSpec
                    where jmgProfileSpec.ProfileType==jmgProfileTypeTable.RecId
                    && jmgProfileSpec.DayId==dayOfWk( jmgStampJournalTrans.StartDate) - 1
                    && jmgProfileSpec.Profile==jmgStampJournalTable.Profile;

  • Blue Wang Profile Picture
    on at
    RE: compare enum with string on query

    Hi Mohammed,

    The DayId value is derived from JmgDayIdEnum. As Nikolaos reminds that the enumeration field 0 represents Monday, which requires operation.

    You can try to use Global :: enum2int (jmgProfileSpec.DayId), and then compare it with dayOfWk (jmgStampJournalTrans.StartDate).

    global::enum2int(jmgProfileSpec.DayId)   1  == dayOfWk( jmgStampJournalTrans.StartDate)
    OR
    global::enum2int(jmgProfileSpec.DayId)  == dayOfWk( jmgStampJournalTrans.StartDate) -1

  • mohammed.mqi Profile Picture
    2,050 on at
    RE: compare enum with string on query

    ok thanks all for replay

    but even if i decrease ( dayOfWk( jmgStampJournalTrans.StartDate) -1 ) or increase (enum2int(jmgProfileSpec.DayId) + 1)

    i still get wrong day from that query because last day on DayId is Special

    i think the solution must be compare label of that enum

  • Suggested answer
    nmaenpaa Profile Picture
    101,160 Moderator on at
    RE: compare enum with string on query

    I don't see how using the label would change anything. Any anyway it's not possible.

    Could you explain how the last enumeration of DayId would cause issues? Could you share an example where the statement doesn't return you the expected result?

    If jmgStampJournalTrans.StartDate is Monday, then dayOfWk(StartDat)e - 1 is  0 and it corresponds to jmgProfileSpec.DayId Monday (enum value 0)

    If jmgStampJournalTrans.StartDate is Tuesday, then dayOfWk(StartDate) - 1 is 1  and it corresponds tojmgProfileSpec.DayId Tuesday (enum value 1)

    If jmgStampJournalTrans.StartDate is Wednesday, then dayOfWk(StartDate) - 1 is 2  and it corresponds tojmgProfileSpec.DayId Wednesday (enum value 2)

    If jmgStampJournalTrans.StartDate is Thursday, then dayOfWk(StartDate) - 1 is 3  and it corresponds tojmgProfileSpec.DayId Thursday (enum value 3)

    If jmgStampJournalTrans.StartDate is Friday, then dayOfWk(StartDate) - 1 is 4  and it corresponds tojmgProfileSpec.DayId Friday (enum value 4)

    If jmgStampJournalTrans.StartDate is Saturday, then dayOfWk(StartDate) - 1 is 5  and it corresponds tojmgProfileSpec.DayId Saturday (enum value 5)

    If jmgStampJournalTrans.StartDate is Sunday, then dayOfWk(StartDate) - 1 is 6  and it corresponds tojmgProfileSpec.DayId Sunday (enum value 6)

    But then the question is, would you like to find these "Special" records somehow? You can never find it, with enum labels or numerical values, if you search by weekdays Monday-Sunday.

  • Blue Wang Profile Picture
    on at
    RE: compare enum with string on query

    Hi Mohammed,

    Yes, I agree.

    When you use dayOfWk ()-1, you will not get a value of 7, which is the "special" value in the enumeration type, so it will not affect their comparison.

  • Verified answer
    Blue Wang Profile Picture
    on at
    RE: compare enum with string on query

    Hi Mohammed,

    I f you want to get label of enum ,You should use SysDictEnum class.

    SysDictEnum dictEnum = new SysDictEnum(enumnum(JmgDayIdEnum));
    str valueLabel = dictEnum.value2Label(jmgProfileSpec.DayId);
    str valueName = dictEnum.value2Name(jmgProfileSpec.DayId);

    0830.PNG

  • Hariharans87 Profile Picture
    3 on at
    RE: compare enum with string on query

    jmgStampJournalTrans.StartDate is used to store only date. So, the date only have Sun, Mon, Tue, Wed, Thu, Fri and Sat. That means no "Special". Even if you compare with label you couldn't get "Special" record from jmgStampJournalTrans table.

  • mohammed.mqi Profile Picture
    2,050 on at
    RE: compare enum with string on query

    thanks all for replay

    Your answers all seem to make sense but in query i can't use it

    by the way i found long solution

    str daynamefromtrans=dayName(dayOfWk( jmgStampJournalTrans.StartDate));
    
                    switch (daynamefromtrans)
                    {
                        case "Monday" :
    
                        select StartTime from jmgProfileSpec
                    where jmgProfileSpec.ProfileType==jmgProfileTypeTable.RecId
                    && jmgProfileSpec.DayId==0
                    && jmgProfileSpec.Profile==jmgStampJournalTable.Profile;
    
                        break;
    
                        case "Tuesday" :
    
                        select StartTime from jmgProfileSpec
                    where jmgProfileSpec.ProfileType==jmgProfileTypeTable.RecId
                    && jmgProfileSpec.DayId==1
                    && jmgProfileSpec.Profile==jmgStampJournalTable.Profile;
    
                        break;
    
                        case "Wednesday" :
    
                        select StartTime from jmgProfileSpec
                    where jmgProfileSpec.ProfileType==jmgProfileTypeTable.RecId
                    && jmgProfileSpec.DayId==2
                    && jmgProfileSpec.Profile==jmgStampJournalTable.Profile;
    
                        break;
    
                        case "Thursday" :
    
                        select StartTime from jmgProfileSpec
                    where jmgProfileSpec.ProfileType==jmgProfileTypeTable.RecId
                    && jmgProfileSpec.DayId==3
                    && jmgProfileSpec.Profile==jmgStampJournalTable.Profile;
    
                        break;
    
                        case "Friday" :
    
                        select StartTime from jmgProfileSpec
                    where jmgProfileSpec.ProfileType==jmgProfileTypeTable.RecId
                    && jmgProfileSpec.DayId==4
                    && jmgProfileSpec.Profile==jmgStampJournalTable.Profile;
    
                        break;
    
                        case "Saturday" :
    
                        select StartTime from jmgProfileSpec
                    where jmgProfileSpec.ProfileType==jmgProfileTypeTable.RecId
                    && jmgProfileSpec.DayId==5
                    && jmgProfileSpec.Profile==jmgStampJournalTable.Profile;
    
                        break;
    
                        case "Sunday" :
    
                        select StartTime from jmgProfileSpec
                    where jmgProfileSpec.ProfileType==jmgProfileTypeTable.RecId
                    && jmgProfileSpec.DayId==6
                    && jmgProfileSpec.Profile==jmgStampJournalTable.Profile;
    
                        break;
                    }

  • Suggested answer
    nmaenpaa Profile Picture
    101,160 Moderator on at
    RE: compare enum with string on query

    Could you share why you "can't use it in query?

    Your current code works only if the user's language is English.

    Please at least use the dayOfWk values 1-7 in your switch case, instead of hard coded day names.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Abhilash Warrier – Community Spotlight

We are honored to recognize Abhilash Warrier as our Community Spotlight honoree for…

Leaderboard > Finance | Project Operations, Human Resources, AX, GP, SL

#1
CA Neeraj Kumar Profile Picture

CA Neeraj Kumar 2,028

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 878 Super User 2025 Season 2

#3
Sohaib Cheema Profile Picture

Sohaib Cheema 579 User Group Leader

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans