I have a string in mysql like this - "my_report_id". I would like to remove all "_"s and make all first letters in every word capital. The final result should be - "MyReportId". Is it possible to achieve this in mysql without using functions/procs? I don't want to use functions/procs, because of several reasons:
- I don't have the permissions to create them;
- The query I am building should be used in a lot of databases and I don't want to create the same function in every DB on which I am going to run the query;
I already saw a lot of solutions to my problem with the same function on several places over the internet, but as I already wrote I don't need functions.
MY TASK: Basically what I am trying to achive is to parse mysql tables to C# model classes for ASP.NET MVC. I need to create this "parser" in mysql and I found this solution (the second answer with provided code) very useful - Create C# classes based of MySQL table I started from the code from the link and updated it to parse nullable and unsigned types, too. I did something like this:
SELECT CONCAT('[Table(Name = "myTableName")]') UNION
SELECT CONCAT('public class ', "myTableName",'{') UNION
SELECT CONCAT('[Column(Name = "', COLUMN_NAME, '")] \n public ',
CASE IS_NULLABLE
WHEN 'NO' THEN
CASE WHEN COLUMN_TYPE LIKE '%unsigned%' THEN myTypeNonNullableUnsigned.cSharpType
ELSE myTypeNonNullable.cSharpType
END
WHEN 'YES' THEN
CASE WHEN COLUMN_TYPE LIKE '%unsigned%' THEN myTypeNullableUnsigned.cSharpType
ELSE myTypeNullable.cSharpType
END
END, ' ',
CONCAT(UCASE(LEFT(COLUMN_NAME, 1)), SUBSTRING(COLUMN_NAME, 2)),
' {get;set;}')
FROM INFORMATION_SCHEMA.COLUMNS c
JOIN(
SELECT 'char' AS sqlType ,'string' AS cSharpType UNION ALL
SELECT 'varchar', 'string' UNION ALL
SELECT 'datetime', 'DateTime' UNION ALL
SELECT 'text', 'string' UNION ALL
SELECT 'tinyint', 'bool' UNION ALL
SELECT 'int', 'int' UNION ALL
SELECT 'longtext', 'string'
) myTypeNonNullable ON c.DATA_TYPE LIKE myTypeNonNullable.sqlType
JOIN(
SELECT 'char' AS sqlType ,'string' AS cSharpType UNION ALL
SELECT 'varchar', 'string' UNION ALL
SELECT 'datetime', 'DateTime?' UNION ALL
SELECT 'text', 'string' UNION ALL
SELECT 'tinyint', 'bool?' UNION ALL
SELECT 'int', 'int?' UNION ALL
SELECT 'longtext', 'string'
) myTypeNullable ON c.DATA_TYPE LIKE myTypeNullable.sqlType
JOIN(
SELECT 'char' AS sqlType ,'string' AS cSharpType UNION ALL
SELECT 'varchar', 'string' UNION ALL
SELECT 'datetime', 'DateTime' UNION ALL
SELECT 'text', 'string' UNION ALL
SELECT 'tinyint', 'bool' UNION ALL
SELECT 'int', 'uint' UNION ALL
SELECT 'longtext', 'string'
) myTypeNonNullableUnsigned ON c.DATA_TYPE LIKE myTypeNonNullableUnsigned.sqlType
JOIN(
SELECT 'char' AS sqlType ,'string' AS cSharpType UNION ALL
SELECT 'varchar', 'string' UNION ALL
SELECT 'datetime', 'DateTime' UNION ALL
SELECT 'text', 'string' UNION ALL
SELECT 'tinyint', 'bool' UNION ALL
SELECT 'int', 'uint?' UNION ALL
SELECT 'longtext', 'string'
) myTypeNullableUnsigned ON c.DATA_TYPE LIKE myTypeNullableUnsigned.sqlType
WHERE TABLE_SCHEMA='myTableSchema' AND TABLE_NAME='myTableName'
UNION
SELECT '}';
The only thing left is to make the names of the properties to c# naming convention.
Maybe it's not very pretty, but for the time I have is the best solution I could come to. Any help will be greatly appreciated.