I have this piece of code (please look below). I keep getting error: "Invalid column name 'SuppFinish2'
SELECT
CASE
WHEN [RegFinish] IS NULL THEN ''
ELSE [RegFinish]
END AS [RegFinish],
CASE
WHEN [SuppFinish] IS NULL THEN ''
ELSE [SuppFinish]
END AS [SuppFinish2],
CASE
WHEN [RegFinish]<[SuppFinish2] THEN '1'
ELSE '0'
END AS [TEST]
FROM TABLE
Is it because of [SuppFinish2] being an alias? Thanks!
As you said its due to alias and aliased columns can be referenced only on order by to logical query flow order
with cte
as
(
SELECT
CASE
WHEN [RegFinish] IS NULL THEN ''
ELSE [RegFinish]
END AS [RegFinish],
CASE
WHEN [SuppFinish] IS NULL THEN ''
ELSE [SuppFinish]
END AS [SuppFinish2]
FROM TABLE
)
select
CASE
WHEN [RegFinish]<[SuppFinish2] THEN '1'
ELSE '0'
END AS [TEST]
from cte
You cant use those aliases in the same level as you created them, becuase they are not existing yet.. wrap your query with another select like this:
SELECT * ,
CASE
WHEN [RegFinish]<[SuppFinish2] THEN '1'
ELSE '0'
END AS [TEST]
FROM (
SELECT
[ID],
CASE
WHEN [RegFinish] IS NULL THEN ''
ELSE [RegFinish]
END AS [RegFinish],
CASE
WHEN [SuppFinish] IS NULL THEN ''
ELSE [SuppFinish]
END AS [SuppFinish2],
FROM TABLE)
In order to reference aliased columns, you can use a derived table (or CTE, but that is not shown here)
Select *, CASE
WHEN [RegFinish]<[SuppFinish2] THEN '1'
ELSE '0'
END AS [TEST] From
(
SELECT
CASE
WHEN [RegFinish] IS NULL THEN ''
ELSE [RegFinish]
END AS [RegFinish],
CASE
WHEN [SuppFinish] IS NULL THEN ''
ELSE [SuppFinish]
END AS [SuppFinish2]
) T1
FROM TABLE
You cannot, at the same time, set and access an alias in the SELECT clause. I would suggest rewriting your query using CROSS APPLY:
SELECT t1.[RegFinish],
t2.[SuppFinish],
CASE
WHEN t1.[RegFinish] < t2.[SuppFinish] THEN '1'
ELSE '0'
END AS [TEST]
FROM TABLE
CROSS APPLY (SELECT COALESCE([RegFinish], '') AS [RegFinish]) AS t1
CROSS APPLY (SELECT COALESCE([SuppFinish], '') AS [SuppFinish]) AS t2
SELECT ISNULL([RegFinish],'') as [RegFinish]
, ISNULL([SuppFinish],'') as [SuppFinish2], CASE
WHEN
ISNULL([RegFinish],'') < ISNULL([SuppFinish],'') THEN 1
ELSE 0
END AS [TEST]
FROM TABLE
Why not use ISNULL instead of CASE? The problem with your query is that [SuppFinish2] is an alias not an column and can only be used in ORDER BY clause
Related
Instead of below query what I can use to convert sql data to json object.
CREATE FUNCTION [dbo].[ES_GetSchools](#IdListing NVARCHAR(400))
RETURNS NVARCHAR(MAX)
AS
BEGIN
RETURN(
SELECT
CASE WHEN ISNULL(SchoolDistrictId,'')<>'' THEN
SUBSTRING(SchoolDistrictId,1,CHARINDEX(':',SchoolDistrictId)-1)ELSE NULL END AS 'district.name',
CASE WHEN ISNULL(SchoolDistrictId,'')<>'' THEN
SUBSTRING(SchoolDistrictId,CHARINDEX(':',SchoolDistrictId)+1,LEN(SchoolDistrictId))ELSE NULL END AS 'district.id',
CASE WHEN ISNULL(SchoolDistrictSEOPath,'')<>'' AND SchoolDistrictSEOPath<>'0' THEN
SchoolDistrictSEOPath ELSE NULL end 'district.seo',
CASE WHEN ISNULL(SchoolElementary,'')<>'' THEN
CASE WHEN CHARINDEX(':',SchoolElementary)>0 THEN
SUBSTRING(SchoolElementary,1,CHARINDEX(':',SchoolElementary)-1) ELSE SchoolElementary END ELSE NULL END AS 'elementary.name',
CASE WHEN ISNULL(SchoolElementary,'')<>'' THEN
CASE WHEN CHARINDEX(':',SchoolElementary)>0 THEN
SUBSTRING(SchoolElementary,CHARINDEX(':',SchoolElementary)+1,LEN(SchoolElementary)) ELSE NULL END ELSE NULL END AS 'elementary.id',
CASE WHEN ISNULL(SchoolHigh,'')<>'' THEN
CASE WHEN CHARINDEX(':',SchoolHigh)>0 THEN
SUBSTRING(SchoolHigh,1,CHARINDEX(':',SchoolHigh)-1)ELSE SchoolHigh END ELSE NULL END AS 'high.name',
CASE WHEN ISNULL(SchoolHigh,'')<>'' THEN
CASE WHEN CHARINDEX(':',SchoolHigh)>0 THEN
SUBSTRING(SchoolHigh,CHARINDEX(':',SchoolHigh)+1,len(SchoolHigh))ELSE NULL END ELSE NULL END AS 'high.id',
CASE WHEN ISNULL(SchoolMiddle,'')<>'' THEN
CASE WHEN CHARINDEX(':',SchoolMiddle)>0 THEN
SUBSTRING(SchoolMiddle,1,CHARINDEX(':',SchoolMiddle)-1)ELSE SchoolMiddle END ELSE NULL END AS 'middle.name',
CASE WHEN ISNULL(SchoolMiddle,'')<>'' THEN
CASE WHEN CHARINDEX(':',SchoolMiddle)>0 THEN
SUBSTRING(SchoolMiddle,CHARINDEX(':',SchoolMiddle)+1,len(SchoolMiddle))ELSE NULL END ELSE NULL END AS 'middle.id'
FROM Table_Name1 a WITH (NOLOCK)
WHERE a.IdListing = #IdListing
FOR JSON PATH,WITHOUT_ARRAY_WRAPPER
)
END
GO
SELECT dbo.[ES_GetSchools](Idlisting) from Table_Name1 a join Table_Name2 b on a.idlisting=b.idlisting
Use the following syntax
SELECT field1, field2
FROM table
FOR JSON AUTO
I want to write a CASE statement to find the data between two dates based on #sFRomDate empty or not. Given below query not working as per my requirement. Please help me to find a proper solution
SELECT
*
FROM
tbl_emp_data
WHERE
CASE
WHEN #sFRomDate!=''
THEN SubmissionDate BETWEEN #sFRomDate AND DATEADD(DAY,1,#sToDate)
ELSE
SubmissionDate = NULL
END
This might be the logic you were trying to implement. In the event that #sFRomDate be NULL or empty and SubmissionDate also be NULL or empty the record will be returned. Otherwise, the SubmissionDate will be checked to make sure it is within the range you defined.
SELECT *
FROM tbl_emp_data
WHERE (COALESCE(#sFRomDate, '') = '' AND
COALESCE(SubmissionDate, '') = '') OR
(COALESCE(#sFRomDate, '') <> '' AND
COALESCE(SubmissionDate, '') <> '' AND
SubmissionDate BETWEEN #sFRomDate AND DATEADD(DAY, 1, #sToDate))
If I understand what you want, I think it becomes simpler to follow the logic if you write:
IF #sFromDate = ''
BEGIN
SELECT * FROM tbl_emp_data where SubmissionDate=null
END
ELSE
BEGIN
SELECT * FROM tbl_emp_data where SubmissionDate between #sFRomDate and DATEADD(DAY,1,#sToDate)
END
But as #Tim Biegeleisen says, be careful with nulls, not just for #sFromDate but also #sToDate
I am trying to MERGE values from one table to another. One of the values is a conditional value, but it looks like I am not getting the syntax correctly. Initially, I was using an IF-THEN-ELSE statement but was advise to use a CASE statement instead.
Here is the gist the syntax that is failing:
CREATE PROCEDURE EmployeeMerge
AS
BEGIN
SET NOCOUNT ON;
MERGE INTO Employee AS t1
USING
(SELECT
EmployeeName,
Zip,
UpdateDate
FROM table2) AS t2
ON (t1.EmployeeID = t2.EmployeeID)
WHEN MATCHED AND t2.UpdatedDate > t1.UpdatedDate THEN
UPDATE
SET
t1.EmployeeName = s.EmployeeName,
t1.Zip =
(CASE
WHEN t2.ZipExt IS NULL OR t2.ZipExt = '' THEN t2.Zip
ELSE (t2.Zip + '-' + t2.ZipExt)
END),
t1.UpdatedDate = t2.UpdateDate
WHEN NOT MATCHED THEN
INSERT (EmployeeName,
Zip,
ModDate)
VALUES
(t2.Name,
(CASE
WHEN t2.ZipExt IS NULL OR t2.ZipExt = '' THEN t2.Zip
ELSE (t2.Zip + '-' + t2.Zip_Ext)
END),
t2.UpdatedDate)
OUTPUT
deleted.*,
$action,
inserted.*
END; **-- A MERGE statement must be terminated by a semi-colon (;)**
GO
This MERGE statement works just fine if I do not implement the condition, i.e. simply set the t1.Zip = t2.Zip, but of course, this is avoiding the t2.ZipExt field.
A MERGE statement must be terminated by a semi-colon (;)
You haven't terminated the MERGE with a semicolon. You have terminated BEGIN-END. Move the semicolon.
I never really cared for the merge command. There are times where I can see using it, but for the most part, it's more complicated than I like my SQL.
UPDATE e
SET e.EmployeeName=t1.EmployeeName
, e.Zip=CASE
WHEN t1.ZipExt IS NULL OR t1.ZipExt = '' THEN t1.Zip
ELSE (t1.Zip + '-' + t1.ZipExt)
END
, e.UpdatedDate=t1.UpdatedDate
FROM Employee e
INNER JOIN Table t1 ON e.EmployeeID = t1.EmployeeID
WHERE t1.UpdatedDate > e.UpdatedDate
INSERT INTO Employee (EmployeeName,Zip,UpdatedDate)
SELECT
t1.EmployeeName
, t1.Zip=CASE
WHEN t1.ZipExt IS NULL OR t1.ZipExt = '' THEN t1.Zip
ELSE (t1.Zip + '-' + t1.ZipExt)
END
, t1.UpdatedDate
FROM Table t1
LEFT JOIN Employee e ON e.EmployeeID = t1.EmployeeID
WHERE e.EmployeeID IS NULL
I am writing a CASE statement in my SQL Server Stored Procedure. There I am repeeating a same long SQL statements every time for 11 CASEs. Should I put the SQL Statement in CASE condition in a another Stored Procedure? What could be the best approach?
CASE (SELECT ..................)
THEN 'SELCET a.field'
ELSE
''SELECT vlaues'
END
as 'Coulmn1 '
CASE (SELECT ..................)
THEN 'SELCET vaues'
ELSE
''SELECT vlaues'
END
as 'Coulmn1 '
You might want to do something like
SELECT
CASE (your select statement which retuns one value)
WHEN 'option1' THEN 'value2return1'
WHEN 'option2' THEN 'value2return2'
WHEN 'option3' THEN 'value2return3'
...
ELSE 'defaultValue'
END
Use common table expressions link.
WITH CTE (ColA)
AS
(
Select ColA From Table
)
SELECT ColA
FROM CTE
Use a CTE. In the future it would help if you gave us a slightly better example of your code however.
WITH RepeateSelect AS (SELECT ..................)
.....
.....
CASE (SELECT * FROM RepeateSelect)
THEN 'SELCET a.field'
ELSE
'SELECT vlaues'
END
as 'Coulmn1 '
CASE (SELECT * FROM RepeateSelect)
THEN 'SELCET vaues'
ELSE
''SELECT vlaues'
END
as 'Coulmn1 '
Try using the repeated query once thusly:
select A.C1, X.C2, X.C3
from TableA as A inner join
( select B.AlmondJoy,
case when B.YN > 7 then 'Yes' else 'No' end as C2,
case when C.UD in ( -1, 1 ) then 'Up' else 'Down' end as C3
from TableB as B inner join
TableC as C on C.Id = B.Id
) as X on X.AlmondJoy = A.Zagnut
My question is a bit more generic, but let's say I want to test some expression in SQL Server. I write SELECT 1=1 or SELECT 'a' > 'B' and press F5, hoping to see the result, like I do when I type SELECT 0, 1.
But I get an error instead. Why is that? What should I use to evaluate those expressions on the fly?
SQL Server doesn't have a boolean data type.
You would need to use SELECT CASE WHEN 1=1 THEN 1 ELSE 0 END
Simplest way is to select 1 where <test expression here>
You could put your expression after where like this
select 'true' where 1=1
select 'true' where 1<>1
of you could put it in an IF statement
IF 1+1=2
BEGIN
PRINT 'One and one makes two.'
END
You could use a case statement:
select case when 1=1 then 'true' else 'false' end
select case when 'a'>'B' then 'true' else 'false' end
or an IF...ELSE
if 1=1
select 'true'
else
select 'false'
if 'a' > 'B'
select 'true'
else
select 'false'
You can use the following to turn the output of the comparison to pseudo-boolean:
select isnull((select 'false' where not(1**0=1**)) ,'true')
or, to have an int as return:
select isnull((select 1 where 1<1) ,0)