sql server management studio invalid column name where clause - sql

I am writing a query to filter a table by a certain field. However, the filter I am using is being evaluated as a column for some unknown reason.
i.e.
SELECT prn FROM CompanyPack WHERE prn = "212"
In this query, SSMS 2012 throws an error in which it is telling me that "212" is an invalid column name, but it is not a column at all.
prn is a nvarchar(50).
Any advice?

Use single quotes '' not double quotes ". Like so
SELECT prn FROM CompanyPack WHERE prn = '212'

Related

ROUND function in ODBC

I am working on a third-party custom flat file DB that I access through ODBC and the ROUND function is throwing errors always.
Is there a function that can do rounding in ODBC?
An example that throws an error:
SELECT AUDIT_SPLIT.ACCOUNT_REF, ROUND(SUM(AUDIT_SPLIT.GROSS_AMOUNT), 2) FROM AUDIT_SPLIT GROUP BY AUDIT_SPLIT.ACCOUNT_REF
Though Excel the error is "Column not found"
So I will try something like this:
SELECT A.ACCOUNT_REF, ROUND(SUM(A.GROSS_AMOUNT), 2) GROSS_AMOUNT_SUM
FROM AUDIT_SPLIT A
WHERE A.ACCOUNT_REF IS NOT NULL AND A.GROSS_AMOUNT IS NOT NULL
GROUP BY 1
Is your flat file something like a csv, a delimited text file or a fixed length file?
In this case I will suggest to check for the last line is not empty.
If the file ends with a cr/lf a new line is added and it is filled with nulls.
Also, from which client are you running the query? You have not given a name to ROUND() column, maybe you need it.
So I will try something like this:
SELECT A.ACCOUNT_REF, ROUND(SUM(A.GROSS_AMOUNT), 2) AS GROSS_AMOUNT_SUM
FROM AUDIT_SPLIT A
WHERE A.ACCOUNT_REF IS NOT NULL AND A.GROSS_AMOUNT IS NOT NULL
GROUP BY A.ACCOUNT_REF
UPDATE
Maybe the problem is related to the comma present in ROUND function, because it is interpreted as a column separator, you can avoid it using ODBC escape clause {fn ROUND()} (as suggested by jarlh in his comment), and to be sure and clear I will try to split the SUM() and the ROUND() in this way:
SELECT S.ACCOUNT_REF, {fn ROUND(S.GROSS_AMOUNT_SUM, 2)} AS ROUNDED_SUM
FROM (
SELECT A.ACCOUNT_REF, SUM(A.GROSS_AMOUNT) AS GROSS_AMOUNT_SUM
FROM AUDIT_SPLIT AS A
WHERE A.ACCOUNT_REF IS NOT NULL AND A.GROSS_AMOUNT IS NOT NULL
GROUP BY A.ACCOUNT_REF
) AS S

SQL data type mismatch even after checking for numbers

I have the following sql statement:
SELECT SUM(temp.[ENDING TRUST BALANCE] * temp.[BORROWER FICO])
FROM (SELECT *
FROM [tape$A1:BR535]
WHERE ISNUMERIC([BORROWER FICO])) temp
WHERE temp.[BORROWER FICO] > 0
The nested sql statement should return a table that is all numeric. In fact, the * operation works however, when I add the WHERE clause I get the data type error. Any ideas?
UPDATE: got this working btw. for some reason blank string escaped this logic ie considered numbers. i just cleared those before running this and other scripts.

SSIS Precedence Constraint Not Working Due to Path Formatting

Using SSIS and MS-SQL Server 2012
I have a SQL Task executing:
SELECT COUNT(id) as id FROM PORG_Files WHERE filename = ?
It never returns anything except 0 because the SSIS filename looks like:
\\\\erp\\shares\\Save\\item_1168.txt
And the Filename in the Table Looks like:
\\erp\shares\Save\item_1168.txt
I don't think I want to insert the filename into the table like that, so how/where do I format so I can make the matches to get my constraint that depends on this to fire.
Thanks!
Ok, If I run this query in SQL Manager it works.
SELECT COUNT(id) as id FROM PORG_Files WHERE filename = REPLACE('\\\\erp\\shares\\Save\\item_1168.txt','\\','\')
When I put the equivilant into the SQL Task Editor for the SQLStatement, it still returns 0
SELECT COUNT(id) as id FROM PORG_Files WHERE filename = REPLACE(?,'\\','\')
Workaround - Expression
Try using expression instead of passing parameters:
In the Execute SQL Task, Go To Expression Tab, Add an expression for SQLStatementSource property as following:
"SELECT COUNT(id) as id FROM PORG_Files WHERE filename = '" + #[User::CurrentFileName] + "'"
Are you just stored the file name directly in a variable? If you store the file name as the expression of a string variable instead, the output format will what you described. The result of the 4 forward slashes (\) will will be only 2, and the 2 \ will be a single one. This is because the the forward slash must be escaped in an SSIS expression. In the Expression field of the variable, click the ellipsis and enter the text inside double quotes to make this an expression such as in the example below.
“\\\\erp\\shares\\Save\\item_1168.txt”

how to use substr in SQL Server?

I have the following extract of a code used in SAS and wanted to write it in SQL Server to extract data.
substr(zipname,1,4) in("2000","9000","3000","1000");run;
How do I write this in SQL Server ?
I tried and got this error:
An expression of non-boolean type specified in a context where a
condition is expected
In sql server, there's no substr function (it's substring)
by the way, you need a complete query...
select blabla
from blibli
where substring(zipname, 1, 4) in ('2000', '9000', 3000', '1000')
assuming zipname is a varchar or something like that...
You need a table that you are getting the records from, and zipname would be a column in the table. The statement would be something like this:
select * from tablename where substring(zipname,1,4) in ('2000','9000','3000','1000')
Since you want the first x characters, you can also use the left() function.
where left(zipname, 4) in (values go here)
Bear in mind that your values have to be single quoted. Your question has double quotes.

Why won't this where statement work?

Below is the full queory, when I remove grade = "a" it works fine, why does this cause it to error out?
I get error ORA-00904: "A": invalid identifier
select count(grade), dw_course_sect.roomID
from dw_course_facts, dw_course_sect
where (dw_course_facts.coursekey = dw_course_sect.coursekey) and
(dw_course_facts.grade = "A")
group by dw_course_sect.roomID;
Oracle (and most other databases) use double quotes as delimiters for names in SQL. If you have a column name with a space, such as Column Name, then you would write select "Column Name" . . ..
When you write "A", then Oracle is looking for a named object with that name -- a column called "A". And there is none.
You want single quotes. But, you should also be using standard join syntax. The query should look like:
select count(grade), dw_course_sect.roomID
from dw_course_fact join
dw_course_sect
on dw_course_facts.coursekey = dw_course_sect.coursekey
where dw_course_facts.grade = 'A'
group by dw_course_sect.roomID;