question

ahmedsalah-1628 avatar image
0 Votes"
ahmedsalah-1628 asked MelissaMa-msft answered

what condition case when related to portion key mean ?

I work on sql server 2012 I don't understand condition below do
this condition as below
case WHEN Po.PortionKey LIKE '%[_]%' THEN SUBSTRING(Po.PortionKey, LEN(LTRIM(RTRIM(Po.PortionKey)))+1,LEN(LTRIM(RTRIM(Po.PortionKey)))) end as modifiedportionkey
what condition above mean
can please give me sample



sql-server-generalsql-server-transact-sql
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

ErlandSommarskog avatar image
0 Votes"
ErlandSommarskog answered

Why don't you ask the person who wrote it (which I assume is yourself) what the intention is?

My reading is that if the PortionKey contains an underscore, return the empty string, else NULL. Although, this is somewhat simplified. If PortionKey would have some leading spaces, you could actually get back some non-blank characters.

Since I don't know the business rules, I can't say whether this is correct or not. But I will have to admin that is seems nonsensical to me.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

MelissaMa-msft avatar image
0 Votes"
MelissaMa-msft answered

Hi @ahmedsalah-1628,

Glad that you already got your accepted answer.

Please also refer below example:

 create table Portion
 (PortionKey varchar(100))
    
 insert into Portion values
 ('sdg_dgf'),
 (' gfhjkghl_'),
 ('usjmtr_'),
 ('_sdhykt '),
 ('_qdrgj'),
 ('tgjkse')
    
 select * from Portion
    
 select case WHEN Po.PortionKey LIKE '%[_]%' THEN SUBSTRING(Po.PortionKey, LEN(LTRIM(RTRIM(Po.PortionKey)))+1,LEN(LTRIM(RTRIM(Po.PortionKey)))) end as modifiedportionkey
 from Portion Po

105205-1.png

Per my understanding, someone would like to check whether there is an underscore in the PortionKey. If yes, it would return the remaining part after filtering out the left part of PortionKey whose length is the length of non-blank characters. If no, then return NULL.

But as recommended by other expert, you would be better to check more details about this condition with some one who wrote it.

Best regards,
Melissa


If the answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


1.png (3.7 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.