Search code examples
sqljoinreplacecaseprefix

SQL remove text at the end and add a prefix


I'm new to SQL and don't know much of the keywords yet.

I have a case to generate a text where the data I pull in example below

Data I pulled is 10DAYS, 20DAYS, 30DAYS, and so on...

Now, I want to remove the DAYS at the end and just add a prefix of D at the beginning, resulting in the data:

D10, D20, D30...  

I'm using the Replace keyword but I still can't make it to work, hehe

Thanks in advance!


Solution

  • This uses REPLACE to remove the word DAYS from your field:

    SELECT 'D' + REPLACE(yourfield, 'DAYS', '') 
    FROM YourTable
    

    SQL Fiddle Demo

    You may need to concatenate your values differently depending on your RDBMS. Use CONCAT for MySql and || for Oracle for example.