I have a scheduled job which runs at 12:45:00 AM. Now through SP I want to get this time. I am running this query in my SP:
EXEC msdb.dbo.sp_help_job
#job_name = N'Daily Trends',
#job_aspect = N'SCHEDULES' ;
This query shows result with a column
active_start_time
4500
Expected Output: 12:45:00 AM.
Can you please suggest how to show the time with Am/PM.
Edit: I am calling this SP from c# code. If anybody can suggest how i can convert the time in proper format in c# code will also be helpful.
As the time value seems to be stored as HHMMSS with the hour (HH) optional I think you need to check if the length of the string is four chars (or maybe if the value exceeds 115959, whichever is faster). A query like this should work:
SELECT CASE
WHEN Len(active_start_time) = 4 THEN Cast(
Dateadd(minute, active_start_time /
100, '00:00') AS TIME)
ELSE LEFT(RIGHT('0' + Cast(active_start_time AS VARCHAR), 6), 2)
+ ':'
+ Substring(RIGHT('0' + Cast(active_start_time AS VARCHAR), 6), 3,
2)
+ ':'
+ RIGHT(Cast(active_start_time AS VARCHAR), 2)
END AS [Start time]
FROM msdb..sysschedules
INNER JOIN msdb.dbo.sysjobschedules
ON msdb.dbo.sysjobschedules.schedule_id =
msdb..sysschedules.schedule_id
INNER JOIN msdb.dbo.sysjobs
ON msdb.dbo.sysjobs.job_id = msdb.dbo.sysjobschedules.job_id
WHERE msdb.dbo.sysjobs.name = 'Daily Trends'
I didn't test it with that many values but I believe it should work, or at least give you a hint on how to proceed. Accessing the msdb tables directly might be a bad idea and if I recall right, there are some views that give access to similar information and it could be better to use them.
You can use the sysjobschedules table however the date will be the same.
This blog should help parse the date/time:
http://blog.sqlauthority.com/2008/12/22/sql-server-find-next-running-time-of-scheduled-job-using-t-sql/
so, to get the data to look like a HH:MM:SS format, the following SQL would work:
SELECT LEFT(RIGHT('0' + Cast(active_start_time AS VARCHAR), 6), 2)
+ ':'
+ Substring(RIGHT('0' + Cast(active_start_time AS VARCHAR), 6), 3, 2)
+ ':'
+ RIGHT(Cast(active_start_time AS VARCHAR), 2)
FROM msdb..sysschedules
Related
I am trying to check two interval of time, the first is know in the code as two DATETIME instances Start_Time and End_Time , and the second interval i retrieve from the database, How i can check for Minutes in case the Hours are equal, here is what i am trying to write
Select * FROM MY_TABLE
WHERE " ( "+ Start_Time + "<= IF(HOUR(DB_End_Time)=0, 24, HOUR(DB_End_Time) ) AND" +
" IF(HOUR(DB_Start_Time)=0, 24, HOUR(DB_Start_Time) ) <= " + End_Time + " ) "+
" OR "+
" ( "+ Start_Time +"<= IF(HOUR(DB_Start_Time)=0, 24, HOUR(DB_Start_Time))AND " +
" IF(HOUR(DB_End_Time)=0, 24, HOUR(DB_End_Time) ) <= " + End_Time+" ) ;"+
I tried things like Between and >= <= for the datetime it self , but it keep getting me an error and ask to check sql version!
This works fine with hours, any one knows how to include IF statement or something to get it work fine with minutes as well !?
If you have actual datetimes to work with, I think you are making this too complicated by trying to isolate hours, minutes, seconds, etc.
Try something of this nature:
select *
from MY_TABLE
where #Start_Time < DB_End_Time
and #End_Time > DB_Start_Time
This will check for any overlap, but not for consecutive intervals. As in, I don't think 10-11 and 11-12 overlap, but you can change < ... > to <= ... >= to account for that if you wish.
Also, as has been mentioned, please use a parameterized query from your app rather than your concatenated string which is vulnerable to sql injection attacks.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
Getting this code error when executing the following code.. Help would be appreciated
DECLARE #Last_Timestamp DATETIME,
#New_Timestamp DATETIME
SELECT DISTINCT Rtrim(of_displayname) + ' ('
+ Rtrim(ro_officer) + ')',
CONVERT(DATETIME, CONVERT(CHAR(11), ro_shift_start, 106), 106),
Rtrim(ac_name) + ' (' + Rtrim(ac_pin) + ')',
ro_officer
FROM roster WITH(nolock),
template WITH(nolock),
activity WITH(nolock),
officer WITH(nolock)
WHERE ro_status = 'INFO'
AND ro_activity IN ( 'LEAVE', 'SAL LEAVE' )
AND ro_timestamp > #Last_Timestamp
AND ro_timestamp <= #New_Timestamp
AND ro_shift_start > Getdate()
AND ac_pin = to_activity
AND to_pin = ro_officer
AND Unicode(to_type) = Unicode('M')
AND ro_officer = of_pin
--AND (Of_Payroll_No IN ('w','s')
AND ac_product_code IN ( '01', '09' ) /* Corporate Only */ /* W & S */
UNION
SELECT [officer],
[activity],
[status],
[comment]
FROM production.rolecall.[dbo].[holidayrequest]
ORDER BY Rtrim(of_displayname) + ' ('
+ Rtrim(ro_officer) + ')',RTRIM(Of_Displayname) + ' (' + RTRIM(Ro_Officer) + ')',CONVERT(DATETIME,CONVERT(CHAR(11), RO_Shift_Start,106),106)
There are 3 places where you are doing date conversions here:
CONVERT(DATETIME, CONVERT(CHAR(11), ro_shift_start, 106), 106)
CONVERT(DATETIME,CONVERT(CHAR(11), RO_Shift_Start,106),106)
the UINON
The first two look like you are actually trying to get the day without the time. If so: get the day without the time; there are various ways of doing this, but in recent versions of sql server, casting it to a date would work fine:
select cast(columnname as date) as [columnname]
or if you really need it as datetime:
select cast(cast(columnname as date) as datetime) as [columnname]
On other database versions you can use the sneeky "cast it as a float" approach:
select cast(floor(cast(columnname as float)) as datetime) as [columnname]
The UNION is more interesting; it looks like these are very different shapes - in which case: why are you unioning them? I very much doubt that [activity] is a datetime, yet that is the column it will become part of. It is entirely possible that this error is coming from trying to convert [activity] to a datetime.
I'm trying to display in a graph (Winforms/C#) the total amount from one column vs unit of time (in this case month) - so it would be a amount vs time graph. The problem is that the user would like the freedom of lets say - choosing the totals for January and June and compare them in a single graph (so the total for the month of January would be represented as a bar next to June's total's bar). I already capture the selected months (also, I have the graph control on the for) within a list but where I am really stuck is to build the mysql statement and its something like this
selectdataforGraph = "SELECT SUM(Amount_Net) AS Total FROM testingproject.incomeinformation WHERE date";
foreach (int month in selectedMonth) {
selectdataforGraph += "between '" + selected_year+ "-" + month +
"'-1 AND '" + selected_year + "-"+month+ "-31' AND";
}
I know it has some space missing and some quotation mark problems - already ran the query and I figured as much but I don't think the in-between would work because I don't know how to AND the next part of it so if a user picks May then August would be between 2007-5-01 and 2007-5-30 AND 2007-8-01 and 2007-8-30???
EDIT: didn't seem MySQL was your DB...
Definitely use a parameterized query! However... to fit in with what you have and so you can test it quickly...
I think I would use DATEPART rather than BETWEEN....
var selectdataforGraph = "SELECT SUM(Amount_Net) AS Total FROM testingproject.incomeinformation WHERE ";
var monthList = string.Join(",", selectedMonth);
selectdataforGraph += " YEAR(date) = " + selected_year;
selectdataforGraph += " AND MONTH(date) in (" + monthList + ")";
I have an SQL query :
SELECT DATEDIFF(deadline,CURDATE()) FROM tasks WHERE 1
The Result is : 3
How can I return the result as : 3 Days instead of 3
I know that I can manually append the string from my C# code something like :
string result = getSqlresult();
string result += " Days";
But I want to get the result directly as 3 Days from MySQL database.
The reason :
I'm binding information directly to datagridview and therefore, In order to modify the result i need to iterate through all rows and update them. So to increase performance, I need to get the result directly from database as 3 Days instead of 3
Anyhelp would be highly appreciated
you can concatenate the string Days into the result of DATEDIFF using CONCAT.
SELECT CONCAT(DATEDIFF(deadline,CURDATE()), ' Days')
FROM tasks
WHERE 1
if you are using old versions of MySQL, convert it to string so you will not get bolb result.
SELECT CONCAT(CAST(DATEDIFF(deadline,CURDATE()) AS CHAR(5)), ' Days')
FROM tasks
WHERE 1
UPDATE 1
SELECT CASE
WHEN DATEDIFF(deadline,CURDATE()) >= 0
THEN CONCAT(DATEDIFF(deadline,CURDATE()), ' Days')
ELSE CONCAT('Expired since ', DATEDIFF(deadline,CURDATE()) * -1, ' Days')
END
FROM tasks
SQLFiddle Demo
I want to retrieve records which the users are active n last modified date is greater than lastrun date, i wrote the following CAML but doesnt seem like working. Any help is much appreciated.
camlQuery.ViewXml
= "<View><Query><Where><And><Eq><FieldRef Name='Active'/>"
+ "<Value Type='Boolean'> " + 1 + "</Value></Eq><Gt>"
+ "<FieldRef Name='_DCDateModified'/><Value Type='DateTime'>"
+ lastUpdate + "</Value></Gt></And></Where></Query></View>";
I suppose it could be a Date formatting issue. You could attempt to:
1) explicit exclude of Time portion of DateTime
2) convert in advance your Date
like this:
...<Value IncludeTimeValue='False' Type='DateTime'>" + SPUtility.CreateISO8601DateTimeFromSystemDateTime(lastUpdate) +"</Value>