error in sql query - sql

I have this query:
( SELECT
SUM(
CONVERT(
FLOAT,
CASE '01'
WHEN '01' THEN
CASE SIGN((sltr_tran_amt - ISNULL(sltr_matched_amt, 0)))
WHEN 1 THEN
CASE DBO.glas_aging_oth(CONVERT(DATETIME, '2009/04/04') - FLOOR(sltr_pstng_date))
WHEN 0 THEN (sltr_tran_amt - ISNULL(sltr_matched_amt, 0))
ELSE 0
END
ELSE 0
END
WHEN '02' THEN
CASE SIGN((sltr_tran_amt - ISNULL(sltr_matched_amt, 0)))
WHEN -1 THEN
CASE DBO.glas_aging_oth(CONVERT(DATETIME, '2009/04/04') - FLOOR(sltr_doc_date))
WHEN 0 THEN (sltr_tran_amt - ISNULL(sltr_matched_amt, 0))
ELSE 0
END
ELSE 0
END
END
)
)
FROM
WHERE
CASE '01'
WHEN '01' THEN sltr_pstng_date
ELSE sltr_doc_date
END =
CASE '01'
WHEN '01' THEN sltr_pstng_date
ELSE sltr_doc_date
END
) thirty_days,
from glas_sl_transactions
When i execute this query, an error is ocurring near 'where'. How do I avoid this? If possible, please also tell me how to shorten this query.

Somewhere deep in the middle of that query, you have FROM WHERE - ie. you are missing a table name after the FROM.
Also, you can make it shorter by removing all the CASE '1' WHEN '1' THEN bits, since '1' is always '1'.
Edit: Ok, here's my reformatting of it:
( SELECT
SUM(
CONVERT(
FLOAT,
CASE '01'
WHEN '01' THEN
CASE SIGN((sltr_tran_amt - ISNULL(sltr_matched_amt, 0)))
WHEN 1 THEN
CASE DBO.glas_aging_oth(CONVERT(DATETIME, '2009/04/04') - FLOOR(sltr_pstng_date))
WHEN 0 THEN (sltr_tran_amt - ISNULL(sltr_matched_amt, 0))
ELSE 0
END
ELSE 0
END
WHEN '02' THEN
CASE SIGN((sltr_tran_amt - ISNULL(sltr_matched_amt, 0)))
WHEN -1 THEN
CASE DBO.glas_aging_oth(CONVERT(DATETIME, '2009/04/04') - FLOOR(sltr_doc_date))
WHEN 0 THEN (sltr_tran_amt - ISNULL(sltr_matched_amt, 0))
ELSE 0
END
ELSE 0
END
END
)
)
FROM
WHERE
CASE '01'
WHEN '01' THEN sltr_pstng_date
ELSE sltr_doc_date
END =
CASE '01'
WHEN '01' THEN sltr_pstng_date
ELSE sltr_doc_date
END
) thirty_days,
from glas_sl_transactions
So it looks like you are also missing an END in that gap before the first closing bracket above the FROM.
Also, it contains a lot of code that will never be executed. Eg.
CASE '01'
WHEN '01' /* always executed */
WHEN '02' /* never executed */
END
The where-clause also boils down to 1=1, and can therefore be removed.
In conclusion, here's what I think it should look like.
SELECT SUM( CONVERT(
FLOAT,
CASE SIGN((sltr_tran_amt - ISNULL(sltr_matched_amt, 0)))
WHEN 1 THEN
CASE DBO.glas_aging_oth(CONVERT(DATETIME, '2009/04/04') - FLOOR(sltr_pstng_date))
WHEN 0 THEN (sltr_tran_amt - ISNULL(sltr_matched_amt, 0))
ELSE 0
END
ELSE 0
END
))
from glas_sl_transactions

Related

CASE WHEN using AND statement in SQL

