Getting invalid column name in SQL/Excel - sql-server

I will preface this by saying I am nowhere near being an expert in SQL. Using Excel I am trying to use one specific cell as the input to query, but I run into a problem where a column I created isn't defined as a column. Please help.
SELECT
CASE
WHEN CHARINDEX(',', TCPIPADDRESS) > 0 THEN LEFT(TCPIPADDRESS, CHARINDEX(',', TCPIPADDRESS) - 1) ELSE TCPIPADDRESS END AS IPADDRESS,
ADMachine.ADMachineName, ADMachine.SerialNumber,
ADMachine.OperatingSystem, ADUsers.ADUser, ADUsers.ADDisplayName, employee_data.employee_first_name,
employee_data.employee_last_name, asset_center.LOCATION_SITENAME, asset_center.TCPIPHOSTNAME,
asset_center.MAC_ADDRESS, ADUsers.ADUserOU
FROM PC_GAP.dbo.ADMachine ADMachine, PC_GAP.dbo.ADUsers ADUsers, PC_GAP.dbo.asset_center asset_center, PC_GAP.dbo.employee_data employee_data
WHERE ADUsers.ADUser = employee_data.employee_user_name AND ADMachine.SerialNumber = asset_center.SERIALNO AND ADUsers.ADUser = asset_center.LAST_LOGGED_ON_USER
The IPAddress at the end is where the problem lies.
Edit 1: Added the additional information from the SQL statement to paint the whole picture (originally left out irrelevant data)

Alright so first off you need to stop using the old style joins for all of the reasons listed here.
You are also going to run into an issue that you aren't joining on anything other than one of your tables. I'm pretty sure this will not give you the results that you are looking for.
Then finally you need to think about the order of operations. Since the WHERE clause is evaluated before the the select you cannot refer to your alias in the where clause. You can only reference an alias with an ORDER BY or by using a subquery or a cte.
You can however use your case expression in your where clause. The example would be as follows.
CASE
WHEN CHARINDEX(',', TCPIPADDRESS) > 0
THEN LEFT(TCPIPADDRESS, CHARINDEX(',', TCPIPADDRESS) - 1)
ELSE TCPIPADDRESS
END = ?
As stated previously you could also turn this entire thing into a subquery. I would use your code as an example for that but I'm not entirely sure what exactly you're hoping to accomplish with your current set of joins so my example will be a bit generic.
select
GenericColumn
(select
blah as Pity,
GenericColumn
from dbo.TheFoo)
where Pity = SeachCondition

You can't use a column alias in the WHERE clause. You have to use the whole formula that you aliased.
WHERE ADMachine.SerialNumber = asset_center.SERIALNO AND ((CASE
WHEN CHARINDEX(',', TCPIPADDRESS) > 0 THEN LEFT(TCPIPADDRESS, CHARINDEX(',', TCPIPADDRESS) - 1) ELSE TCPIPADDRESS END=?))
To illustrate Aaron's suggestion, it would look more like this:
WITH cte AS (
SELECT
CASE
WHEN CHARINDEX(',', TCPIPADDRESS) > 0 THEN LEFT(TCPIPADDRESS, CHARINDEX(',', TCPIPADDRESS) - 1) ELSE TCPIPADDRESS END AS IPADDRESS,
ADMachine.ADMachineName,
ADMachine.OperatingSystem,
asset_center.MAC_ADDRESS,
FROM PC_GAP.dbo.ADMachine ADMachine, PC_GAP.dbo.ADUsers ADUsers, PC_GAP.dbo.asset_center asset_center, PC_GAP.dbo.employee_data employee_data
)
SELECT * FROM cte
WHERE ADMachine.SerialNumber = asset_center.SERIALNO AND ((IPAddress=?))

Related

if a number in column is negative,show positive,else show the number

