# Simplified Case Statement using First letter of Artist Tag

**URL:** https://community.mp3tag.de/t/simplified-case-statement-using-first-letter-of-artist-tag/13737
**Category:** Support
**Created:** [August 8, 2012, 11:11pm UTC](https://community.mp3tag.de/t/simplified-case-statement-using-first-letter-of-artist-tag/13737 "2012-08-08T23:11:03Z")
**Posts on this page:** 12
**Page:** 1

<div class="post-metadata">

### Author: ![kittmaster](https://community.mp3tag.de/user_avatar/community.mp3tag.de/kittmaster/32/14793_2.png) [@kittmaster](https://community.mp3tag.de/u/kittmaster)
#### Post date: [August 8, 2012, 11:11pm UTC](https://community.mp3tag.de/t/simplified-case-statement-using-first-letter-of-artist-tag/13737/1 "2012-08-08T23:11:03Z")

</div>

I'm familiar with how case statements work, I'm looking for some help on how to select the proper case statement based solely on the artists first letter. This is being used for a MySQL database being imported via csv.

So it would be something like:

if (%artist% == 'A')

then SQL foo = SQL A directory

else

if (%artist% == 'B')

then SQL foo = SQL B directory

.

.

if (%artist% == 'Z')

then SQL foo = SQL Z directory

if (%artist% == '0-9')

then SQL foo = SQL NUMBER directory

This will be done in the export option that I have to build. I have full control of the csv aspect, I'm just trying to figure out how to do case selection or do I have to do a long winded if/else/elseif/elseif type thing

Can someone point me to a previous example or give me a short hand script of how this would look in the export routine.

Thanks!

Chris

---

<div class="post-metadata">

### Author: ![JJ\_Johnson](https://community.mp3tag.de/user_avatar/community.mp3tag.de/jj_johnson/32/67_2.png) [@JJ\_Johnson](https://community.mp3tag.de/u/JJ_Johnson)
#### Post date: [August 9, 2012, 12:51am UTC](https://community.mp3tag.de/t/simplified-case-statement-using-first-letter-of-artist-tag/13737/2 "2012-08-09T00:51:38Z")

</div>

You really just need an if/then/else construct, albeit a long winded one, as are most complex things in Mp3tag's scripting language.

```
$if($and($geql($left(%artist%,1),'0'),$leql($left(%artist%,1),'9')),SQL foo = SQL NUMBER directory,SQL foo = SQL $left(%artist%,1) directory)
```

---

<div class="post-metadata">

### Author: ![kittmaster](https://community.mp3tag.de/user_avatar/community.mp3tag.de/kittmaster/32/14793_2.png) [@kittmaster](https://community.mp3tag.de/u/kittmaster)
#### Post date: [August 9, 2012, 12:59am UTC](https://community.mp3tag.de/t/simplified-case-statement-using-first-letter-of-artist-tag/13737/3 "2012-08-09T00:59:52Z")

</div>

So I see it as if X is true then y else z.....got that......I don't understand the test logic, will that cover 0 through 9 or just 0 AND 9?

Also, how will this propagate to A through Z?

I realize I will have have 27 lines for each condition, I just want to ensure I understand the logic behind it.......

thanks for the fast answer to my first question.

Chris

---

<div class="post-metadata">

### Author: ![JJ\_Johnson](https://community.mp3tag.de/user_avatar/community.mp3tag.de/jj_johnson/32/67_2.png) [@JJ\_Johnson](https://community.mp3tag.de/u/JJ_Johnson)
#### Post date: [August 9, 2012, 1:20am UTC](https://community.mp3tag.de/t/simplified-case-statement-using-first-letter-of-artist-tag/13737/4 "2012-08-09T01:20:11Z")

</div>

Make sure you look at the Mp3tag help for scripting and the available functions.

[https://docs.mp3tag.de/scripting](https://docs.mp3tag.de/scripting)

Roughly translated:

```
if (first character is between 0 and 9) then
  return: SQL foo = SQL NUMBER directory
else
  return: SQL foo = SQL + (first character) + directory
endif

```

The 'between' is done with the $and, the $geql (greater than or equal) and the $leql (less than or equal) functions. If the character is greater than or equal 0 _AND_ it's less than or equal 9, then it's a digit.

The A through Z part is simply the ELSE part of the if then else, which inserts the first character into the SQL string.

---

<div class="post-metadata">

### Author: ![DetlevD](https://community.mp3tag.de/user_avatar/community.mp3tag.de/detlevd/32/123_2.png) [@DetlevD](https://community.mp3tag.de/u/DetlevD)
#### Post date: [August 9, 2012, 4:08am UTC](https://community.mp3tag.de/t/simplified-case-statement-using-first-letter-of-artist-tag/13737/5 "2012-08-09T04:08:42Z")

</div>

> [@kittmaster](#):
>
> I'm familiar with how case statements work, I'm looking for some help on how to select the proper case statement based solely on the artists first letter. This is being used for a MySQL database being imported via csv. ...  
> This will be done in the export option that I have to build.  
> I have full control of the csv aspect, I'm just trying to figure out how to do case selection or do I have to do a long winded if/else/elseif/elseif type thing  
> Can someone point me to a previous example or give me a short hand script of how this would look in the export routine. ...

It is not clear to me what you want to do.

Do you want to export MySQL data into CSV file?  
==\> See MySQL scripting language.  
[http://en.wikipedia.org/wiki/MySQL](http://en.wikipedia.org/wiki/MySQL)

Do you want to export tag-field data into CSV file?  
==\> See Mp3tag scripting language.  
[https://docs.mp3tag.de/scripting](https://docs.mp3tag.de/scripting)  
[https://docs.mp3tag.de/export](https://docs.mp3tag.de/export)

In Mp3tag scripting language the first letter of a tag-field can be extracted using the function $left().

Examples:

**$left(%ARTIST%,1)****$upper($left(%ARTIST%,1))****$if($isdigit($left(%ARTIST%,1)),'SQL NUMBER directory','SQL '$upper($left(%ARTIST%,1))' directory')****'SQL '$if($isdigit($left(%ARTIST%,1)),'NUMBER',$upper($left(%ARTIST%,1)))' directory'**

DD.20120809.0818.CEST

---

<div class="post-metadata">

### Author: ![kittmaster](https://community.mp3tag.de/user_avatar/community.mp3tag.de/kittmaster/32/14793_2.png) [@kittmaster](https://community.mp3tag.de/u/kittmaster)
#### Post date: [August 9, 2012, 10:21am UTC](https://community.mp3tag.de/t/simplified-case-statement-using-first-letter-of-artist-tag/13737/6 "2012-08-09T10:21:22Z")

</div>

Thanks to both of you, I'll give these a try understand the logic.

To answer DetlevD, yes, tags to CSV \> CSV into SQL via HTML CSV import utility.

You guys are the best.

Regards,

Chris

---

<div class="post-metadata">

### Author: ![DetlevD](https://community.mp3tag.de/user_avatar/community.mp3tag.de/detlevd/32/123_2.png) [@DetlevD](https://community.mp3tag.de/u/DetlevD)
#### Post date: [August 9, 2012, 10:43am UTC](https://community.mp3tag.de/t/simplified-case-statement-using-first-letter-of-artist-tag/13737/7 "2012-08-09T10:43:16Z")

</div>

> [@kittmaster](#):
>
> ... tags to CSV \> CSV into SQL ...

Regarding SQL see also ...  
[/t/10187/1](https://community.mp3tag.de/t/10187/1)  
[Export data to database?](https://community.mp3tag.de/t/9703/3)

DD.20120809.1443.CEST

---

<div class="post-metadata">

### Author: ![kittmaster](https://community.mp3tag.de/user_avatar/community.mp3tag.de/kittmaster/32/14793_2.png) [@kittmaster](https://community.mp3tag.de/u/kittmaster)
#### Post date: [August 9, 2012, 11:05am UTC](https://community.mp3tag.de/t/simplified-case-statement-using-first-letter-of-artist-tag/13737/8 "2012-08-09T11:05:53Z")

</div>

Thanks for the SQL info, I'll bookmark those for future use. My input at the core is csv import, but the HTML is bringing that into a MySQL database for web application. So I don't really need to have true SQL generation.

I'm using this for an online "wishlist" system for a backend. They can search by artist and then create an online playlist.

I'm looking at the code now, I understand how it works and again, it seems the simplistic logic just clouded my thought......easy when I saw the examples.

Thanks again.....🙂

Chris

---

<div class="post-metadata">

### Author: ![kittmaster](https://community.mp3tag.de/user_avatar/community.mp3tag.de/kittmaster/32/14793_2.png) [@kittmaster](https://community.mp3tag.de/u/kittmaster)
#### Post date: [August 9, 2012, 11:38am UTC](https://community.mp3tag.de/t/simplified-case-statement-using-first-letter-of-artist-tag/13737/9 "2012-08-09T11:38:34Z")

</div>

I'm bumping into syntax errors and I thought I had a good handle on it, not sure what I'm doing wrong. I've tried a few variants like:

```
($if($and($geql($left(%artist%,1),'0'),$leql($left(%artist%,1),'9'))),ABP Alphabetical Music List/NUMBERS, NULL)

($if($and($geql($left(%artist%,1),'0'),$leql($left(%artist%,1),'9'))),ABP Alphabetical Music List NUMBERS, NULL)

($if($and($geql($left(%artist%,1),'0'),$leql($left(%artist%,1),'9'))),ABP Alphabetical Music List/NUMBERS,)

```

But the output keeps giving me:

invalid $if syntax

I want to do this for the numbers and then a-z, so I believe leaving the false part of the statement blank should leave the output alone and then test for the remaining 26 cases. I need the final output to wind up as \> ABP Alphabetical Music List/NUMBERS or ABP Alphabetical Music List/A or ABP Alphabetical Music List/B............etc

I thought it might need an escape char for the \ but even removing that isn't work. I'm not sure what the issue is. I'm sure its something stupid on my end. Thoughts?

Thanks

---

<div class="post-metadata">

### Author: ![DetlevD](https://community.mp3tag.de/user_avatar/community.mp3tag.de/detlevd/32/123_2.png) [@DetlevD](https://community.mp3tag.de/u/DetlevD)
#### Post date: [August 9, 2012, 12:25pm UTC](https://community.mp3tag.de/t/simplified-case-statement-using-first-letter-of-artist-tag/13737/10 "2012-08-09T12:25:21Z")

</div>

> [@kittmaster](#):
>
> I'm bumping into syntax errors ... I want to do this for the numbers and then a-z ...

It is not clear to me what you want to achieve.  
Do you want to use Mp3tag to create a report file, hm ... maybe yes, and the output lines should be sorted by groups, depending on the first letter of the artist name?

So you have to concentrate your work to develop a **$loop() ... $loopend()** construct, which fits to your need.

Example:

**1: $filename($getEnv('USERPROFILE')'\Desktop\Mp3tag.Report.ByFirstNumberOrLetter.txt',UTF-8)** 

**2: $loop($regexp($regexp($if2(%ARTIST%,%ARTISTSORT%),'\[1\]_(.)._$','\U$1',1),'\d','0-9'),1)  
3: '['$regexp($regexp($if2(%ARTIST%,%ARTISTSORT%),'\[2\]_(.)._$','\U$1',1),'\d','0-9')']'  
4: $loop($if2(%ARTIST%,%ARTISTSORT%),1)$if2(%ARTIST%,%ARTISTSORT%)  
5: $loopend()$loopend()**

 

Doing another approach you can create a temporary tag-field within each media file, which holds the sort criterium for the report loop, and what can be removed afterwards if needed.

 

Action: Format value  
Field: TMP\_SORT  
Format string: **$if($isdigit($left(%ARTIST%,1)),'0',$upper($left(%ARTIST%,1)))**

 

The export loop expression can be rather simple like ...

 **1: $loop(%TMP\_SORT%)%ARTIST%** 

**2: $loopend()**

 

DD.20120809.1630.CEST

 
* * *
 
1. \W 

2. \W

---

<div class="post-metadata">

### Author: ![kittmaster](https://community.mp3tag.de/user_avatar/community.mp3tag.de/kittmaster/32/14793_2.png) [@kittmaster](https://community.mp3tag.de/u/kittmaster)
#### Post date: [August 9, 2012, 12:32pm UTC](https://community.mp3tag.de/t/simplified-case-statement-using-first-letter-of-artist-tag/13737/11 "2012-08-09T12:32:01Z")

</div>

I was able to use

```
$if($isdigit($left(%ARTIST%,1)), ABP Alphabetical Music List/NUMBERS , ABP Alphabetical Music List/$upper($left(%ARTIST%,1)))

```

to achieve my goal for generating the tag to output. My CSV is now working and I can import into my web app.

Thanks for your help. Your solution is simple and brilliant.....🙂

Chris

---

<div class="post-metadata">

### Author: ![system](https://community.mp3tag.de/uploads/default/original/2X/c/ce7035d426cb755a7916793326d23b465222a407.png) [@system](https://community.mp3tag.de/u/system)
#### Post date: [February 9, 2026, 12:23pm UTC](https://community.mp3tag.de/t/simplified-case-statement-using-first-letter-of-artist-tag/13737/12 "2026-02-09T12:23:59Z")

</div>


