MySQL Format String with columns - mysql

I am currently working on our MySQL Database and am in need of help. Would be happy if you have a solution! :)
We are generating automatic invoices with text. The text is very long and contains customerinformation and other fields of the database.
Currently we are using a long concat function which is so long that you can't see through. I would like to have a function that takes a string with placeholders (like php sprintf function) with values and formats the text.
For example (pseudocode)
FILLTEXT("Customer %s has payed invoice %s on %s",customername,invoicenumber,invoicepaydate) as Invoice_text
(We are using a lot of columns and a much longer text, I just wanted to do a quick example).
I am very grateful for any help or advices!
Thanks

Related

SQLCMD changes "7 k" string to 7k or 7,000?

I have a SQL query that returns a value similar to "7 KI" for a description column, but when I export my data to a .csv/text file using SQLCMD, the resulting data is "7KI" (no space inbetween 7 and K). I believe that this causes a formatting issue when I open this csv file in Excel later on...(I'm wondering if "7K" is getting read as 7,000 because the data is splitting into a new column as-if it contained a comma there?)...
This is so weird to me. I am using the Quotename function to return the Unicode version of this description field in the first place - do you think that's why I am experiencing this issue? I used the Quotename function to fix issues with this field a few days ago. To me, it's odd that the export is stripping the space inbetween 7 and K in the first place.
Has anyone ever seen/fixed this before? It really looks like there is a space included in the SQL data, and I copied-and-pasted the value into a online Unicode converter to confirm there is a space. Why would a random space be getting stripped in the SQLCMD export? Plenty of my other text fields are not having this issue, even though they have spaces too (although not followed by "K"...) so maybe the issue is with CMD somehow reading the value...?

MS Access Update Query Truncates Text

I'm running an update query that involves Long Text (Memo) fields from a table:
UPDATE Core, Interpretation SET Core.SEIS_SS_Final = 434,
Core.SEIS_SS_Final_SH = [Interpretation].[Short_Hand],
Core.SEIS_SS_Final_Std = [Interpretation].[Student_Txt],
Core.SEIS_SS_Final_Adv = [Interpretation].[Advisor_Txt] WHERE
(Core.SID =[Forms]![Process_frm]![SID]) AND
(Interpretation.Interpretation_id=545);
The fields in the table Core are set to Long Text, but after the update, they are truncated to 256 characters (Short Text length).
I've checked the Interpretation table and the fields are okay in there (they are not truncated) so something is happening when I run the query.
The absolutely bizarre thing is that I've been running this query for six months and this is the first time it's happened. I ran it this morning and have the output, which is not truncated---but now suddenly it's truncating, and I can't think of what I could have done to make it break.
Thanks in advance for any suggestions or help

Convert Column Type to Long Text in Access

I am running a query in Access 2013.
SELECT short, long INTO newTbl
FROM tbl
My problem is that long is a 1000 characters and by default newTbl treats it as Short Text. I can edit the field in Design View and change the type to Long Text, but when I rerun the query, it switches back to Short Text. Is there something I can add to my query so it automatically becomes Long Text?
I ran into a similar issue today. The workaround I'm using is an aggregate function 'First'. For this purpose try something like:
SELECT tbl.short, First(tbl.long) AS FirstOflong INTO newTbl
FROM tbl GROUP BY tbl.short, tbl.long;

MySQL export to MS Excel : 1-9 becomes 9-Jan or 42013

I use Workbench to query database at work. We have a field which indicates company size and has the following options :
1-9
10-49
50-99
100-499
500+
When I export the results containing this field in Excel(which I use for analysis), 1-9 becomes 9-Jan, when I change the format of the cell to text, it becomes 42013. Similarly, 10-49 becomes Oct-50 and in text - 18537. Is there a way to avoid this?
I know this may seem trivial but I take a download of the results every couple of hours or so, and currently, I use the Replace function in Excel to fix this which is a time cost. Also, adding manual intervention increases the probability of error which I want to minimize. I would ideally like the result to export as 1-9, as it exists in the database, based on which the analytical model is built to take input.
I would appreciate any help or pointers on how to fix this issue.
Thanks!
You are not saying how you are bringing the data into Excel. The simplest method is to bring the column in as "text". You can do this when you are importing the data into Excel, by setting the column type to "text".
Alternatively, when you create the output file, you can prepend the value with a single quote or some other character:
select concat('''', company_size)
select concat('_', company_size)
Appreciate the help! I used to export the results in CSV which caused the problem I think. I exported them in XML and that solved it, the fields appear as they exist in the database. Thanks a lot. The concatenation would work as well!

How can I write a query with unwated spaces in my text?

I am currently improving the search functionality of my cms so that users can search for entries by copying and pasting text from a web page and finding it in the database.
The query is simple. It takes the search term and does a LIKE '% text here %' query.
The problem is, I'm not getting many results and have figured out why.
In the CMS itself, a lot of the text that has been entered from MS Word seems to be double spaced. Such as
"Hello my name is James"
However on the front end website it renders properly, with single spaces, like:
"Hello my name is James"
This means my query is never picking up the database entry based on what is shown on the web page.
Any suggestions? Do I tackle the double spaces in the CMS (seems risky to me with so much HTML in there!), or can I adjust my query to cope with it?
if it is only double spaces that are creating the issue, then just
replace(columnToSearch,' ',' ')
when searching, or as #ManMohan suggests, before inserting the data into your table in the first place
Take a look at MySQL REPLACE
REPLACE(str,from_str,to_str)
Returns the string str with all occurrences of the string from_str replaced by the string to_str.
REPLACE() performs a case-sensitive match when searching for from_str.