Related
need your expertise on the "Conversion failed when converting date and/or time from character string". Much written about it and tried looking though it and understanding it to the extend I could but it did not solve the issue.
My code includes a number on conversations from dates and strings to the common numerical format. The conversation work separately form the main code and even together when the code is loose. However, when I start adding more conditions in the "where" function, SQL doesn't seem to like it. Do you possible know how to handle it best? Any help is much appreciated.
SELECT DISTINCT
DOTWHL as "DC",
DORELD as "Release date",
DODLDT as "Prelim ETA",
DOITNO as "Item #",
MMITDS as "Item name",
MMSTCN as "Lkr",
DOPPQT as "Plan Qty",
DOPPQT/MMCFI2 as "Plan Qty, pall",
DOPSTS as "Sts",
SUM(MHFOQT) as "FC between ETA & BBD",
MLSTQT as "Stock, LHU",
MMCFI2 as "LHU/PAL",
MMCFI3 as "LHU/Layer",
CASE WHEN DOPPQT >= MLSTQT
THEN CASE
WHEN MLSTQT > SUM(MHFOQT)
THEN 'YES'
ELSE 'NO'
END
ELSE CASE
WHEN DOPPQT > SUM(MHFOQT)
THEN 'YES'
ELSE 'NO'
END
END AS "FEFO violation necessary?",
CASE WHEN DOPPQT >= MLSTQT
THEN CASE
WHEN MLSTQT > SUM(MHFOQT)
THEN SUM(MHFOQT)
ELSE MLSTQT
END
ELSE CASE
WHEN DOPPQT > SUM(MHFOQT)
THEN SUM(MHFOQT)
ELSE DOPPQT
END
END AS "Recommended Qty, LHU",
CASE WHEN DOPPQT >= MLSTQT
THEN CASE
WHEN MLSTQT > SUM(MHFOQT)
THEN SUM(MHFOQT)/MMCFI2
ELSE MLSTQT/MMCFI2
END
ELSE CASE
WHEN DOPPQT > SUM(MHFOQT)
THEN SUM(MHFOQT)/MMCFI2
ELSE DOPPQT/MMCFI2
END
END AS "Recommended Qty, pall",
MLBREF as "Batch",
CASE WHEN MLSTQT > DOPPQT
THEN CASE
WHEN DOPPQT < SUM(MHFOQT)
THEN SUM(MHFOQT)-DOPPQT
ELSE NULL
END
ELSE NULL
END AS "Fill Up possibility, LHU",
Sales.Sold_Qty as "Sold Qty 10 weeks",
Sales.Forecasted_Qty as "FC Qty 10 weeks",
ISNULL(Sales.Sold_Qty/NULLIF(Sales.Forecasted_Qty,0), 0) as "Sold/FC, %",
MBBUYE as "NP",
MMRESP as "DmP",
CASE
WHEN Other_Assortments.item <> 0 THEN 'Many Markets'
ELSE 'DC 091 only'
END as "Distributed on"
FROM MVXJDTA.MDOPLP
LEFT JOIN MVXJDTA.MITMAS
ON DOCONO=MMCONO AND DOITNO=MMITNO
LEFT JOIN MVXJDTA.MITBAL
ON DOCONO=MBCONO AND DOFWHL=MBWHLO AND DOITNO=MBITNO
LEFT JOIN MVXJDTA.MITSTA
ON DOCONO=MHCONO AND DOTWHL=MHWHLO AND DOITNO=MHITNO
LEFT JOIN MVXJDTA.MITLOC
ON DOCONO=MLCONO AND DOFWHL=MLWHLO AND DOITNO=MLITNO
LEFT JOIN
(
SELECT DISTINCT
MHITNO as "Item_#",
sum(MHSOQT) as "Sold_Qty",
sum(MHFOQT) as "Forecasted_Qty"
FROM MVXJDTA.MITSTA
WHERE MHCONO=1
AND MHWHLO='091'
AND MHCYP6 between
(cast(datepart(yyyy, dateadd(ww,-10,getdate())) as varchar) + right('00' + cast(datepart(ww, dateadd(ww,-10,getdate())) as varchar),2)) and
(cast(datepart(yyyy, dateadd(ww,-1,getdate())) as varchar) + right('00' + cast(datepart(ww, dateadd(ww,-1,getdate())) as varchar),2))
AND right(MHCYP6, 2) <>'00'
group by MHITNO
)
as Sales
on DOITNO=Sales.Item_#
LEFT JOIN
(
select distinct
OIITNO as "item"
from MVXJDTA.OCUSMA
left join MVXJDTA.OASCUS
on OKCONO=OCCONO and OKCUNO=OCCUNO
left join MVXJDTA.OASITN
on OKCONO=OICONO and OCASCD=OIASCD
where OKCONO=1
and OITDAT='99999999'
and OKCSCD<>'KR'
and OKCSCD<>'HK'
and OKCSCD<>'TW'
and OKCSCD<>'SG'
and OKCSCD<>'MY'
and OKCSCD<>'TH'
and OKCSCD<>'PH'
group by OIITNO) as Other_Assortments
on Other_Assortments.item=DOITNO
WHERE DOCONO=1
AND DOTWHL='091'
AND DOFWHL='010'
AND MHCYP6 NOT IN (
SELECT
MHCYP6
FROM MVXJDTA.MITSTA
WHERE MHWHLO='091'
AND MHCONO=1
AND RIGHT(CONVERT(VARCHAR, MHCYP6, 102), 2) ='00'
)
AND MHCYP6 BETWEEN
CASE
WHEN DATEPART(WEEK, CONVERT(DATETIME, RIGHT(DODLDT, 2) + '/' + LEFT(RIGHT(DODLDT, 4), 2) + '/' + LEFT (DODLDT, 4), 103))<10
THEN CONCAT( DATEPART(YEAR, CONVERT(DATETIME, RIGHT(DODLDT, 2) + '/' + LEFT(RIGHT(DODLDT, 4), 2) + '/' + LEFT (DODLDT, 4), 103)), 0,
DATEPART(WEEK, CONVERT(DATETIME, RIGHT(DODLDT, 2) + '/' + LEFT(RIGHT(DODLDT, 4), 2) + '/' + LEFT (DODLDT, 4), 103)))
ELSE CONCAT( DATEPART(YEAR, CONVERT(DATETIME, RIGHT(DODLDT, 2) + '/' + LEFT(RIGHT(DODLDT, 4), 2) + '/' + LEFT (DODLDT, 4), 103)),
DATEPART(WEEK, CONVERT(DATETIME, RIGHT(DODLDT, 2) + '/' + LEFT(RIGHT(DODLDT, 4), 2) + '/' + LEFT (DODLDT, 4), 103)))
END
CASE
WHEN MLBREF IS NULL
THEN '999999'
ELSE CASE
WHEN DATEPART(WEEK, DATEADD(DAY, -82, CONVERT(DATETIME, RIGHT(MLBREF, 2) + '/' + LEFT(RIGHT(MLBREF, 4), 2) + '/' + '20'+LEFT (MLBREF, 2), 103)))<10
THEN CONCAT(DATEPART(YEAR, DATEADD(DAY, -82, CONVERT(DATETIME, RIGHT(MLBREF, 2) + '/' + LEFT(RIGHT(MLBREF, 4), 2) + '/' + '20'+LEFT (MLBREF, 2), 103))), 0,
DATEPART(WEEK, DATEADD(DAY, -82, CONVERT(DATETIME, RIGHT(MLBREF, 2) + '/' + LEFT(RIGHT(MLBREF, 4), 2) + '/' + '20'+LEFT (MLBREF, 2), 103))))
ELSE CONCAT(DATEPART(YEAR, DATEADD(DAY, -82, CONVERT(DATETIME, RIGHT(MLBREF, 2) + '/' + LEFT(RIGHT(MLBREF, 4), 2) + '/' + '20'+LEFT (MLBREF, 2), 103))),
DATEPART(WEEK, DATEADD(DAY, -82, CONVERT(DATETIME, RIGHT(MLBREF, 2) + '/' + LEFT(RIGHT(MLBREF, 4), 2) + '/' + '20'+LEFT (MLBREF, 2), 103))))
END
END
AND (MLSTQT-MLALQT)<>0
GROUP BY DOTWHL, DORELD, MLBREF, DODLDT, DOITNO, DOPPQT, MLSTQT, MMITDS, MLALQT, MBPLCD, MMSTCN, MMCFI2, MMCFI3, DOPSTS, Sales.Sold_Qty, Sales.Forecasted_Qty, MBBUYE, MMRESP, Other_Assortments.item
ORDER BY DOTWHL, DORELD
I'm trying to get data per hour however as I execute, the data for day is displayed in the table. IDC_YMD with per hour result must be in the table and I'm not sure if I have the right SUBSTR(string, start, length).
SELECT EQP_ID, EQP_NAME, 'WorkPreparation' GUBUN,WorkPreparation
CNT,SITE_CODE,GODS_CODE,STEP_NAME,IDC_YMD,IDC_SUMR_UNIT_CODE,MTBI,MTBA
FROM
(
SELECT CASE WHEN WorkPreparation = 0 THEN '-' ELSE EQP_ID END AS EQP_ID, CASE WHEN WorkPreparation =
0 THEN '-' ELSE SUBSTR(EQP_NAME,1,4) END AS EQP_NAME,
WorkPreparation,SITE_CODE,GODS_CODE,STEP_NAME,IDC_YMD,IDC_SUMR_UNIT_CODE,MTBI,MTBA
FROM(
SELECT EQP_ID, EVENT_COMMENT EQP_NAME, SUM(WORK_PRE_CNT) WorkPreparation, A.SITE_CODE
, A.GODS_CODE
, A.EES_STEP_ID AS STEP_NAME
, TO_CHAR(TO_NUMBER(SUBSTR(A.IDC_YMD, 12, 2))) || '/' || TO_CHAR(TO_NUMBER(SUBSTR(A.IDC_YMD, 12,
2))) AS IDC_YMD
, 'D' AS IDC_SUMR_UNIT_CODE
, CASE WHEN (SUM(NVL(A.INTR_CNT, 0)) + SUM(NVL(A.BRDN_CNT, 0))) = 0
THEN ROUND(SUM(NVL(A.OPER_MOP, 0)),1)
ELSE ROUND(SUM(NVL(A.OPER_MOP, 0)) / (SUM(NVL(A.INTR_CNT, 0)) + SUM(NVL(A.BRDN_CNT, 0))) /
60,1)
END AS MTBI
, CASE WHEN (SUM(NVL(A.INTR_CNT, 0)) + SUM(NVL(A.BRDN_CNT, 0)) + SUM(NVL(A.WORK_PRE_CNT, 0)) +
SUM(NVL(A.PROD_CHANGE_CNT, 0)) + SUM(NVL(A.QUAL_EXATN_CNT, 0)) +
SUM(NVL(A.MTR_EXATN_CNT, 0)) + SUM(NVL(A.STEP_CHRC_LOSS_CNT, 0)) +
SUM(NVL(A.IDLE_3MN_UND_EXCPT_CNT, 0))) = 0
THEN 0
ELSE ROUND(SUM(NVL(A.OPER_MOP, 0)) / (SUM(NVL(A.INTR_CNT, 0)) + SUM(NVL(A.BRDN_CNT, 0)) +
SUM(NVL(A.WORK_PRE_CNT, 0)) + SUM(NVL(A.PROD_CHANGE_CNT, 0)) + SUM(NVL(A.QUAL_EXATN_CNT, 0)) +
SUM(NVL(A.MTR_EXATN_CNT, 0)) + SUM(NVL(A.STEP_CHRC_LOSS_CNT,
0)) + SUM(NVL(A.IDLE_3MN_UND_EXCPT_CNT, 0))) / 60,1)
END AS MTBA
FROM IFR_EES_PCL A
WHERE SITE_CODE = 'E502AA'
AND GODS_CODE = 'N5210'
AND MTBI_MNG_YN = 'Y'
AND EES_STEP_ID = 'TP'
AND A.IDC_YMD BETWEEN TO_CHAR(SYSDATE-6, 'YYYYMMDDHH') AND TO_CHAR(SYSDATE, 'YYYYMMDD')
GROUP BY EQP_ID, EVENT_COMMENT,A.SITE_CODE
, A.GODS_CODE
,A.EQP_TYPE
, A.EES_STEP_ID
, TO_CHAR(TO_NUMBER(SUBSTR(A.IDC_YMD, 12, 2)))
, A.IDC_YMD
ORDER BY A.IDC_YMD, MTBI DESC--TO_CHAR(TO_NUMBER(SUBSTR(A.IDC_YMD, 12, 2)))
))
WHERE ROWNUM <= 10
and EQP_NAME like 'TA%'
Thank you!
I'm using SQL Server 2012. I have 2 pivots (Current Year (CY) vs Last Year (LY) that work fine by themselves but want to Union them and have the results all togeather, how do I go about this? I need to have the data look like like this:
Cost Centre Name May_CY May_LY Jun_CY Jun LY.....This_Yr_Total Last_Yr_Total
This is my code:
SELECT
pvt.*,
Isnull(pvt.jan_CY, 0) +
Isnull(pvt.feb_CY, 0) +
Isnull(pvt.mar_CY, 0) +
Isnull(pvt.apr_CY, 0) +
Isnull(pvt.may_CY, 0) +
Isnull(pvt.jun_CY, 0) +
Isnull(pvt.jul_CY, 0) +
Isnull(pvt.aug_CY, 0) +
Isnull(pvt.sept_CY, 0) +
Isnull(pvt.oct_CY, 0) +
Isnull(pvt.nov_CY, 0) +
Isnull(pvt.Dec_CY, 0)
AS This_Year
FROM (SELECT [CostCentre], [CostCentreDesc] AS Name, { fn CONCAT(DATENAME(month, dbo.tblGLS215_2016_2017.AccDate), '_CY') } AS AccMonth, [RecordedAmount]
FROM [tblGLS215_2016_2017]
WHERE Employee <> '') AS s PIVOT (SUM([RecordedAmount]) FOR [AccMonth] IN (May_CY, Jun_CY, Jul_CY, Aug_CY, Sept_CY, Oct_CY, Nov_CY, Dec_CY, Jan_CY, Feb_CY, Mar_CY, Apr_CY)) AS pvt
UNION ALL
SELECT
pvt.*,
Isnull(pvt.jan_LY, 0) +
Isnull(pvt.feb_LY, 0) +
Isnull(pvt.mar_LY, 0) +
Isnull(pvt.apr_LY, 0) +
Isnull(pvt.may_LY, 0) +
Isnull(pvt.jun_LY, 0) +
Isnull(pvt.jul_LY, 0) +
Isnull(pvt.aug_LY, 0) +
Isnull(pvt.sept_LY, 0) +
Isnull(pvt.oct_LY, 0) +
Isnull(pvt.nov_LY, 0) +
Isnull(pvt.Dec_LY, 0)
AS Last_Year
FROM (SELECT [CostCentre], [CostCentreDesc] AS Name, { fn CONCAT(DATENAME(month, dbo.tblGLS215_2015_2016.AccDate), '_LY') } AS AccMonth, [RecordedAmount]
FROM [tblGLS215_2015_2016]
WHERE Employee <> '') AS s PIVOT (SUM([RecordedAmount]) FOR [AccMonth] IN (May_LY, Jun_LY, Jul_LY, Aug_LY, Sept_LY, Oct_LY, Nov_LY, Dec_LY, Jan_LY, Feb_LY, Mar_LY, Apr_LY)) AS pvt
Any help would be greatly appreciated.
This is one of the pivots for 'This Year' that returns results:
SELECT
pvt.*,
Isnull(
pvt.jan,0)+ Isnull(pvt.feb,0)+ Isnull(pvt.mar,0)+ Isnull(pvt.apr,0)+ Isnull(pvt.may,0)+ Isnull(pvt.jun,0)+ Isnull(pvt.jul,0)+
Isnull(pvt.aug,0)+ Isnull(pvt.sept,0)+ Isnull(pvt.oct,0)+ Isnull(pvt.nov,0) AS This_Year_Total
FROM
(
SELECT
[CostCentre],
[CostCentreDesc] AS Name,
CONVERT(
CHAR(4),
AccDate,
100
) AS [Month],
[RecordedAmount]
FROM
[tblGLS215_2016_2017]
) AS s PIVOT(
SUM( [RecordedAmount] ) FOR [Month] IN(
May,
Jun,
Jul,
Aug,
Sept,
Oct,
Nov,
DEC, Jan, Feb, Mar, Apr)) AS pvt
Select A.[CostCentre], A.Name
,A.[May_CY],B.[May_LY]
,A.[Jun_CY],B.[Jun_LY]
,A.[Jul_CY],B.[Jul_LY]
,A.[Aug_CY],B.[Aug_LY]
,A.[Sept_CY],B.[Sept_LY]
,A.[Oct_CY],B.[Oct_LY]
,A.[Nov_CY],B.[Nov_LY]
,A.[Dec_CY],B.[Dec_LY]
,A.[Jan_CY],B.[Jan_LY]
,A.[Feb_CY],B.[Feb_LY]
,A.[Mar_CY],B.[Mar_LY]
,A.[Apr_CY],B.[Apr_LY]
,A.[This_Year] as This_Yr_Total , B.[Last_Year] as Last_Yr_Total
From
(
SELECT
pvt.*,
Isnull(pvt.jan_CY, 0) +
Isnull(pvt.feb_CY, 0) +
Isnull(pvt.mar_CY, 0) +
Isnull(pvt.apr_CY, 0) +
Isnull(pvt.may_CY, 0) +
Isnull(pvt.jun_CY, 0) +
Isnull(pvt.jul_CY, 0) +
Isnull(pvt.aug_CY, 0) +
Isnull(pvt.sept_CY, 0) +
Isnull(pvt.oct_CY, 0) +
Isnull(pvt.nov_CY, 0) +
Isnull(pvt.Dec_CY, 0)
AS This_Year
FROM (SELECT [CostCentre], [CostCentreDesc] AS Name, { fn CONCAT(DATENAME(month, dbo.tblGLS215_2016_2017.AccDate), '_CY') } AS AccMonth, [RecordedAmount]
FROM [tblGLS215_2016_2017]
WHERE Employee <> '') AS s PIVOT (SUM([RecordedAmount]) FOR [AccMonth] IN (May_CY, Jun_CY, Jul_CY, Aug_CY, Sept_CY, Oct_CY, Nov_CY, Dec_CY, Jan_CY, Feb_CY, Mar_CY, Apr_CY)) AS pvt
)as A
full outer Join
(
SELECT
pvt.*,
Isnull(pvt.jan_LY, 0) +
Isnull(pvt.feb_LY, 0) +
Isnull(pvt.mar_LY, 0) +
Isnull(pvt.apr_LY, 0) +
Isnull(pvt.may_LY, 0) +
Isnull(pvt.jun_LY, 0) +
Isnull(pvt.jul_LY, 0) +
Isnull(pvt.aug_LY, 0) +
Isnull(pvt.sept_LY, 0) +
Isnull(pvt.oct_LY, 0) +
Isnull(pvt.nov_LY, 0) +
Isnull(pvt.Dec_LY, 0)
AS Last_Year
FROM (SELECT [CostCentre], [CostCentreDesc] AS Name, { fn CONCAT(DATENAME(month, dbo.tblGLS215_2015_2016.AccDate), '_LY') } AS AccMonth, [RecordedAmount]
FROM [tblGLS215_2015_2016]
WHERE Employee <> '') AS s PIVOT (SUM([RecordedAmount]) FOR [AccMonth] IN (May_LY, Jun_LY, Jul_LY, Aug_LY, Sept_LY, Oct_LY, Nov_LY, Dec_LY, Jan_LY, Feb_LY, Mar_LY, Apr_LY)) AS pvt
) as B
On A.[CostCentre]=B.[CostCentre] and A.Name=B.Name
I have a table in the following structureL
{col_a1, .. ,Col_a15, Continuous_count}
These columns might have either a NULL or a particular value (in col_a1 to col_a15).
I need to find out the most continuous set of data and keep the count in the column continuous_count. For example:
Columns: col_a1 col_a2 col_a3 col_a4 col_a5 col_a6 col_a7 Continuous_count
ROW_1: NULL NULL 2 2 3 NULL 2 3
ROW_2: NULL 1 2 2 3 NULL 2 4
ROW_3: NULL NULL NULL 2 3 NULL 2 2
ROW_4: 2 1 2 2 3 NULL 2 5
I am not able to write the query, i wrote one but looks too big with a lot of ifs and else's. is there a simple method to do this.
The only way I see is check for all combinations of adjacent columns.
I am using power(1,col_ax) below to get 1 for any value and null for null. When adding, combinations having at least one null result in null (and thus in 0 for the use of NVL, because otherwise GREATEST would result in null).
select mytable.*,
greatest
(
nvl(power(1,col_a1), 0),
nvl(power(1,col_a1) + power(1,col_a2), 0),
nvl(power(1,col_a1) + power(1,col_a2) + power(1,col_a3), 0),
nvl(power(1,col_a1) + power(1,col_a2) + power(1,col_a3) + power(1,col_a4), 0),
nvl(power(1,col_a1) + power(1,col_a2) + power(1,col_a3) + power(1,col_a4) + power(1,col_a5), 0),
nvl(power(1,col_a1) + power(1,col_a2) + power(1,col_a3) + power(1,col_a4) + power(1,col_a5) + power(1,col_a6), 0),
nvl(power(1,col_a1) + power(1,col_a2) + power(1,col_a3) + power(1,col_a4) + power(1,col_a5) + power(1,col_a6) + power(1,col_a7), 0),
nvl(power(1,col_a2), 0),
nvl(power(1,col_a2) + power(1,col_a3), 0),
nvl(power(1,col_a2) + power(1,col_a3) + power(1,col_a4), 0),
nvl(power(1,col_a2) + power(1,col_a3) + power(1,col_a4) + power(1,col_a5), 0),
nvl(power(1,col_a2) + power(1,col_a3) + power(1,col_a4) + power(1,col_a5) + power(1,col_a6), 0),
nvl(power(1,col_a2) + power(1,col_a3) + power(1,col_a4) + power(1,col_a5) + power(1,col_a6) + power(1,col_a7), 0),
nvl(power(1,col_a3), 0),
nvl(power(1,col_a3) + power(1,col_a4), 0),
nvl(power(1,col_a3) + power(1,col_a4) + power(1,col_a5), 0),
nvl(power(1,col_a3) + power(1,col_a4) + power(1,col_a5) + power(1,col_a6), 0),
nvl(power(1,col_a3) + power(1,col_a4) + power(1,col_a5) + power(1,col_a6) + power(1,col_a7), 0),
nvl(power(1,col_a4), 0),
nvl(power(1,col_a4) + power(1,col_a5), 0),
nvl(power(1,col_a4) + power(1,col_a5) + power(1,col_a6), 0),
nvl(power(1,col_a4) + power(1,col_a5) + power(1,col_a6) + power(1,col_a7), 0),
nvl(power(1,col_a5), 0),
nvl(power(1,col_a5) + power(1,col_a6), 0),
nvl(power(1,col_a5) + power(1,col_a6) + power(1,col_a7), 0),
nvl(power(1,col_a6), 0),
nvl(power(1,col_a6) + power(1,col_a7), 0),
nvl(power(1,col_a7), 0),
0
) as continous_count
from mytable;
This is a two-pronged question:
Scenario:
I have a script to query MSDB and get me details of job schedules. Obviously, the tables differ from SQL 2000 to SQL 2005. Hence, I want to check the version running on the box and query accordingly. Now the questions:
Question 1:
This is what I am doing.
IF LEFT(CAST(SERVERPROPERTY('ProductVersion') As Varchar),1)='8'
BEGIN
PRINT 'SQL 2000'--Actual Code Goes Here
END
IF LEFT(CAST(SERVERPROPERTY('ProductVersion') As Varchar),1)='9'
BEGIN
PRINT 'SQL 2005'--Actual Code Goes Here
END
Is there a better way of doing this?
Question 2:
Though the above script runs fine on both 2000 and 2005 boxes, when I replace the "Print.." statements with my actual code, it runs fine on a 2000 box, but when executed on a 2005 box,tries to run the code block meant for 2000 and returns errors.
Here is the actual code:
USE [msdb]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
--Check SQL Server Version
IF LEFT(CAST(SERVERPROPERTY('ProductVersion') As Varchar),1)='9'
BEGIN
SELECT ##SERVERNAME
,sysjobs.name
,dbo.udf_schedule_description(dbo.sysschedules.freq_type, dbo.sysschedules.freq_interval,
dbo.sysschedules.freq_subday_type, dbo.sysschedules.freq_subday_interval, dbo.sysschedules.freq_relative_interval,
dbo.sysschedules.freq_recurrence_factor, dbo.sysschedules.active_start_date, dbo.sysschedules.active_end_date,
dbo.sysschedules.active_start_time, dbo.sysschedules.active_end_time) AS [Schedule Description]
, CONVERT(CHAR(8), CASE WHEN LEN(msdb.dbo.sysschedules.Active_Start_Time) = 3
THEN CAST('00:0' + LEFT(msdb.dbo.sysschedules.Active_Start_Time, 1) + ':'
+ SUBSTRING(CAST(msdb.dbo.sysschedules.Active_Start_Time AS VARCHAR(6)),
2, 2) AS VARCHAR(8))
WHEN LEN(msdb.dbo.sysschedules.Active_Start_Time) = 4
THEN CAST('00:'
+ SUBSTRING(CAST(msdb.dbo.sysschedules.Active_Start_Time AS VARCHAR(6)),
1, 2) + ':'
+ SUBSTRING(CAST(msdb.dbo.sysschedules.Active_Start_Time AS VARCHAR(6)),
3, 2) AS VARCHAR(8))
WHEN LEN(msdb.dbo.sysschedules.Active_Start_Time) = 5
THEN CAST('0' + LEFT(msdb.dbo.sysschedules.Active_Start_Time, 1) + ':'
+ SUBSTRING(CAST(msdb.dbo.sysschedules.Active_Start_Time AS VARCHAR(6)),
2, 2) + ':'
+ SUBSTRING(CAST(msdb.dbo.sysschedules.Active_Start_Time AS VARCHAR(6)),
4, 2) AS VARCHAR(8))
WHEN msdb.dbo.sysschedules.Active_Start_Time = 0
THEN '00:00:00'
ELSE CAST(LEFT(msdb.dbo.sysschedules.Active_Start_Time, 2) + ':'
+ SUBSTRING(CAST(msdb.dbo.sysschedules.Active_Start_Time AS VARCHAR(6)),
3, 2) + ':'
+ SUBSTRING(CAST(msdb.dbo.sysschedules.Active_Start_Time AS VARCHAR(6)),
5, 2) AS VARCHAR(8))
END, 108) AS Start_Time,
CONVERT(CHAR(8), CASE WHEN LEN(msdb.dbo.sysschedules.active_end_time) = 3
THEN CAST('00:0' + LEFT(msdb.dbo.sysschedules.active_end_time, 1) + ':'
+ SUBSTRING(CAST(msdb.dbo.sysschedules.active_end_time AS VARCHAR(6)),
2, 2) AS VARCHAR(8))
WHEN LEN(msdb.dbo.sysschedules.active_end_time) = 4
THEN CAST('00:'
+ SUBSTRING(CAST(msdb.dbo.sysschedules.active_end_time AS VARCHAR(6)),
1, 2) + ':'
+ SUBSTRING(CAST(msdb.dbo.sysschedules.active_end_time AS VARCHAR(6)),
3, 2) AS VARCHAR(8))
WHEN LEN(msdb.dbo.sysschedules.active_end_time) = 5
THEN CAST('0' + LEFT(msdb.dbo.sysschedules.active_end_time, 1) + ':'
+ SUBSTRING(CAST(msdb.dbo.sysschedules.active_end_time AS VARCHAR(6)),
2, 2) + ':'
+ SUBSTRING(CAST(msdb.dbo.sysschedules.active_end_time AS VARCHAR(6)),
4, 2) AS VARCHAR(8))
WHEN msdb.dbo.sysschedules.active_end_time = 0
THEN '00:00:00'
ELSE CAST(LEFT(msdb.dbo.sysschedules.active_end_time, 2) + ':'
+ SUBSTRING(CAST(msdb.dbo.sysschedules.active_end_time AS VARCHAR(6)),
3, 2) + ':'
+ SUBSTRING(CAST(msdb.dbo.sysschedules.active_end_time AS VARCHAR(6)),
5, 2) AS VARCHAR(8))
END, 108) AS End_Time
,CAST(CASE WHEN LEN(msdb.dbo.sysjobservers.last_run_duration) = 1
THEN CAST('00:00:0' + LEFT(msdb.dbo.sysjobservers.last_run_duration, 1)AS VARCHAR(8))
WHEN LEN(msdb.dbo.sysjobservers.last_run_duration) = 2
THEN CAST('00:00:' + LEFT(msdb.dbo.sysjobservers.last_run_duration, 2)AS VARCHAR(8))
WHEN LEN(msdb.dbo.sysjobservers.last_run_duration) = 3
THEN CAST('00:0' + LEFT(msdb.dbo.sysjobservers.last_run_duration, 1) + ':'
+ SUBSTRING(CAST(msdb.dbo.sysjobservers.last_run_duration AS VARCHAR(6)),
2, 2) AS VARCHAR(8))
WHEN LEN(msdb.dbo.sysjobservers.last_run_duration) = 4
THEN CAST('00:'
+ SUBSTRING(CAST(msdb.dbo.sysjobservers.last_run_duration AS VARCHAR(6)),
1, 2) + ':'
+ SUBSTRING(CAST(msdb.dbo.sysjobservers.last_run_duration AS VARCHAR(6)),
3, 2) AS VARCHAR(8))
WHEN LEN(msdb.dbo.sysjobservers.last_run_duration) = 5
THEN CAST('0' + LEFT(msdb.dbo.sysjobservers.last_run_duration, 1) + ':'
+ SUBSTRING(CAST(msdb.dbo.sysjobservers.last_run_duration AS VARCHAR(6)),
2, 2) + ':'
+ SUBSTRING(CAST(msdb.dbo.sysjobservers.last_run_duration AS VARCHAR(6)),
4, 2) AS VARCHAR(8))
WHEN msdb.dbo.sysjobservers.last_run_duration = 0
THEN '00:00:00'
ELSE CAST(LEFT(msdb.dbo.sysjobservers.last_run_duration, 2) + ':'
+ SUBSTRING(CAST(msdb.dbo.sysjobservers.last_run_duration AS VARCHAR(6)),
3, 2) + ':'
+ SUBSTRING(CAST(msdb.dbo.sysjobservers.last_run_duration AS VARCHAR(6)),
5, 2) AS VARCHAR(8))
END AS VARCHAR(8)) AS LastRunDuration
FROM msdb.dbo.sysjobs INNER JOIN
msdb.dbo.syscategories ON msdb.dbo.sysjobs.category_id = msdb.dbo.syscategories.category_id LEFT OUTER JOIN
msdb.dbo.sysoperators ON msdb.dbo.sysjobs.notify_page_operator_id = msdb.dbo.sysoperators.id LEFT OUTER JOIN
msdb.dbo.sysjobservers ON msdb.dbo.sysjobs.job_id = msdb.dbo.sysjobservers.job_id LEFT OUTER JOIN
msdb.dbo.sysjobschedules ON msdb.dbo.sysjobschedules.job_id = msdb.dbo.sysjobs.job_id LEFT OUTER JOIN
msdb.dbo.sysschedules ON msdb.dbo.sysjobschedules.schedule_id = msdb.dbo.sysschedules.schedule_id
WHERE sysjobs.enabled = 1 AND msdb.dbo.sysschedules.Active_Start_Time IS NOT NULL
ORDER BY Start_time,sysjobs.name
END
IF LEFT(CAST(SERVERPROPERTY('ProductVersion') As Varchar),1)='8'
BEGIN
SELECT ##SERVERNAME
,sysjobs.name
,dbo.udf_schedule_description(sysjobschedules.freq_type, sysjobschedules.freq_interval,
sysjobschedules.freq_subday_type, sysjobschedules.freq_subday_interval, sysjobschedules.freq_relative_interval,
sysjobschedules.freq_recurrence_factor, sysjobschedules.active_start_date, sysjobschedules.active_end_date,
sysjobschedules.active_start_time, sysjobschedules.active_end_time) AS [Schedule Description]
, CONVERT(CHAR(8), CASE WHEN LEN(msdb.dbo.sysjobschedules.Active_Start_Time) = 3
THEN CAST('00:0' + LEFT(msdb.dbo.sysjobschedules.Active_Start_Time, 1) + ':'
+ SUBSTRING(CAST(msdb.dbo.sysjobschedules.Active_Start_Time AS VARCHAR(6)),
2, 2) AS VARCHAR(8))
WHEN LEN(msdb.dbo.sysjobschedules.Active_Start_Time) = 4
THEN CAST('00:'
+ SUBSTRING(CAST(msdb.dbo.sysjobschedules.Active_Start_Time AS VARCHAR(6)),
1, 2) + ':'
+ SUBSTRING(CAST(msdb.dbo.sysjobschedules.Active_Start_Time AS VARCHAR(6)),
3, 2) AS VARCHAR(8))
WHEN LEN(msdb.dbo.sysjobschedules.Active_Start_Time) = 5
THEN CAST('0' + LEFT(msdb.dbo.sysjobschedules.Active_Start_Time, 1) + ':'
+ SUBSTRING(CAST(msdb.dbo.sysjobschedules.Active_Start_Time AS VARCHAR(6)),
2, 2) + ':'
+ SUBSTRING(CAST(msdb.dbo.sysjobschedules.Active_Start_Time AS VARCHAR(6)),
4, 2) AS VARCHAR(8))
WHEN msdb.dbo.sysjobschedules.Active_Start_Time = 0
THEN '00:00:00'
ELSE CAST(LEFT(msdb.dbo.sysjobschedules.Active_Start_Time, 2) + ':'
+ SUBSTRING(CAST(msdb.dbo.sysjobschedules.Active_Start_Time AS VARCHAR(6)),
3, 2) + ':'
+ SUBSTRING(CAST(msdb.dbo.sysjobschedules.Active_Start_Time AS VARCHAR(6)),
5, 2) AS VARCHAR(8))
END, 108) AS Start_Time,
CONVERT(CHAR(8), CASE WHEN LEN(msdb.dbo.sysjobschedules.active_end_time) = 3
THEN CAST('00:0' + LEFT(msdb.dbo.sysjobschedules.active_end_time, 1) + ':'
+ SUBSTRING(CAST(msdb.dbo.sysjobschedules.active_end_time AS VARCHAR(6)),
2, 2) AS VARCHAR(8))
WHEN LEN(msdb.dbo.sysjobschedules.active_end_time) = 4
THEN CAST('00:'
+ SUBSTRING(CAST(msdb.dbo.sysjobschedules.active_end_time AS VARCHAR(6)),
1, 2) + ':'
+ SUBSTRING(CAST(msdb.dbo.sysjobschedules.active_end_time AS VARCHAR(6)),
3, 2) AS VARCHAR(8))
WHEN LEN(msdb.dbo.sysjobschedules.active_end_time) = 5
THEN CAST('0' + LEFT(msdb.dbo.sysjobschedules.active_end_time, 1) + ':'
+ SUBSTRING(CAST(msdb.dbo.sysjobschedules.active_end_time AS VARCHAR(6)),
2, 2) + ':'
+ SUBSTRING(CAST(msdb.dbo.sysjobschedules.active_end_time AS VARCHAR(6)),
4, 2) AS VARCHAR(8))
WHEN msdb.dbo.sysjobschedules.active_end_time = 0
THEN '00:00:00'
ELSE CAST(LEFT(msdb.dbo.sysjobschedules.active_end_time, 2) + ':'
+ SUBSTRING(CAST(msdb.dbo.sysjobschedules.active_end_time AS VARCHAR(6)),
3, 2) + ':'
+ SUBSTRING(CAST(msdb.dbo.sysjobschedules.active_end_time AS VARCHAR(6)),
5, 2) AS VARCHAR(8))
END, 108) AS End_Time
,CAST(CASE WHEN LEN(msdb.dbo.sysjobservers.last_run_duration) = 1
THEN CAST('00:00:0' + LEFT(msdb.dbo.sysjobservers.last_run_duration, 1)AS VARCHAR(8))
WHEN LEN(msdb.dbo.sysjobservers.last_run_duration) = 2
THEN CAST('00:00:' + LEFT(msdb.dbo.sysjobservers.last_run_duration, 2)AS VARCHAR(8))
WHEN LEN(msdb.dbo.sysjobservers.last_run_duration) = 3
THEN CAST('00:0' + LEFT(msdb.dbo.sysjobservers.last_run_duration, 1) + ':'
+ SUBSTRING(CAST(msdb.dbo.sysjobservers.last_run_duration AS VARCHAR(6)),
2, 2) AS VARCHAR(8))
WHEN LEN(msdb.dbo.sysjobservers.last_run_duration) = 4
THEN CAST('00:'
+ SUBSTRING(CAST(msdb.dbo.sysjobservers.last_run_duration AS VARCHAR(6)),
1, 2) + ':'
+ SUBSTRING(CAST(msdb.dbo.sysjobservers.last_run_duration AS VARCHAR(6)),
3, 2) AS VARCHAR(8))
WHEN LEN(msdb.dbo.sysjobservers.last_run_duration) = 5
THEN CAST('0' + LEFT(msdb.dbo.sysjobservers.last_run_duration, 1) + ':'
+ SUBSTRING(CAST(msdb.dbo.sysjobservers.last_run_duration AS VARCHAR(6)),
2, 2) + ':'
+ SUBSTRING(CAST(msdb.dbo.sysjobservers.last_run_duration AS VARCHAR(6)),
4, 2) AS VARCHAR(8))
WHEN msdb.dbo.sysjobservers.last_run_duration = 0
THEN '00:00:00'
ELSE CAST(LEFT(msdb.dbo.sysjobservers.last_run_duration, 2) + ':'
+ SUBSTRING(CAST(msdb.dbo.sysjobservers.last_run_duration AS VARCHAR(6)),
3, 2) + ':'
+ SUBSTRING(CAST(msdb.dbo.sysjobservers.last_run_duration AS VARCHAR(6)),
5, 2) AS VARCHAR(8))
END AS VARCHAR(8)) AS LastRunDuration
FROM sysjobs LEFT OUTER JOIN
msdb.dbo.sysjobservers ON msdb.dbo.sysjobs.job_id = msdb.dbo.sysjobservers.job_id INNER JOIN
sysjobschedules ON sysjobs.job_id = sysjobschedules.job_id
WHERE sysjobs.enabled = 1
ORDER BY Start_time,sysjobs.name
END
This script requires a udf in MSDB. Here is the code for the function:
USE [msdb]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE FUNCTION [dbo].[udf_schedule_description] (#freq_type INT ,
#freq_interval INT ,
#freq_subday_type INT ,
#freq_subday_interval INT ,
#freq_relative_interval INT ,
#freq_recurrence_factor INT ,
#active_start_date INT ,
#active_end_date INT,
#active_start_time INT ,
#active_end_time INT )
RETURNS NVARCHAR(255) AS
BEGIN
DECLARE #schedule_description NVARCHAR(255)
DECLARE #loop INT
DECLARE #idle_cpu_percent INT
DECLARE #idle_cpu_duration INT
IF (#freq_type = 0x1) -- OneTime
BEGIN
SELECT #schedule_description = N'Once on ' + CONVERT(NVARCHAR, #active_start_date) + N' at ' + CONVERT(NVARCHAR, cast((#active_start_time / 10000) as varchar(10)) + ':' + right('00' + cast((#active_start_time % 10000) / 100 as varchar(10)),2))
RETURN #schedule_description
END
IF (#freq_type = 0x4) -- Daily
BEGIN
SELECT #schedule_description = N'Every day '
END
IF (#freq_type = 0x8) -- Weekly
BEGIN
SELECT #schedule_description = N'Every ' + CONVERT(NVARCHAR, #freq_recurrence_factor) + N' week(s) on '
SELECT #loop = 1
WHILE (#loop <= 7)
BEGIN
IF (#freq_interval & POWER(2, #loop - 1) = POWER(2, #loop - 1))
SELECT #schedule_description = #schedule_description + DATENAME(dw, N'1996120' + CONVERT(NVARCHAR, #loop)) + N', '
SELECT #loop = #loop + 1
END
IF (RIGHT(#schedule_description, 2) = N', ')
SELECT #schedule_description = SUBSTRING(#schedule_description, 1, (DATALENGTH(#schedule_description) / 2) - 2) + N' '
END
IF (#freq_type = 0x10) -- Monthly
BEGIN
SELECT #schedule_description = N'Every ' + CONVERT(NVARCHAR, #freq_recurrence_factor) + N' months(s) on day ' + CONVERT(NVARCHAR, #freq_interval) + N' of that month '
END
IF (#freq_type = 0x20) -- Monthly Relative
BEGIN
SELECT #schedule_description = N'Every ' + CONVERT(NVARCHAR, #freq_recurrence_factor) + N' months(s) on the '
SELECT #schedule_description = #schedule_description +
CASE #freq_relative_interval
WHEN 0x01 THEN N'first '
WHEN 0x02 THEN N'second '
WHEN 0x04 THEN N'third '
WHEN 0x08 THEN N'fourth '
WHEN 0x10 THEN N'last '
END +
CASE
WHEN (#freq_interval > 00)
AND (#freq_interval < 08) THEN DATENAME(dw, N'1996120' + CONVERT(NVARCHAR, #freq_interval))
WHEN (#freq_interval = 08) THEN N'day'
WHEN (#freq_interval = 09) THEN N'week day'
WHEN (#freq_interval = 10) THEN N'weekend day'
END + N' of that month '
END
IF (#freq_type = 0x40) -- AutoStart
BEGIN
SELECT #schedule_description = FORMATMESSAGE(14579)
RETURN #schedule_description
END
IF (#freq_type = 0x80) -- OnIdle
BEGIN
EXECUTE master.dbo.xp_instance_regread N'HKEY_LOCAL_MACHINE',
N'SOFTWARE\Microsoft\MSSQLServer\SQLServerAgent',
N'IdleCPUPercent',
#idle_cpu_percent OUTPUT,
N'no_output'
EXECUTE master.dbo.xp_instance_regread N'HKEY_LOCAL_MACHINE',
N'SOFTWARE\Microsoft\MSSQLServer\SQLServerAgent',
N'IdleCPUDuration',
#idle_cpu_duration OUTPUT,
N'no_output'
SELECT #schedule_description = FORMATMESSAGE(14578, ISNULL(#idle_cpu_percent, 10), ISNULL(#idle_cpu_duration, 600))
RETURN #schedule_description
END
-- Subday stuff
SELECT #schedule_description = #schedule_description +
CASE #freq_subday_type
WHEN 0x1 THEN N'at ' + CONVERT(NVARCHAR, cast(
CASE WHEN LEN(cast((#active_start_time / 10000)as varchar(10)))=1
THEN '0'+cast((#active_start_time / 10000) as varchar(10))
ELSE cast((#active_start_time / 10000) as varchar(10))
END
as varchar(10)) + ':' + right('00' + cast((#active_start_time % 10000) / 100 as varchar(10)),2))
WHEN 0x2 THEN N'every ' + CONVERT(NVARCHAR, #freq_subday_interval) + N' second(s)'
WHEN 0x4 THEN N'every ' + CONVERT(NVARCHAR, #freq_subday_interval) + N' minute(s)'
WHEN 0x8 THEN N'every ' + CONVERT(NVARCHAR, #freq_subday_interval) + N' hour(s)'
END
IF (#freq_subday_type IN (0x2, 0x4, 0x8))
SELECT #schedule_description = #schedule_description + N' between ' +
CONVERT(NVARCHAR, cast(
CASE WHEN LEN(cast((#active_start_time / 10000)as varchar(10)))=1
THEN '0'+cast((#active_start_time / 10000) as varchar(10))
ELSE cast((#active_start_time / 10000) as varchar(10))
END
as varchar(10)) + ':' + right('00' + cast((#active_start_time % 10000) / 100 as varchar(10)),2) )
+ N' and ' +
CONVERT(NVARCHAR, cast(
CASE WHEN LEN(cast((#active_end_time / 10000)as varchar(10)))=1
THEN '0'+cast((#active_end_time / 10000) as varchar(10))
ELSE cast((#active_end_time / 10000) as varchar(10))
END
as varchar(10)) + ':' + right('00' + cast((#active_end_time % 10000) / 100 as varchar(10)),2) )
RETURN #schedule_description
END
I have got this far and have spent too much time trying to find out what the problem is. Please help.
the errors are compile time (I ran on 2005):
Msg 207, Level 16, State 1, Line 106
Invalid column name 'freq_type'.
Msg 207, Level 16, State 1, Line 106
Invalid column name 'freq_interval'.
Msg 207, Level 16, State 1, Line 107
Invalid column name 'freq_subday_type'.
Msg 207, Level 16, State 1, Line 107
Invalid column name 'freq_subday_interval'.
Msg 207, Level 16, State 1, Line 107
Invalid column name 'freq_relative_interval'.
Msg 207, Level 16, State 1, Line 108
Invalid column name 'freq_recurrence_factor'.
Msg 207, Level 16, State 1, Line 108
Invalid column name 'active_start_date'.
Msg 207, Level 16, State 1, Line 108
Invalid column name 'active_end_date'.
Msg 207, Level 16, State 1, Line 109
Invalid column name 'active_start_time'.
Msg 207, Level 16, State 1, Line 109
Invalid column name 'active_end_time'.
Msg 207, Level 16, State 1, Line 110
I added PRINTs and they never appear.
your code has problems because the column names are not compatible with the database you are running. SQL Server 2005 does not have a "sysjobschedules.freq_type" column.
Make a stored procedure XYZ, put the 2000 version in the 2000 database, put the same XYZ procedure on the 2005 machine and put the 2005 version in it. No IF necessary...
EDIT
run this code:
PRINT 'Works'
now run this code
PRINT 'will not see this'
error
try this:
PRINT 'will not see this'
SELECT xyz from sysjobschedules
now try running this, but only highlight the PRINT line:
PRINT 'you can see this' --only select this line of code and run it
SELECT xyz from sysjobschedules
see how compile errors prevent anything from running
EDIT
you might try something like this...
DECLARE #Query varchar(max)
IF LEFT(CAST(SERVERPROPERTY('ProductVersion') As Varchar),1)='8'
BEGIN
SET #Query=......
END
IF LEFT(CAST(SERVERPROPERTY('ProductVersion') As Varchar),1)='9'
BEGIN
SET #Query=......
END
EXEC (#Query)
The original question is only specific to 2000,2005 but here's some code that should work on 2000, 2005, 2008 and onwards
DECLARE #ver NVARCHAR(128)
DECLARE #majorVersion int
SET #ver = CAST(SERVERPROPERTY('productversion') AS NVARCHAR)
SET #ver = SUBSTRING(#ver,1,CHARINDEX('.',#ver)-1)
SET #majorVersion = CAST(#ver AS INT)
IF #majorVersion < 11
PRINT 'Plesae Upgrade'
ELSE
PRINT #majorVersion
I tried the code on 2008 and it ran fine. are you in the msdb database when running it?
to get the version you can also do this
SELECT
PARSENAME(CONVERT(VARCHAR(100),SERVERPROPERTY('ProductVersion')),4)
AS SqlServerVersion
--I found it easier this way to code for service pack features for multiple SQL versions.
For example, say I want all the new performance counters and know they were released on a particular service pack on multiple SQL versions. So if I code for greater than SQL Server 12 SP3, SQL 2014 SP2, AND SQL 2016 or better I would do this.
DECLARE #sql_version INT = CONVERT(INT,LEFT(REPLACE(CAST(SERVERPROPERTY('ProductVersion') AS CHAR(15)),'.',''),7))
SELECT
CASE
WHEN
(#sql_version >= 1106020 AND #sql_version < 1200000) OR
(#sql_version >= 1205000 AND #sql_version < 1300000) OR
#sql_version >= 1301601
THEN
'Execute code block'
ELSE
'Execute code block'
END
or shorthand like this...
SELECT
CASE
WHEN
(#sql_version BETWEEN 1106020 AND 1200000) OR
(#sql_version BETWEEN 1205000 AND 1300000) OR
#sql_version >= 1301601
THEN
'Execute code block'
ELSE
'Execute code block'
END
Easy, eh?