I have a nomenclature to respect while performing some tasks against the Active Directory.
Here's the nomenclature:
TT-EEE-Mnemonic: if TT = 'GA' or 'GS' or 'PA' or 'PF' -> the schema to create is a "group", with a groupScope of Global.
LT-EEE-Mnemonic: if T = 'A' or 'G' or 'I' or 'N' or 'P' -> the schema to create is a "group", with a groupScope of Domain local.
TTT-EEE-Mnemonic: if TTT* = 'CNX' or 'GST' or 'SVC' -> the shema to create is an "user"
T-SSSS-Mnemonic: if T = 'A' or 'L' or 'M' or 'R' or 'S' -> the schema to create is an "organizationUnit"
What I'm after is a simpler and more effective way than this:
If(dn.Substring(3, 2).Contains("GA") _
Or variable.Substring(3, 2).Contains("GS") _
Or dn.Substring(3, 2).Contains("PA") _
Or dn.Substring(3, 2).Contains("PF")) Then
schema = "group" ' Global'
Else If(dn.Substring(4, 1).Contains("A") _
Or dn.Substring(4, 1).Contains("G") _
Or dn.Substring(4, 1).Contains("I") _
Or dn.Substring(4, 1).Contains("N") _
Or dn.Substring(4, 1).Contains("P")) Then
schema = "group" ' Local'
Else If(dn.Substring(3, 3).Contains("CNX") _
' Well... You get the idea, don't you?
End If
I guess I could use a RegularExpression, or perhaps one for each of the nomenclature I got, something alike.
Is there a way a RegularExpression could become handy in this situation? Or would it be best to stick with that old big-If? Any suggestions are welcome.
Sorry for asking, but I'm not used to use RegularExpression. I know they exist, and bit of what they can do, but that's all.
Thanks!
Your code does not seem to conform your description. With your description, you may want the following regular expression:
^(((GA|GS|PA|PF)|L[AGINP]|(CNX|GST|SVC))-EEE|[ALRMS]-SSSS)$
EDIT: you may want to read up this tutorial about what the regular expression means, specifically look for the "Character classes" and "Grouping and alternatives" sections.
In short, the bar character (i.e. |) is the "OR" operator. The square brackets (i.e. []) are the character class; in other words, "OR" between the characters.
It'd vastly reduce the number of tests and explicit Ors.
If Regex.IsMatch(dn, "^CN=(G[AS]|P[AF])-") Then
schema = "group" ' Global 'damn syntax highlighting
ElseIf Regex.IsMatch(dn, "^CN=L[AGINP]-") Then
schema = "group" ' Local 'damn syntax highlighting
ElseIf Regex.IsMatch(dn, "^CN=(CNX|GST|SVC)-") Then
schema = "user"
ElseIf Regex.IsMatch(dn, "^CN=[ALMRS]-") Then
schema = "organizationUnit"
End If
How about...
Dim Schema As String = Nothing
Select Case dn.SubString(3, 2) ' Am not sure about your index of 3 here!
Case "GA", "GS", "PA", "PS"
Schema = "group"
End Select
If IsNothing(Schema) Then
Select Case ...
End If
etc.
Related
I have the following select statement in ABAP:
SELECT munic~mandt VREFER BIS AB ZZELECDATE ZZCERTDATE CONSYEAR ZDIMO ZZONE_M ZZONE_T USAGE_M USAGE_T M2MC M2MT M2RET EXEMPTMCMT EXEMPRET CHARGEMCMT
INTO corresponding fields of table GT_INSTMUNIC_F
FROM ZCI00_INSTMUNIC AS MUNIC
INNER JOIN EVER AS EV on
MUNIC~POD = EV~VREFER(9).
"where EV~BSTATUS = '14' or EV~BSTATUS = '32'.
My problem with the above statement is that does not recognize the substring/offset operation on the 'ON' clause. If i remove the '(9) then
it recognizes the field, otherwise it gives error:
Field ev~refer is unknown. It is neither in one of the specified tables
nor defined by a "DATA" statement. I have also tried doing something similar in the 'Where' clause, receiving a similar error:
LOOP AT gt_instmunic.
clear wa_gt_instmunic_f.
wa_gt_instmunic_f-mandt = gt_instmunic-mandt.
wa_gt_instmunic_f-bis = gt_instmunic-bis.
wa_gt_instmunic_f-ab = gt_instmunic-ab.
wa_gt_instmunic_f-zzelecdate = gt_instmunic-zzelecdate.
wa_gt_instmunic_f-ZZCERTDATE = gt_instmunic-ZZCERTDATE.
wa_gt_instmunic_f-CONSYEAR = gt_instmunic-CONSYEAR.
wa_gt_instmunic_f-ZDIMO = gt_instmunic-ZDIMO.
wa_gt_instmunic_f-ZZONE_M = gt_instmunic-ZZONE_M.
wa_gt_instmunic_f-ZZONE_T = gt_instmunic-ZZONE_T.
wa_gt_instmunic_f-USAGE_M = gt_instmunic-USAGE_M.
wa_gt_instmunic_f-USAGE_T = gt_instmunic-USAGE_T.
temp_pod = gt_instmunic-pod.
SELECT vrefer
FROM ever
INTO wa_gt_instmunic_f-vrefer
WHERE ( vrefer(9) LIKE temp_pod ). " PROBLEM WITH SUBSTRING
"AND ( BSTATUS = '14' OR BSTATUS = '32' ).
ENDSELECT.
WRITE: / sy-dbcnt.
WRITE: / 'wa is: ', wa_gt_instmunic_f.
WRITE: / 'wa-ever is: ', wa_gt_instmunic_f-vrefer.
APPEND wa_gt_instmunic_f TO gt_instmunic_f.
WRITE: / wa_gt_instmunic_f-vrefer.
ENDLOOP.
itab_size = lines( gt_instmunic_f ).
WRITE: / 'Internal table populated with', itab_size, ' lines'.
The basic task i want to implement is to modify a specific field on one table,
pulling values from another. They have a common field ( pod = vrefer(9) ). Thanks in advance for your time.
If you are on a late enough NetWeaver version, it works on 7.51, you can use the OpenSQL function LEFT or SUBSTRING. Your query would look something like:
SELECT munic~mandt VREFER BIS AB ZZELECDATE ZZCERTDATE CONSYEAR ZDIMO ZZONE_M ZZONE_T USAGE_M USAGE_T M2MC M2MT M2RET EXEMPTMCMT EXEMPRET CHARGEMCMT
FROM ZCI00_INSTMUNIC AS MUNIC
INNER JOIN ever AS ev
ON MUNIC~POD EQ LEFT( EV~VREFER, 9 )
INTO corresponding fields of table GT_INSTMUNIC_F.
Note that the INTO clause needs to move to the end of the command as well.
field(9) is a subset operation that is processed by the ABAP environment and can not be translated into a database-level SQL statement (at least not at the moment, but I'd be surprised if it ever will be). Your best bet is either to select the datasets separately and merge them manually (if both are approximately equally large) or pre-select one and use a FAE/IN clause.
They have a common field ( pod = vrefer(9) )
This is a wrong assumption, because they both are not fields, but a field an other thing.
If you really need to do that task through SQL, I'll suggest you to check native SQL sentences like SUBSTRING and check if you can manage to use them within an EXEC_SQL or (better) the CL_SQL* classes.
Disclaimer: it is probably a trivial mistake.
To solve the query at this link:
Can you return the results with a column named sound that returns
"talk" for humans, "bark" for dogs, and "meow" for cats?
I need to write a CASE with multiple possibilities.
What is wrong with the following syntax?
SELECT *
CASE
WHEN species='human' THEN 'talk'
WHEN species='dog' THEN 'bark'
WHEN species='cat' THEN 'meow'
END AS sound
FROM friends_of_pickles
;
I double-checked the syntax bar reading this question and it seems correct to me?
Where is the mistake?
Thank you very much in advance!
Well, you are missing a comma after *:
SELECT *,
(CASE WHEN species = 'human' THEN 'talk'
WHEN species = 'dog' THEN 'bark'
WHEN species = 'cat' THEN 'meow'
END) AS sound
FROM friends_of_pickles;
However, some databases do not allow * with other columns. So a qualified * is required:
SELECT fop.*,
(CASE WHEN species = 'human' THEN 'talk'
WHEN species = 'dog' THEN 'bark'
WHEN species = 'cat' THEN 'meow'
END) AS sound
FROM friends_of_pickles fop;
To be honest, I recommend a qualifying column names (and *s) in general.
I have a below SQL query running in one of my project. I am struggling to understand the "as" concept here. In the result "user_key" and "user_all" are appearing as empty. Where as at the front end "user_all" is the combination of "rx.ord_by_userid" + "rx.ord_by_inst_id,"
SELECT rx.rx_id,
rx.pt_visit_id,
rx.pt_id,
pt_visit.date_time_sch,
' ' as print_dea_ind,
' ' as phys_rx_label,
rx.ord_by_userid,
rx.ord_by_inst_id,
' ' as user_key,
pt_visit.visit_inst_id,
' ' as user_all,
' ' as tp_agt_ind,
FROM rx LEFT OUTER JOIN tx_pln ON rx.tp_name = tx_pln.tp_name AND rx.tp_vers_no = tx_pln.tp_vers_no, pt_visit
WHERE ( pt_visit.pt_visit_id = rx.pt_visit_id ) and
( pt_visit.pt_id = rx.pt_id ) and
( ( rx.pt_id = :pt_id ) and
( rx.rx_id = :rx_id ) )
Thanks.
I think when they query database, they need two fields called "user_key" and "user_all" with empty value for some purpose. However, in the front end, they need to display column "user_all" with the combination of "rx.ord_by_userid" + "rx.ord_by_inst_id" because of business rule.
The meaning of "AS" is just setting the alias of any field which is needed to have a new name. In this situation, new columns "user_key" and "user_all" are set with empty value.
AS just provides the field in the data set a name, or in SQL terms, an alias. In PB, this is usually done so that the DataWindow gives it a consistent, easy name. That is all that AS does.
The other part of your mystery is how these get populated with non-blank values. You were assuming this was done in the SQL with AS, but we can assure you that is not the case. Most likely, this value is being set in a script that fires in the client after the Retrieve() (if I were to bet, I'd bet a script on the DataWindow control, maybe RetrieveRow or RetrieveEnd).
I have a query which returns a bunch of different data, however I want to have it replace all the values upon a certain condition.
What I have written below kind of gives me the result I want but not really. It creates a new column instead of replacing the other one:
SELECT
CASE
WHEN T4.[U_DestType] = '6'
THEN (SELECT
'Company Limited' AS [ShipToCode]
)
END AS [ShipToCode],
T2.[ShipToCode],
T6.[StreetS],
T6.[StreetNoS],
T6.[CityS],
T6.[ZipCodeS],
T6.[CountryS],
T5.[LicTradNum],
T2.[CardCode],
T4.[Phone1],
T4.[E_Mail],
T4.[U_DestType],
CASE
WHEN T4.[Country] = 'GB'
THEN 'EN'
ELSE T4.[Country]
END AS [Country],
T4.[U_ShortName]
FROM[...]
The end goal is to replace all of the columns with some preset values instead of just ShipToCode as above.
I tried putting an EXIST subquery after FROM too but that didn't work either.
Is this possible? I'm probably missing something very obvious.
Many thanks!
You can use an ELSE in your CASE expression to combine the two "columns":
CASE
WHEN T4.[U_DestType] = '6'
THEN (SELECT
'Company Limited' AS [ShipToCode]
)
ELSE T2.[ShipToCode]
END AS [ShipToCode],
And by the way, you didn't need to use a Sub-Select. This would work just as well and is easier to read:
CASE
WHEN T4.[U_DestType] = '6' THEN 'Company Limited'
ELSE T2.[ShipToCode]
END AS [ShipToCode],
Hoping this is something I can fix, or maybe someone can point out an obvious mistake.
I have two queries:
SELECT HIDEORSHOW FROM tblProspects WHERE PROSPECT_ID = 1261484;
HIDEORSHOW
1
SELECT PROSPECT_ID FROM tblProspects WHERE HIDEORSHOW = 1;
PROSPECT_ID
196248
199004
204190
204338
210918
211932
213332
214186
216980
218254
222420
223578
223824
224429
224390
224672
224714
227031
227481
228363
230040
238168
239230
240790
241409
243827
244553
245785
247947
248349
250426
250640
252399
252555
253610
253949
254641
255109
Sorry for the long list I just want you guys to see the madness. Is there any reason why this would happen? There is another prospect that I know of, 1257506, that has this HIDEORSHOW value and does not appear in the list.
I would lay good money that the HIDEORSHOW field is a string of some kind, and that some values have leading or trailing spaces.
This is true, due to implicit CASTing: '1' = 1 (Becomes : '1' = '1')
This is false, even with implicit CASTing: ' 1' = 1 (Becomes : ' 1' = '1')
To test this, try this query (or it's equivalent in your version of SQL)...
SELECT PROSPECT_ID FROM tblProspects WHERE CAST(HIDEORSHOW AS INT) = 1;
This will force the casting to be string => int rather than the other way around.
Or you could try this test...
SELECT '<' + HIDEORSHOW + '>', LEN(HIDEORSHOW) FROM tblProspects WHERE PROSPECT_ID = 1261484;
You'll then have more visibility on the exact value in that field.