Formatting a string in SQL Server - sql

I have a report and it must display text from a table, and I am not allowed to just type the text in the report or this would be really easy.
I just want to bold one word in a SQL string, and have it show up bolded in the SSRS report. Is there a way to format strings in SQL Server?

No, you can not. SQL Server stores and manipulates data, but is not meant to format it, let alone trying to apply some kind of visual formatting.

Strings in SQL Server (or rather, text types, like CHAR, VARCHAR, NCHAR and NVARCHAR) are simply textual - they have no formatting and can't have formatting.
You will need to make the values bold in the report itself.

You can't change the font for the strings in SQL Server. Any formatting must should be done in the Report.

Related

Proper formatting of Excel sheets to avoid errors in SQL querying?

What do you avoid when creating and filling out a Excel spreadsheet of data for a SQL database (certain formats, characters, character length issues?)
2.Does it matter how dates are formatted?
VARCHAR or INTEGER errors you've seen?
Finally, what SQL or Python queries did you use to address errors you found that you might have shared for questions 1-3?
The easiest way would be, if you can import Database-EDI (e.g. Oracle SQL Developer) a TXT- or CSV-Excel-Export into our Database.
→ Depending on the database, different requirements must be observed.
The main focus is on the correct formatting with regard to the country settings (Excel & database):
Excel-Format-Date YYYY-M-DD HH24:MM / Databe-Timestamp YYYY-MM-DD HH24:MM:SS.FFFF
→ That would not work
In addition, make sure that Excel does not cut any numbers:
Excel-Format-Long-Number 89632150000 (orignal 896321512345 )
→ Excel automatically shortens the number in the standard settings.
The length of a text must not exceed the specified maximum length in the assigned column of the type (VARCHAR).
I think these would be the main points to look out for.

Downloading .csv from SSRS need to avoid scientific notation format

Have an issue where when a report is downloaded, a field that has over 15 digits is converted to scientific notation instead of displaying the full digits (i.e. 672000000000000 = 6.72×10 to the 14th degree). I know this is an excel feature, but is there any workaround for it on the SSRS end?
Thanks!
This is an Excel issue, not a Reporting Services one. The SSRS CSV export should contain the number as it exists in the report, e.g. 672000000000000
The default behaviour for Excel (which you are presumably using to open the CSV file) is to display values this large using scientific notation, i.e. "6.72E+14". This behaviour can't be controlled from within SSRS.
As an alternative you might consider using the Excel render method for the report, which might give you more control over the display of these numbers.
Excel won't mess with it if you convert the expression to a string within SSRS.
Go into the expression for the text box and type the following:
=Str(Fields!YourFieldHere.Value)

Access SQL: Format a calculation as a percentage (Numeric, not string)

I'm working in Access 2010's SQL editor and I'm performing a calculation that results in a ratio.
I would like to display this ratio as a percentage using the SQL syntax editor, but as a number, not a string. I'd like to be able to paste out/export to Excel and not have to convert text to numbers.
Let's say this is my calculation:
OriginCount/DestinationCount AS MatchRate
I used the FORMAT function to make it appear as a percentage, but the result appears as a string. (Which I think is how the FORMAT function is designed to work)
FORMAT(OriginCount/DestinationCount,'Percent') AS MatchRate
Question 1: Is this possible using the SQL syntax editor?
Question 2: How do I do it?
Thanks!
How will you be using this data? It is standard to leave it as a double, EG: 0.02354, and then simply change the format of any control displaying that field.
Users should not being seeing tables or queries without them being the recordsource of a form, so this shouldn't be a problem.
That way, when exporting to Excel/Wherever else, it will properly display as a decimal number, and when viewing in Access, it will display as a Percentage 2.35%
The result of the FORMAT function in access is always in string format, so that would be expected behavior.
Did you try the CONVERT function instead?
https://msdn.microsoft.com/en-us/library/ms187928.aspx

SQL Parse NVARCHAR Field

I am loading data from Excels into database on SQL Server 2008. There is one column which is in nvarchar data type. This field contains the data as
Text text text text text text text text text text.
(ABC-2010-4091, ABC-2011-0586, ABC-2011-0587, ABC-2011-0604)
Text text text text text text text text text text.
(ABC-2011-0562, ABC-2011-0570, ABC-2011-0575, ABC-2011-0588)
so its text with many sentences of this kind.
For each row I need to get the data ABC-####-####, respectivelly I only need the last part. So e.g. for ABC-2010-4091 I need to obtain 4091. This number I will need to join to other table. I guess it would be enough to get the last parts of the format ABC-####-####, then I should be able to handle the request.
So the example of given above, the result should be 4091, 0586, 0587, 0604, 0562, 0570, 0575, 0588 in the row instead of the whole nvarchar value field.
Is this possible somehow? The text in the nvarchar field differ, but the text format (ABC-####-####) I want to work with is still the same. Only the count of characters for the last part may vary so its not only 4 numbers, but could be 5 or more.
What is the best approach to get these data? Should I parse it in SSIS or on the SQL server side with SQL Query? And how?
I am aware this is though task. I appreciate every help or advice how to deal with this. I have not tried anything yet as I do not know where to start. I read articles about SQL parsing, but I want to ask for best approach to deal with this task.
Stackoverflow is about programming.
Sit down and start programming.
Ok, seriously. That is string parsing and the last part in brackets with multiple fields means no bulk import, it is not a standard CSV file.
Either you use SSIS in SQL Server and program the parsing there or.... you write a program for that.
String maniupation in SQL is the worst part of the language and I would avoid it.
So, yes, sit down and program a routine. Probable the fastest way.
If I understand correctly, "ABS-####-####" will be the value coming through in the column and the numeric part is variable in length.
If that is the case, maybe this will work.
Use a "Derived Column" transformation.
Lets say we call "ABC-####-####" = Column1
SUBSTRING("Column1",(FINDSTRING("Column1","-",2)+1),LEN(Column1)-(FINDSTRING("Column1","-",2)))
If I am not mistaken, that should give you the last # values in a new column no matter how long that value is.
HTH
I have worked this problem out with the following guides:
Split Multi Value Column into Multiple Records &
Remove Multiple Spaces with Only One Space

MS SQL Server 2008 Encoding

I have table in database with Lithuanian_100_CI_AS collation. Some rows has text fields with text, which contains random symbols instead of Lithuanian ones. Is it possible to change the encoding, that i would see the letters i need? Changing collation does totally nothing.
If you have got the data like this (manipulated) then you can not realy save it by changing the collation, but if you set the right collation this could help you to get the data written in a right way to your database (more relevant for the future)
No, the data is random.
You need to
use nvarchar to store this data correctly
ensure the client is using nvarchar for parameters
ensure all string constants have N in front (example: N'foobar')
The collation is not encoding: it only determins how strings and compared/sported, but determines the code page for non-unicode (unicode = nvarchar) columns
Note, the data types "text" and "ntext" are deprecated in SQL Server. Use the max types