DateDiff Result Set to HH:MM:SS in SSRS - reporting-services

I want a value returned from a DateDiff to format to HH:MM:SS.
Currently, I am using:
=DateDiff("S",Fields!StartDate.Value,Fields!EndDate.Value).
How do I modify the above expression?

=RIGHT("0" + CStr(DateDiff("S",Fields!StartDate.Value,Fields!EndDate.Value)/3600), 2) + ":" +
RIGHT("0" + CStr((DateDiff("S",Fields!StartDate.Value,Fields!EndDate.Value)/60) Mod 60), 2) + ":" +
RIGHT("0" + CStr(DateDiff("S",Fields!StartDate.Value,Fields!EndDate.Value) Mod 60), 2)

The fix:
=DATEDIFF("h",Fields!StartDate.Value,Fields!EndDate.Value)
& ":" & DATEDIFF("n",Fields!StartDate.Value,Fields!EndDate.Value) MOD 60
& ":" & DATEDIFF("s",Fields!StartDate.Value,Fields!EndDate.Value) MOD 60

Related

IIF in microsoft report builder

I need to insert an IIF into the following expression in micorsoft report builder, and I keep getting an error. I want it to say if >1,1,""
=Round((Fields!percent_excellent.Value + Fields!percent_good.Value) * 100,0) & "% (" & Round(((Fields!percent_excellent.Value + Fields!percent_good.Value) - Fields!peer_group.Value) * 100,0) & "%)"
I don't know what you tried but this should work:
=IIF(Round((Fields!percent_excellent.Value + Fields!percent_good.Value) * 100,0) > 1,
100,
Round((Fields!percent_excellent.Value + Fields!percent_good.Value) * 100,0)) &
"% (" & Round(((Fields!percent_excellent.Value + Fields!percent_good.Value) - Fields!peer_group.Value) * 100,0) & "%)"
I think you'd be better using FORMATPERCENT to format your number with a set number of decimal places and adding the percent sign.
=FORMATPERCENT(
IIF(
(Fields!percent_excellent.Value + Fields!percent_good.Value) * 100 > 1,
1,
(Fields!percent_excellent.Value + Fields!percent_good.Value) * 100
)
, 0) & " ("
FORMATPERCENT(((Fields!percent_excellent.Value + Fields!percent_good.Value) - Fields!peer_group.Value) * 100, 0) & ")"

SQL Query (Find Range On Dates Stored as Strings)

I am currently working with a database which the dates are stored as strings in the (DD/MM/YYYY) format.
I am currently attempting to find a range of dates i.e. (15/07/2017 - 26/07/2017) which will then populate a weekly list.
The issue I am having it because these values are stored as strings within SQL I am unable to select a range unless I specify the values.
I am wondering are there any SQL Wizards out there who can help me.
Thanks,
Joe
you can use between and str_to_date eg:
select *
from my_table
where str_to_date(my__date_column, '%d/%m/%Y')
between str_to_date('15/07/2017', '%d/%m/%Y')
and str_to_date('26/07/2017', '%d/%m/%Y')
Hello Everyone I amended my answer as the previous one was incorrect and unhelpful to other users. In short I had to change the Dates to be stored as Dates correctly within SQL. In short there was no short cut unfortunately.
This is a the SQL Query which i used (No SQL Injection Protected), I hope this is helpful
"SELECT * FROM placement WHERE ('" + StartDate.ToString("yyyy-MM-dd") + "'
between StartDate and EndDate OR '" + EndDate.ToString("yyyy-MM-dd") + "'
between StartDate and EndDate or StartDate = '" + StartDate.ToString("yyyy-
MM-dd") + "' or EndDate = '" + StartDate.ToString("yyyy-MM-dd") + "' or
StartDate = '" + EndDate.ToString("yyyy-MM-dd") + "' or EndDate = '" +
EndDate.ToString("yyyy-MM-dd") + "') AND YearGroup = '" +
User.IntakeYear.ToString() + "' AND Cohort = '" + User.Cohort.ToString() +
"';";
Thanks For Your Feedback.

Can't get the SQL query to work with variables b/c of syntax errors

