sql server - Updating a column based on first occurance of a text in another column -


i have requirement convert 150 char free form text , map 1 of 2 texts: sibling or spouse in member_type field in sql server database.

i came below update statement update:

update my_table set member_type = case       when (relationship_description 'brother%' or             relationship_description 'sister%' or             relationship_description  'sibling%'         'sibling'         when ( relationship_description 'spouse%' or              relationship_description 'husband%' or              relationship_description 'wife%' or        ) 'spouse'       else '' end; 

but there additional requirement: if there multiple key words relationship_description, convert using first key word.

for example: case 1: relationship_description = "mark's brother" : contains "brother" in treated sibling.

case2: relationship_description = "walter brother of mark , greg howard. mark husband of julie":first occurance brother treated sibling.

case3:relationship_description = "john's wife": contains keyword wife, should treated spouse

case 4: relationship_description = "johns's wife , peter's sister": contains 2 keywords wife , sister, should consider wife should treated spouse.

i came know there sql keyword stuff might work. can help?. need via sql script , not via java.

try pathindex function:

declare @t table(relationship_description varchar(max), member_type varchar(10)) insert @t values ('mark''s brother in', null), ('walter brother of mark , greg howard. mark husband of julie', null), ('john''s wife', null), ('johns''s wife , peter''s sister', null)  update t     set member_type = case when m = 0                             null else                                      case when ca.t = 1                                           'sibling'                                           else 'spouse'                                      end                        end @t t cross apply (select top 1 *              (values              (patindex('%brother%', relationship_description), 1),               (patindex('%sister%', relationship_description), 1),               (patindex('%sibling%', relationship_description), 1),               (patindex('%spouse%', relationship_description), 2),               (patindex('%husband%', relationship_description), 2),               (patindex('%wife%', relationship_description), 2)) t(m, t)              order row_number() over(order case when m = 0 1000000 else m end))ca  select * @t 

in cross apply finding out indexes of first occurrences of key words. picking minimal index different 0(0 means there no occurrence, mark 1000000). in main case verifying if there no occurrence of word select null else @ occurrence type , select appropriate description.

output:

relationship_description                                                  member_type mark's brother                                                            sibling walter brother of mark , greg howard. mark husband of julie   sibling john's wife                                                               spouse johns's wife , peter's sister                                           spouse 

Comments

Popular posts from this blog

Java 3D LWJGL collision -

spring - SubProtocolWebSocketHandler - No handlers -

methods - python can't use function in submodule -