Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get first or second values from a comma separated value in SQL

Tags:

sql

sql-server

I have a column that stores data like (42,12). Now I want to fetch 42 or 12 (two different select queries). I have searched and found some similar but much more complex scenarios. Is there any easy way of doing it? I am using MSSQL Server 2005.

Given there will always be only two values and they will be integer

like image 518
Tanmoy Avatar asked Feb 06 '26 06:02

Tanmoy


1 Answers

The reason you have this problem is because the database (which you may not have any control over), violates first normal form. Among other things, first normal form says that each column should hold a single value, not multiple values. This is bad design.

Now, having said this, the first solution that pops into my head is to write a UDF that parses the value in this column, based on the delimiter and returns either the first or second value.

like image 90
Randy Minder Avatar answered Feb 09 '26 02:02

Randy Minder