Formatting Datetime in SSRS Expression - reporting-services

Part of my query is like so:
SELECT * FROM TableA
WHERE ColumnA >= DATEADD(DAY, - 30, GETDATE())
With the expression at the where clause above, you can pull a rolling 30 days data without having to supply values. Now users of the report want to see it represented like:
2nd April – 1st May
when the report is ran. Knowing that I have no parameters as the requirement is to not use parameters, how do I reference ">= DATEADD(DAY, - 30, GETDATE())" to reflect the start date and the end date in the report?

SSRS doesn't have built-in support for ordinal numbers (i.e. "1st" or "2nd" instead of "1" or "2"). This page contains custom code to add this functionality to your SSRS report; however it is slightly wrong. Here is a corrected version:
Public Function FormatOrdinal(ByVal day As Integer) as String
' Starts a select case based on the odd/even of num
if(day = 11 or day = 12 or day = 13)
' If the nymber is 11,12 or 13 .. we want to add a "th" NOT a "st", "nd" or "rd"
return day.ToString() + "th"
else
' Start a new select case for the rest of the numbers
Select Case day Mod 10
Case 1
' The number is either 1 or 21 .. add a "st"
Return day.ToString() + "st"
Case 2
' The number is either a 2 or 22 .. add a "nd"
Return day.ToString() + "nd"
Case 3
' The number is either a 3 or 33 .. add a "rd"
Return day.ToString() + "rd"
Case Else
' Otherwise for everything else add a "Th"
Return day.ToString() + "th"
End Select
end if
End Function
If you add this code to the code section of your report under report properties, your textbox expression would be:
Code.FormatOrdinal(Day(Globals!ExecutionTime)) & " " & MonthName(Month(Globals!ExecutionTime), False) & " - " & Code.FormatOrdinal(Day(DateAdd("d", -30,Globals!ExecutionTime))) & " " & MonthName(Month(DateAdd("d", -30,Globals!ExecutionTime)), False)


Right Click on the Textbox, Go To Textbox Properties then, Click on Number tab, click on custom format option then click on fx button in black.
Write just one line of code will do your work in simpler way:
A form will open, copy the below text and paste there to need to change following text with your database date field.
Fields!FieldName.Value, "Dataset"
Replace FieldName with your Date Field
Replace Dataset with your Dateset Name
="d" + switch(int(Day((Fields!FieldName.Value, "Dataset"))) mod 10=1,"'st'",int(Day((Fields!FieldName.Value, "Dataset"))) mod 10 = 2,"'nd'",int(Day((Fields!FieldName.Value, "Dataset"))) mod 10 = 3,"'rd'",true,"'th'") + " MMMM, yyyy"

Related

SSRS Textbox expression filter from Dataset

Trying to create a TextBox expression:
="Validity: " & IIF(Fields!ID.Value = 2, Fields!Value.Value, "") & " from date above."
from a dataset:
ID; NAME; VALUE;
1; Delivery; x Factory;
2; Validity; 30 days;
3; Pricing Structure; Subject to...;
so that the text box would read "Validity: 30 days from date above" but returns "Validity: from date above"
The problem is the report only allows me to use aggregate First, max, etc from the dataset producing an incorrect result.
"Validity: " & IIF(First(Fields!ID.Value, "DataSet") = 1, First(Fields!Value.Value, ), "") & " from date above."
"Validity: x Factory from date above"
Your dataset is showing "30 days", do you require the text box to show this or do you require it to be "60 days"?
Meanwhile if you restrict you dataset to one row of data, ie insert a where/having clause such as : HAVING (ID = 2), then you could use the aggregate sum function in your expression:
="Validity: " & IIF(Sum(Fields!ID.Value, "DataSet1") = 2, Fields!Value.Value, "") & " from date above."

Set field format in query to report

Is there a way for a report's field to take into account the format of a field in a query?
In example:
I have a StudentPercent field in a query. Values of the field are between 0 to 1, but since it is formatted to percent, they appear from 0% to 100% . When I run the report, it doesn't consider the format of the field and the values are between 0 to 1. Why is that?
Edit 1: I'm using Microsoft Access 2016.
Also, datas are populated dynamically, so I can't just set the format of the fields manually.
Edit 2:
Private Sub Detail_Format(Cancel As Integer, FormatCount As Integer)
'Exit Sub
' Place values in text boxes and hide unused text boxes.
Dim intX As Integer
' Verify that not at end of recordset.
If Not rstReport.EOF Then
' If FormatCount is 1, place values from recordset into text boxes
' in detail section.
If Me.FormatCount = 1 Then
Me("Col" + Format(intColumnCount + 1)) = 0
For intX = 1 To intColumnCount
' Convert Null values to 0.
Me("Col" + Format(intX)) = Nz(rstReport(intX - 1))
If intX < intColumnCount Then
Me("Col" + Format(intColumnCount + 1)) = _
Me("Col" + Format(intColumnCount + 1)) + Nz(rstReport(intX))
End If
Next intX
' Hide unused text boxes in detail section.
'For intX = intColumnCount + 2 To conTotalColumns
'Me("Col" + Format(intX)).Visible = False
'Next intX
For intX = 2 To intColumnCount + 1
Me("Tot" + Format(intX)) = Nz(Me("Tot" + Format(intX))) + Nz(Me("Col" + Format(intX)))
Next intX
' Move to next record in recordset.
rstReport.MoveNext
End If
End If
End Sub
^ is the code of the detail part of my report.
I'm getting the error '13' - Type mismatch when I run the report after casting my field with Format(FieldName, "Percent") and the following code is highlighted:
Me("Col" + Format(intColumnCount + 1)) = _
Me("Col" + Format(intColumnCount + 1)) + Nz(rstReport(intX))
Set the Format property of the textbox in the report to: Percent
Or, expand the source query to have a field returning the formatted value as text:
StudentPercentText: Format([StudentPercent],"Percent")
Then use this field in your report and not the StudentPercent field. However, this is text, so you cannot use such a field in a calculation in the report.

