PARSENAME function in SQL Server

Problem Statement:- I have two columns one column contains a value with string as comma-separated like ‘James, Sawyer’ and another column contains like James. So get an output the third column as true if the value within the first column contains in the second column

Solution:-

— CASE TABLE FOR CHECKING SCENARIO NAMED check_n

create table check_n

(comb_name varchar(20),

name varchar(10)

);

— INSERT SOME ROWS INTO THE TABLE AS PER THE REQUIREMENT

insert into check_n values(‘James,Sawyer’,’James’);

insert into check_n values(‘James,Sawyer’,’Sawyer’);

insert into check_n values(‘James,Sawyer’,’Jamey’);

select * from check_n;

Result of the above query

select

comb_name , name ,

case

when name=x.first_name or name = x.second_name then ‘true’

else ‘false’

end as condition_check from (

select comb_name , name,

PARSENAME(REPLACE(comb_name, ‘,’, ‘.’), 1) AS first_name,

PARSENAME(REPLACE(comb_name, ‘,’, ‘.’), 2) AS second_name

from check_n) x

RESULT:-

Result of above-executed query

Database Engineer having working experience in SQL and NO-SQL databases. 👨‍💻

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Game Managing — Singleton Pattern and Unity Events

Converting a PHP web application to Heroku from a Shared Host (ex. Host Gator)

[EN] Why FireFly?

Stripe’s new API reference documentation

Quick Start: How could you build a messaging solution with .Net Core? — Part 4

Golang Packages

Day 5 of my Developer Journey: Simple Enemy behavior

Modbus for Industrial IoT

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Nidhi Gupta

Nidhi Gupta

Database Engineer having working experience in SQL and NO-SQL databases. 👨‍💻

More from Medium

Cinchoo ETL — Split a large JSON file based on deeply nested array property

Trying out DATE_ADD function in MySQL

Data migration in PostgreSQL

Journey of DLithe Bootcamp JAVA Full Stack Developer||My Presentation:(april23)