I have a [Preko]column in a query that is calculating the difference between two columns.If the number is negative,I need to show it positive,and if it is positive,it should stay the same.I can't use ABS in this one.I tried with the case,but it didn't work.
The problem I am getting is that column Preko is invalid.
This is the code for my second try with iif:
SELECT FP.Firma
,FP.NazFirme
,FP.Konto
,FP.NazivKonta
,FP.Partner
,FP.NazivPartnera
,Sum(FP.Duguje) AS dug
,Sum(FP.Potrazuje) AS pot
,Sum(IIf([FP].[Konto] Like '2*'
,[duguje]-[potrazuje]
,[potrazuje]-[duguje])) AS USaldo
,Sum(IIf([datumval]<= '1.1.2017'
,IIf([FP].[Konto] Like '2*'
,[duguje]-[potrazuje]
,[potrazuje]-[duguje]),0)) AS [Preko]
,IIf([Preko]<0,0,[Preko]) AS Preko1
FROM tblFinansijskiPodaci FP
Where FP.Firma = 1
AND FP.Partner=1110
GROUP BY FP.Firma
,FP.NazFirme
,FP.Konto
,FP.NazivKonta,
,FP.Partner
,FP.NazivPartnera
HAVING (((FP.Konto)=2040))
You can also use a case statement instead of the iif section.
CASE WHEN Preko<0 THEN 0 ELSE Preko END
or
CASE WHEN Preko<0 THEN -Preko ELSE Preko END
That seems to be more in line with your logic depending on how you want to handle the negatives.
Agree with HoneyBadger - ABS is the way to go ABS(-1) returns 1. Have a look at the APEX SQL tools for a quick formatting option. It's free and makes your code a lot easier to read, which means you'll find you get more answers.
WITH cte AS (
SELECT FP.Firma
,FP.NazFirme
,FP.Konto
,FP.NazivKonta
,FP.Partner
,FP.NazivPartnera
,Sum(FP.Duguje) AS dug
,Sum(FP.Potrazuje) AS pot
,Sum(IIf([FP].[Konto] Like '2*'
,[duguje]-[potrazuje]
,[potrazuje]-[duguje])) AS USaldo
,Sum(IIf([datumval]<= '1.1.2017'
,IIf([FP].[Konto] Like '2*'
,[duguje]-[potrazuje]
,[potrazuje]-[duguje]),0)) AS [Preko]
FROM tblFinansijskiPodaci FP
Where FP.Firma = 1
AND FP.Partner=1110
GROUP BY FP.Firma
,FP.NazFirme
,FP.Konto
,FP.NazivKonta,
,FP.Partner
,FP.NazivPartnera
HAVING (((FP.Konto)=2040))
)
SELECT *, CASE WHEN Preko<0 THEN 0 ELSE Preko END preko1 FROM cte

SQL Server - How to Use Merge Statement for Slowly Changing Dimension with More Than Two Conditions?

I am trying to implement Slowly Changing Dimension Type 2 through T-SQL but I can't figure out how to put a request to work.
Table columns: cpf, nome, telefone_update, endereco_insert
Basically the logic is: if the MATCH doesn't happen using cpf, then the record must be inserted; if the MATCH happens but only the telefone_update field has changed, there is no need to another record and I just want to UPDATE and override the values; if the MATCH happens but only the endereco_insert field has changed I want to add a new record and update the start and end dates.
What I have so far is:
insert into #dm_lucas_tst (
[cpf],
[nome],
[telefone_update],
[endereco_insert],
[dt_scd_start],
[dt_scd_end],
[nu_scd_version]
)
select [cpf],
[nome],
[telefone_update],
[endereco_insert],
cast(dateadd(month, datediff(month, 0, getdate()), 0) as date) as [dt_scd_start],
'2199-12-31' AS [dt_scd_end],
1 AS [nu_scd_version]
from (
merge edw.dim.dm_lucas_tst as Target
using edw.dim.stg_lucas_tst as Source
on Target.cpf = Source.cpf
when not matched by target
then
insert (
[cpf],
[nome],
[telefone_update],
[endereco_insert],
[dt_scd_start],
[dt_scd_end],
[nu_scd_version]
)
values (
Source.[cpf],
Source.[nome],
Source.[telefone_update],
Source.[endereco_insert],
cast(dateadd(month, datediff(month, 0, getdate()), 0) as date),
'2199-12-31',
1
)
when matched
and Source.telefone_update <> Target.telefone_update
and Target.dt_scd_end = '2199-12-31'
then
update set telefone_update = Source.telefone_update
output $ACTION ActionOut,
Source.[cpf],
Source.[nome],
Source.[telefone_update],
Source.[endereco_insert]
) AS MergeOut
where MergeOut.ActionOut = 'UPDATE';
But I don't think putting another WHEN MATCH AND ... will make that work.
Any suggestions?
Thanks in advance!
Accordingly to your description, I'm assuming that you need:
SCD Type 1 for column [telefone_update]
SCD Type 2 for column [endereco_insert]
I've used application SCD Merge Wizard to easily create described logic.
When I made tests for it - everything looks as expected, I guess.
I described the process on my blog - please have a look and tell me if that was exactly what you wanted?
https://sqlplayer.net/2018/01/scd-type-1-type-2-in-merge-statement/

Query fails on "converting character string to smalldatetime data type"