Mysql CASE with multiple substring legths

I have hundreds of phone number of the world. Each has its country prefix (the prefix varies: some are 1, 2, 3 or 4 digit long) + the phone number. I want to write a mysql query, which will show me the Country name by using the prefix.
Example : If i use sub-string for the first 3 digits, its working fine. But how i can show the prefixes which are 2 or 4 digit long ?
SELECT(
CASE (SUBSTR(Number,1,3))
WHEN '998' Then 'Uzbekistan '
WHEN '996' Then 'Kyrgyzstan '
WHEN '995' Then 'Georgia '
.....
....
ELSE 'OTHERS' END ) AS Country
A simple solution is based on the fact that the CASE statement is evaluated sequentially.
SELECT(
CASE
WHEN SUBSTR(Number,1,2) = '23' Then 'Try For 23 '
WHEN SUBSTR(Number,1,3) = '998' Then 'Uzbekistan '
WHEN SUBSTR(Number,1,3) = '996' Then 'Kyrgyzstan '
WHEN SUBSTR(Number,1,3) = '995' Then 'Georgia '
.....
....
ELSE 'OTHERS' END ) AS Country

How do I create a query which displays dots (....) after a certain number of characters within the field

I would like to create a query on a field which after a certain number of characters adds/displays a number of dots to show the user that there is additional text to read. At the moment there is a syntax error using the following code in which it doesn't like the "Left" instruction:
X:IIF(len(description) > 5, Left(description, 5) & "....", description)
Note: "X" is what i am naming the field 'description' in my query screen in Access
You can start with Len() to determine the length of the string value stored in your field. Here is an Immediate window example which uses a variable as a stand-in for the field value:
my_field = "0123456789" ' 10 characters
? Len(my_field)
10
my_field = "012345678901234" ' 15 characters
? Len(my_field)
15
Then you can use that length in an IIf() expression. If the length of the string is greater than your cut-off number (14 in the following example), use Left() to retrieve a substring and append " ..." to that substring. Otherwise return the entire string.
' my_field is still 012345678901234 (15 characters) at this point ...
? IIf(Len(my_field) > 14, Left(my_field, 10) & " ...", my_field)
0123456789 ...
' use the same expression with the shorter string ...
my_field = "0123456789" ' 10 characters
? IIf(Len(my_field) > 14, Left(my_field, 10) & " ...", my_field)
0123456789
' longest string which avoids the "..." is 14 characters ...
my_field = "01234567890123" ' 14 characters
? IIf(Len(my_field) > 14, Left(my_field, 10) & " ...", my_field)
01234567890123
You can use that approach in your query where my_field is actually the name of a text field in your data source.
The exact syntax for this problem is:
X:IIF(len(description) > 5; Left(description; 5) & "...."; description)
Thank you Hans for leading me in the right direction

Questions on SQL Server 2008 Full-Text Search

I have some questions about SQL 2K8 integrated full-text search.
Say I have the following tables:
Car with columns: id (int - pk), makeid (fk), description (nvarchar), year (int), features (int - bitwise value - 32 features only)
CarMake with columns: id (int - pk), mfgname (nvarchar)
CarFeatures with columns: id (int - 1, 2, 4, 8, etc.), featurename (nvarchar)
If someone searches "red honda civic 2002 4 doors", how would I parse the input string so that I could also search in the "CarMake" and "CarFeatures" tables?
Trying to parse search criteria like that will be a pain. A possible alternate solution would be to create a view that creates a long description of the car and create a full text index on that. So that view might look like:
Create View dbo.CarData
WITH SCHEMABINDING
As
Select dbo.Cars.Id
, dbo.CarMake.Manufactuer
+ ' ' + dbo.Cars.[Year]
+ Coalesce(' ' + dbo.Cars.Description,'')
+ ' ' + Case When Features & 1 <> 0 Then (Select Name From dbo.CarFeature Where Id = 1) Else '' End
+ ' ' + Case When Features & 2 <> 0 Then (Select Name From dbo.CarFeature Where Id = 2) Else '' End
+ ' ' + Case When Features & 4 <> 0 Then (Select Name From dbo.CarFeature Where Id = 4) Else '' End
+ ' ' + Case When Features & 8 <> 0 Then (Select Name From dbo.CarFeature Where Id = 8) Else '' End
+ ' ' + Case When Features & 16 <> 0 Then (Select Name From dbo.CarFeature Where Id = 16) Else '' End As Description
From dbo.Cars
Join dbo.CarMake
On CarMake.Id = Cars.MakeId
With a fulltext index on that view, then you might be able to take your search criteria and do:
Select ...
From CarData
Where Contains(Description, Replace('red honda civic 2002 4 doors', ' ', ' AND '))
Now, this is far from perfect. For example, it will result in '...4 AND doors' and thus find car models in 2004 with 2 doors or 4WD and 2 doors. In addition, I did not see color in your schema so I'm not sure how that would get into the mix.
It would obviously be substantially simpler to force the user to break up the search criteria into its constituent pieces instead of trying to implement a Google-like search. Thus, you would restrict the user to selecting the color from a drop list, selecting the make from another drop list and so on. If you did this, then you wouldn't need the above mentioned View and could instead query against the columns in the tables.
Btw, the features column being a bitwise value makes searches more of a pain as you will need to do a bitwise AND operation on each value to determine if it has the feature in question. It would be better to break out the Feature to Car mapping into a separate table.