String query = "insert into course_data values(null," + CourseName + ","
+ SCrsDesrpTemp + "," + CrsDes + "," + crsurl + ","
+ youtube + "," + sqlStrDate + "," + crsduration + ","
+ CrsImg + "," + "'Open2Study', 'Free', 'English', 'Yes'," + CrsImgUni + date + ")";
I keep getting syntax errors. The variable names are strings that hold values from scraped websites. I printed them out and they work fine, they all are of type string. But for some reason, I keep getting syntax error in the SQL query.
When presented to the database like this, string (and date) values need to be in single quotes.
String query = "insert into course_data values(null,'" + CourseName + "','"
+ SCrsDesrpTemp + "','" + CrsDes + "','" + crsurl + "','"
+ youtube + "','" + sqlStrDate + "','" + crsduration + "','"
+ CrsImg + "'," + "'Open2Study', 'Free', 'English', 'Yes','" + CrsImgUni + date + "')";
The last part may be incorrect "CrsImgUni + date" and you may need to ensure that dates are formatted correctly.
See also What is SQL injection?

Trying to insert date time into sql with variables in the query

String query = "insert into course_data
values (null, '"
+ CourseName + "','"
+ SCrsDesrpTemp + "','"
+ CrsDes + "','"
+ crsurl + "','"
+ youtube + "','"
+ sqlStrDate + "','"
+ crsduration + "','"
+ CrsImg + "','"
+ category + "',"
+ "'Open2Study',
'0.00',
'English',
'Yes','"
+ CrsImgUni + "','"
+ "GETDATE()" + "')";
That is my attempt above. I am trying to insert the current date and time into a date-time column but I keep getting syntax error for the query. It says GETDATE() is not the correct datatype for the column date-time.
Try this for Sql Server:-
ALTER TABLE course_data ADD CONSTRAINT
DF_MyTable_Inserted DEFAULT GETDATE() FOR crsduration
GO
This assumes your table is named course_data, the column is crsduration, and the name of the contstraint is to be DF_MyTable_Inserteddb in
If db in MySQL NOW() for get current date time
NOW()//Current date time
CURDATE()//Current date

convert yyyymmdd to dd MMM yyyy

Is it possible to convert date 4 digit year 2 digit month and 2 digit day to dd (3 digit month) 4 digit year?
Right now I have the input of date "use date" as user entered YYYYMMDD. I prefer to use the calendar input as it keeps the date consistent
A Date/Time value is actually a double precision float number.
So you can take a number, and use CDate to represent it as a date.
? CDate(41668.0)
1/29/2014
The display format of the date value is a separate issue. The same numeric date value can be displayed in whatever format you prefer.
? Format(CDate(41668.0), "yyyymmdd")
20140129
? Format(CDate(41668.0), "dd mmm yyyy")
29 Jan 2014
But the actual date value (the number) is unchanged --- that number doesn't get "converted" regardless of how it's displayed.
If your issue is that the users are working with a text value instead of a Date/Time value, you either have to convert that text to a valid Date/Time value or modify your application so they enter Date/Time values instead of text.
The second alternative is less fuss. But if you're stuck with dates as text, you can do something like this ...
use_date = "20140129"
' transform it to a string CDate can accept ...
? Left(use_date, 4) & "-" & Mid(use_date, 5, 2) & "-" & Right(use_date, 2)
2014-01-29
' get the date from that string ...
? CDate(Left(use_date, 4) & "-" & Mid(use_date, 5, 2) & "-" & Right(use_date, 2))
1/29/2014
' finally make it a string in your desired format ...
? Format(CDate(Left(use_date, 4) & "-" & Mid(use_date, 5, 2) & "-" & Right(use_date, 2)), "dd mmm yyyy")
29 Jan 2014
try
DIM DateStr : DateStr = "20140119" 'Your date
Response.Write "DEBUG: DateStr = " & DateStr & "<br>"
'Split number so can use Date functions
DIM NewDate : NewDate = DateSerial(CInt(Mid(DateStr, 1, 4)), CInt(Mid(DateStr, 5, 2)), Mid(DateStr, 7, 2))
Response.Write "DEBUG: NewDate = " & NewDate & "<br>"
TheDate=CDate(NewDate)
Response.Write "DEBUG: CDate(NewDate) = " & TheDate & "<br>"
DIM FinalDate: FinalDate = DatePart("d", TheDate) & " "
FinalDate = FinalDate & MonthName(Month(TheDate),1) & " "
FinalDate = FinalDate & DatePart("yyyy", TheDate)
Response.write "DEBUG: Required Date = " & FinalDate