I have a query like this :
SELECT
CASE
WHEN (CONVERT(INT, (datepart(DD, A.CIDATE))) AND Year(getDate()) = 2016 ) <= 15
THEN CONVERT(INT,(datepart(MM,A.CIDATE)))
WHEN CONVERT(INT, (datepart(DD, A.CIDATE))) > 15
THEN CONVERT(INT, (datepart(MM, A.CIDATE))) + 1
ELSE 0
END AS MASA_PAJAK
FROM
V_CLAIM_INTERNAL A
It's working but when I add AND Year(getDate()) = 2016 it throws an error:
Msg 4145, Level 15, State 1, Line 1
An expression of non-boolean type specified in a context where a condition is expected, near 'AND'.
I try to make convert also but still having the same error.
SELECT
CASE
WHEN ((CONVERT(INT, (datepart(DD, A.CIDATE))) AND (convert(int, Year(getDate()) = 2016 ))) <= 15
THEN CONVERT(INT, (datepart(MM, A.CIDATE)))
WHEN CONVERT(INT, (datepart(DD, A.CIDATE))) > 15
THEN CONVERT(INT, (datepart(MM, A.CIDATE))) + 1
ELSE 0
END AS MASA_PAJAK
FROM
V_CLAIM_INTERNAL A
What is my query mistake :?
This is probably what you are intending to do:
SELECT CASE WHEN DATEPART(DD, A.CIDATE) <= 15 AND YEAR(GETDATE()) = 2016
THEN DATEPART(MM, A.CIDATE)
WHEN DATEPART(DD, A.CIDATE) > 15
THEN DATEPART(MM, A.CIDATE) + 1
ELSE 0
END AS MASA_PAJAK
FROM V_CLAIM_INTERNAL A
Note that you don't need to convert/cast the result of calling DATEPART, because it already returns an integer.
SELECT CASE
WHEN (CONVERT(INT,(datepart(DD,A.CIDATE)))<= 15 AND Year(getDate()) = 2016 ) THEN CONVERT(INT,(datepart(MM,A.CIDATE)))
WHEN CONVERT(INT,(datepart(DD,A.CIDATE))) > 15 THEN CONVERT(INT,(datepart(MM,A.CIDATE)))+1
ELSE 0 END AS MASA_PAJAK
FROM V_CLAIM_INTERNAL A
Try this Query:
SELECT CASE WHEN datepart(DD,A.CIDATE)<= 15 AND
Year(getDate()) = 2016
THEN datepart(MM,A.CIDATE)
WHEN datepart(DD,A.CIDATE) > 15
THEN datepart(MM,A.CIDATE) + 1
ELSE 0
END AS MASA_PAJAK
FROM V_CLAIM_INTERNAL A

I want to get number of CenterMeeting between give two date on that case

case when #mode='TwoDays'
then CenterMeetingDay between datepart(dw,GETDATE()-1) and DATEPART(DW,GETDATE()+1)
end CenterMeetingDay
In SQL Server, you cannot simply put a boolean comparison as the when clause.
So, include the condition in the when and be explicit about the return values:
(case when #mode = 'TwoDays' and
CenterMeetingDay between datepart(dw, GETDATE() - 1) and DATEPART(DW, GETDATE() + 1)
then 1 else 0
end) as CenterMeetingDay
In a where clause, you can do:
where ( (#mode = 'TwoDays') and CenterMeetingDay between datepart(dw, GETDATE() - 1) and DATEPART(DW, GETDATE() + 1)) or
( (#mode <> 'TwoDays' . . . )
or:
where (case when #mode = 'TwoDays' and
CenterMeetingDay between datepart(dw, GETDATE() - 1) and DATEPART(DW, GETDATE() + 1)
then 1 else 0
end) = 1

Calculating columns vertically

We are trying to do an analysis of how long our staff have been working on a hourly basis for trending and forecasting purposes.
I want to do a headcount by calculating all the values in the columns vertically.
This is the outputs in SQL in hourly format
(I have circled the values I want to sum up)
As long as the value is more than 0, I want to count it as 1 as this is for headcount by hour.
This is part of my query to produce the output.
SELECT
--b.*,
b.EMPLOYEENAME,
B.DEPARTMENT,
CONVERT(datetime, LEFT(b.SHIFTA_start,17),103) AS SHIFTA_start,
CONVERT(datetime, LEFT(b.ShiftA_End,17),103) as ShiftA_End,
b.StartTime_HOUR,
b.StartTime_min,
b.EndTime_HOUR,
b.EndTime_min,
CASE WHEN b.[0H_START] < b.[0H_END] THEN b.[0H_START] ELSE b.[0H_END] END AS [0],
CASE WHEN b.[1H_START] < b.[1H_END] THEN b.[1H_START] ELSE b.[1H_END] END AS [1]
from
(
/*Step 2 - calculating minutes from starttime and endtime */
select a.*,
/**Calculating the number of minutes worked from start_time MIN **/
CASE WHEN a.StartTime_HOUR = 0 and a.[0] = 1 AND a.StartTime_min !=0 THEN cast(cast((60-a.StartTime_min) as decimal(10,2))/60 as decimal(10,2)) ELSE a.[0] END AS [0H_START],
CASE WHEN a.StartTime_HOUR = 1 and a.[1] = 1 AND a.StartTime_min !=0 THEN cast(cast((60-a.StartTime_min) as decimal(10,2))/60 as decimal(10,2)) ELSE a.[1] END AS [1H_START],
/**Calculating the number of minutes worked from END_time MIN **/
CASE WHEN a.EndTime_HOUR = 0 and a.[0] = 1 AND a.EndTime_min !=0 THEN cast(cast((a.EndTime_min) as decimal(10,2))/60 as decimal(10,2)) ELSE a.[0] END AS [0H_END],
CASE WHEN a.EndTime_HOUR = 1 and a.[1] = 1 AND a.EndTime_min !=0 THEN cast(cast((a.EndTime_min) as decimal(10,2))/60 as decimal(10,2)) ELSE a.[1] END AS [1H_END]
from
(--Step 1:
/*to determine 1 or 0 using the start and end hour
If time falls in the respective hour = 1
if time doesnt fall in the respective hours = 0*/
SELECT
[EMPLOYEENAME],
[DEPARTMENT],
[SHIFTA_start],
CASE WHEN [SHIFTA_START] !='' OR SHIFTA_START != NULL THEN CONVERT(datetime, LEFT([SHIFTA_START],17),103) ELSE NULL END AS SHIFTA_START_con,
CASE WHEN [SHIFTA_START] !='' OR SHIFTA_START != NULL THEN DATEPART(hh,CONVERT(datetime, LEFT([SHIFTA_START],17),103)) ELSE NULL END AS StartTime_HOUR,
CASE WHEN [SHIFTA_START] !='' OR SHIFTA_START != NULL THEN DATEPART(mi,CONVERT(datetime, LEFT([SHIFTA_START],17),103)) ELSE NULL END AS StartTime_min,
[SHIFTA_end],
CASE WHEN [SHIFTA_END] !='' OR SHIFTA_end != NULL THEN CONVERT(datetime, LEFT([SHIFTA_END],17),103) ELSE NULL END AS SHIFTA_END_con,
CASE WHEN [SHIFTA_END] !='' OR SHIFTA_end != NULL THEN DATEPART(hh,CONVERT(datetime, LEFT([SHIFTA_end],17),103)) ELSE NULL END AS EndTime_HOUR,
CASE WHEN [SHIFTA_END] !='' OR SHIFTA_end != NULL THEN DATEPART(mi,CONVERT(datetime, LEFT([SHIFTA_end],17),103)) ELSE NULL END AS EndTime_min,
CASE WHEN [SHIFTA_START] !='' AND 0 BETWEEN DATEPART(hh,CONVERT(datetime, LEFT([SHIFTA_START],17),103)) AND DATEPART (hh,CONVERT(datetime, LEFT([SHIFTA_end],17),103)) THEN 1 ELSE 0 END AS [0],
CASE WHEN [SHIFTA_START] !='' AND 1 BETWEEN DATEPART(hh,CONVERT(datetime, LEFT([SHIFTA_START],17),103)) AND DATEPART (hh,CONVERT(datetime, LEFT([SHIFTA_end],17),103)) THEN 1 ELSE 0 END AS [1]
from [DatabaseTable].[dbo].[ATTENDANCE]
where ShiftA_Start != '' and ShiftA_End !='' and shiftA_start != shiftA_End
)a
)b
Please help, thank you v much!
If you want to calculate the number of all values in this column you can go simply with:
SELECT COUNT(T.[22]) FROM Table AS T
WHERE T.[22] > 0

Calculate business days between two dates

I tried the following query I got from an earlier question:
SELECT OrderNumber, InstallDate, CompleteDate,
(TRUNC(CompleteDate) - TRUNC(InstallDate) ) +1 -
((((TRUNC(CompleteDate,'D'))-(TRUNC(InstallDate,'D')))/7)*2) -
(CASE WHEN TO_CHAR(InstallDate,'DY','nls_date_language=english')='SUN' THEN 1 ELSE 0 END) -
(CASE WHEN TO_CHAR(CompleteDate,'DY','nls_date_language=english')='SAT' THEN 1 ELSE 0 END) as BusinessDays
FROM Orders
ORDER BY OrderNumber;
But I get an 'ORA-01722 - Invalid Number' error.
What am I doing wrong?
Following is my modified code that I'm having issues with:
select
CASE WHEN a.reqdt=0 THEN to_char('01/01/2010') ELSE TO_CHAR(TO_DATE('01-01-1970','DD-MM-YY') + ( a.reqdt/ 86400000 ), 'MM/DD/YYYY') END AS DT_SENT_TO_VENDOR,
CASE WHEN a.completedt=0 THEN to_char('01/01/2010') ELSE TO_CHAR(TO_DATE('01-01-1970','DD-MM-YY') + ( a.completedt/ 86400000 ), 'MM/DD/YYYY') END AS COMPLETED_DT,
(CASE WHEN a.completedt=0 THEN to_char('01/01/2010') ELSE TO_CHAR(TO_DATE('01-01-1970','DD-MM-YY') + ( a.completedt/ 86400000 ), 'dd-mm-yyyyY') END -
CASE WHEN a.reqdt=0 THEN to_char('01/01/2010') ELSE TO_CHAR(TO_DATE('01-01-1970','DD-MM-YY') + ( a.reqdt/ 86400000 ), 'MM/DD/YYYY') END ) + 1 -
/*(a.completedt - a.reqdt ) + 1 - */
((((TRUNC(a.completedt,'D'))) - (TRUNC(a.reqdt,'D'))/7)*2) -
(CASE WHEN TO_CHAR(a.reqdt,'DY','nls_date_language=american')='SUN' THEN 1 ELSE 0 END) -
(CASE WHEN TO_CHAR(a.completedt,'DY','nls_date_language=american')='SAT' THEN 1 ELSE 0 END) as BusinessDays
from EST_VER a;

SQL Server 2005 : conversion failed when converting the varchar value '0.00' to data type int

When adding this line into my query:
convert(varchar(20), convert(varchar(20),
sum(case when tsr.other like '%aa%' then tsr.block1 else 0 end) +
sum(case when tsr.other like '%aa%' then tsr.block2 else 0 end) +
sum(case when tsr.other like '%aa%' then tsr.block3 else 0 end) +
sum(case when tsr.other like '%aa%' then tsr.block4 else 0 end)) * 450)
I get this error message:
Conversion failed when converting the varchar value '0.00' to data
type int
Data in block column is days - e.g. 10.0
Any ideas?
i've got it fixed, it was just changing the 450 to 450.0.
The reason for the varchars is that this is just 1 line from 1 of multiple unioned select statements.
10.0 isn't an int - it's a decimal.
Try
declare #i int
select #i = convert(decimal(9,4),'10.0')
select #i
and the conversion from the decimal to the int will be done implicitly.
'10.0' isn't an int/decimal - it's a varchar .
do any mathematical calculation only on decimal/numeric/float/int values.
SELECT convert(varchar(20), convert(decimal(10,2), sum(case when
tsr.other like '%aa%' then convert(decimal(10,2),tsr.block1) else 0
end) + sum(case when tsr.other like '%aa%' then
convert(decimal(10,2),tsr.block2) else 0 end) + sum(case when
tsr.other like '%aa%' then convert(decimal(10,2),tsr.block3) else 0
end) + sum(case when tsr.other like '%aa%' then
convert(decimal(10,2),tsr.block4) else 0 end)) * 450)