I've been tasked with fixing some SQL code that doesn't work. The query reads from a view against a predicate. The query right now looks like so.
SELECT TOP (100) Beginn
FROM V_LLAMA_Seminare
//Removal of the following line makes the query successful, keeping it breaks it
where Beginn > (select cast (getdate() as smalldatetime))
order by Beginn desc
When I run the above query, I am greeted with the following error.
Msg 295, Level 16, State 3, Line 1
Conversion failed when converting character string to smalldatetime data type.
I decided to remove the WHERE clause, and now it runs returning 100 rows.
At first, I thought that behind the scenes, SQL Server was somehow including my predicate when bringing back the View . But then I investigated how the View was being created, especially the Beginn field, and at no point does it return a String.
Long story short, the column that becomes the Beginn field is a BIGINT timestamp like 201604201369.... The original user transforms this BIGINT to a smalldatetime using the following magic.
....
CASE WHEN ma.datum_dt = 0
THEN null
ELSE CONVERT(smalldatetime, SUBSTRING(CAST(ma.datum_dt AS varchar(max)),0,5) + '-' +
SUBSTRING(CAST(ma.datum_dt AS varchar(max)),5,2) + '-' +
SUBSTRING(CAST(ma.datum_dt AS varchar(max)),7,2) + ' ' +
SUBSTRING(CAST(ma.datum_dt AS varchar(max)),9,2) +':'+
SUBSTRING(CAST(ma.datum_dt AS varchar(max)),11,2) +':' +
RIGHT(CAST(ma.datum_dt AS varchar(max)),2)) END AS Beginn
...
My last attempt at finding the problem was to query the view and run the function ISDATE over the Beginn column and see if it returned a 0 which it never did.
So my question is two fold, "Why does a predicate break something" and two "Where on earth is this string error coming from when the Beginn value is being formed from a BIGINT".
Any help is greatly appreciated.
This problem is culture related...
Try this and then change the first SET LANGUAGE to GERMAN
SET LANGUAGE ENGLISH;
DECLARE #bi BIGINT=20160428001600;
SELECT CASE WHEN #bi = 0
THEN null
ELSE CONVERT(datetime, SUBSTRING(CAST(#bi AS varchar(max)),0,5) + '-' +
SUBSTRING(CAST(#bi AS varchar(max)),5,2) + '-' +
SUBSTRING(CAST(#bi AS varchar(max)),7,2) + ' ' +
SUBSTRING(CAST(#bi AS varchar(max)),9,2) +':'+
SUBSTRING(CAST(#bi AS varchar(max)),11,2) +':' +
RIGHT(CAST(#bi AS varchar(max)),2)) END AS Beginn
It is a very bad habit to think, that date values look the same everywhere (Oh no, my small application will never go international ...)
Try to stick to culture independent formats like ODBC or ISO
EDIT
A very easy solution for you actually was to replace the blank with a "T"
SUBSTRING(CAST(ma.datum_dt AS varchar(max)),7,2) + 'T' +
Then it's ISO 8601 and will convert...
The solution was found after looking through #Shnugo's comment. When I took my query which contained the Bigint->Datetime conversion logic, and put it into a CTE with "TOP 100000000" to avoid any implicit conversion actions, my query worked. Here is what my view looks like now with some unimportant parts omitted.
---Important part---
CREATE VIEW [dbo].[V_SomeView] AS
WITH CTE AS (
SELECT TOP 1000000000 ma.id AS MA_ID,
---Important part---
vko.extkey AS ID_VKO,
vko.text AS Verkaufsorganisation,
fi.f7000 AS MDM_Nr,
vf.f7105 AS SAPKdnr,
CASE WHEN ma.datum_dt = 0 --Conversion logic
CASE WHEN ma.endedatum_dt = 0 --Conversion logic
CONVERT(NVARCHAR(MAX),art.text) AS Art,
.....
FROM [ucrm].[dbo].[CRM_MA] ma,
[ucrm].[dbo].[CRM_fi] fi,
[ucrm].[dbo].[CRM_vf] vf,
[ucrm].[dbo].[CRM_ka] vko,
[ucrm].[dbo].[CRM_ka] art,
[ucrm].[dbo].[CRM_ka] kat
where ma.loskz = 0
and fi.loskz = 0
and vf.loskz = 0
and fi.F7029 = 0
and vf.F7023 = 0
...
GROUP BY ma.id,
vko.extkey,
vko.text,
fi.f7000 ,
vf.f7105,
ma.datum_dt,
ma.endedatum_dt,
....
)
select * FROM CTE;

Yet another subquery issue

Hello from an absolute beginner in SQL!
I have a field I want to populate based on another table. For this I have written this query, which fails with: Msg 512, Level 16, State 1, Line 1
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
The statement has been terminated.
oK, here goes:
Update kre.CustomerOrderLineCopy
SET DepNo = (SELECT customerordercopy.DepNo
FROM kre.CustomerOrderCopy , kre.CustomerOrderLineCopy
WHERE CustomerOrderLineCopy.OrderCopyNo =kre.CustomerOrderCopy.OrderCopyNo)
WHERE CustomerOrderLineCopy.OrderCopyNo = (SELECT CustomerOrderCopy.OrderCopyNo
FROM kre.CustomerOrderCopy, kre.CustomerOrderLineCopy
WHERE kre.CustomerOrderLineCopy.OrderCopyNo = kre.CustomerOrderCopy.OrderCopyNo)
What I'm trying to do is to change DepNo in CustomerOrderLineCopy, with the value in DepNo in CustomerOrderCopy - based on the same OrderCopyNo in both tables.
I'm open for all suggestion.
Thanks,
ohalvors
If you just join the tables together the update is easier:
UPDATE A SET A.DepNo = B.DepNo
FROM kre.CustomerOrderLineCopy A
INNER JOIN kre.CustomerOrderCopy B ON A.OrderCopyNo = B.OrderCopyNo
The problem is that at least one of your sub queries return more than one value. Think about this:
tablePerson(name, age)
Adam, 11
Eva, 11
Sven 22
update tablePerson
set name = (select name from tablePerson where age = 11)
where name = 'Sven'
Which is equivalent to: set Sven's name to Adam and Eva. Which is not possible.
If you want to use sub queries, either make sure your sub queries can only return one value or force one value by using:
select top 1 xxx from ...
This may be enough to quieten it down:
Update kre.CustomerOrderLineCopy
SET DepNo = (SELECT customerordercopy.DepNo
FROM kre.CustomerOrderCopy --, kre.CustomerOrderLineCopy
WHERE CustomerOrderLineCopy.OrderCopyNo =kre.CustomerOrderCopy.OrderCopyNo)
WHERE CustomerOrderLineCopy.OrderCopyNo = (SELECT CustomerOrderCopy.OrderCopyNo
FROM kre.CustomerOrderCopy --, kre.CustomerOrderLineCopy
WHERE kre.CustomerOrderLineCopy.OrderCopyNo = kre.CustomerOrderCopy.OrderCopyNo)
(Where I've commented out kre.CustomerOrderLineCopy in the subqueries) That is, you were hopefully trying to correlate these subqueries with the outer table - not introduce another instance of kre.CustomerOrderLineCopy.
If you still get an error, then you still have multiple rows in kre.CustomerOrderCopy which have the same OrderCopyNo. If that's so, you need to give us (and SQL Server) the rules that you want to apply for how to select which row you want to use.
The danger of switching to the FROM ... JOIN form shown in #Avitus's answer is that it will no longer report if there are multiple matching rows - it will just silently pick one of them - which one is never made clear.
Now I look at the query again, I'm not sure it even needs a WHERE clause now. I think this is the same:
Update kre.CustomerOrderLineCopy
SET DepNo = (
SELECT customerordercopy.DepNo
FROM kre.CustomerOrderCopy
WHERE CustomerOrderLineCopy.OrderCopyNo = kre.CustomerOrderCopy.OrderCopyNo)

Using a bit input in stored procedure to determine how to filter results in the where clause

I'm beating my head against the wall here... can't figure out a way to pull this off.
Here's my setup:
My table has a column for the date something was completed. If it was never completed, the field is null. Simple enough.
On the front end, I have a checkbox that defaults to "Only show incomplete entries". When only pulling incomplete entries, it's easy.
SELECT
*
FROM Sometable
WHERE Completed_Date IS NULL
But offering the checkbox option complicates things a great deal. My checkbox inputs a bit value: 1=only show incomplete, 0=show all.
The problem is, I can't use a CASE statement within the where clause, because an actual value uses "=" to compare, and checking null uses "IS". For example:
SELECT
*
FROM Sometable
WHERE Completed_Date IS <---- invalid syntax
CASE WHEN
...
END
SELECT
*
FROM Sometable
WHERE Completed_Date =
CASE WHEN #OnlyIncomplete = 1 THEN
NULL <----- this translates to "WHERE Completed_Date = NULL", which won't work.. I have to use "IS NULL"
...
END
Any idea how to accomplish this seemly easy task? I'm stumped... thanks.
...
WHERE #OnlyIncomplete = 0
OR (#OnlyIncomplete = 1 AND Completed_Date IS NULL)
Hmmm... I think what you want is this:
SELECT
*
FROM Sometable
WHERE Completed_Date IS NULL OR (#OnlyIncomplete = 0)
So that'll show Date=NULL plus, if OnlyIncomplete=0, Date != Null. Yeah, I think that's it.
If you still want to use a CASE function (although it may be overkill in this case) :
SELECT
*
FROM Sometable
WHERE 1 =
(CASE WHEN #OnlyIncomplete = 0 THEN 1
WHEN #OnlyIncomplete = 1 AND Completed_Date IS NULL THEN 1
END)

Resources