Except operation in TSQL returning error - sql-server

I need to apply an EXCEPT (same as MINUS in Oracle) operation in TSQL. I tried with a simple code
select * from Table1
except
select * from Table1 where calndr_dt between '2014-10-01' and '2014-10-10'`
Each sub query is executing fine. But when joined with EXCEPT, it is returning the following error message.
Msg 103010, Level 16, State 1, Line 1
Parse error at line: 2, column: 1: Incorrect syntax near 'except'.

Remove the ORDER BY clause from the first query, if you have this clause.

Related

Error with FROM clause containing a subquery

I was running this query in MSSQL:
SELECT * FROM (SELECT * FROM ABC)
It gives an error:
Msg 102, Level 15, State 1, Line 1
Incorrect syntax near ')'.
This same command runs just fine on a DB2 database. I know this query doesn't make any sense I was just testing functionality.
So, are there certain features, eg., SELECT in FROM clause, that are not supported in MSSQL that are supported in DB2?
You just need to give the subquery an alias like so:
SELECT * FROM (SELECT * FROM ABC) subTable
Which translates to:
SELECT * FROM (SELECT * FROM ABC) as subTable
The AS is optional.

How can I move one of two perfectly identical rows into a new table through a query?

MS SQL Server 2008
I Use following Query to filter duplicate rows into a single row
Query 1 - SELECT DISTINCT * FROM flatfile_old
But now i want to move those query results into a new table and i try following things
Query 2 - SELECT * INTO flatfile_new FROM Flatfile_old WHERE 1 = 2
Query 3 - INSERT INTO flatfile_new (SELECT DISTINCT * FROM flatfile_old)
But Query No 3 throws error like follows
Msg 156, Level 15, State 1, Line 1 Incorrect syntax near the keyword
'select'. Msg 102, Level 15, State 1, Line 1 Incorrect syntax near
')'.
Please help
Just remove the brackets ( ... ) like this:
INSERT INTO flatfile_new
SELECT DISTINCT * FROM flatfile_old

Query giving error

The below query is giving error, please help me.
declare #dateScorecard datetime
set #dateScorecard = convert(varchar, 4)+'/1/'+ convert(varchar,2013)
select #dateScorecard
select top 1 i_CurrentMonthColor from AK_ScoreCardDetails scr
where datediff(m, convert(Datetime, convert(varchar, 4)+'/1/'+ convert(varchar,2013)), #dateScorecard)
error details:
Msg 4145, Level 15, State 1, Line 5
An expression of non-boolean type specified in a context where a condition is expected, near ')'.
Two quick comments:
First, you don't need to use convert on those numbers--just put them right into your query, i.e.
set #dateScoreCard = convert('4/1/2013');
Also, in the 'where' clause, you are not comparing the result of the datediff to anything There should be a section after the datediff clause, i.e.:
where datediff(.....) > 2
for example. Your datediff function only gives you a number that is the difference of the two dates you gave it, in the terms you define, which is months here. Use that number to compare to some value you want to make your sql statement work for you.

SQL Server 2005 PIVOT Syntax Error

I receive the following error
Msg 156, Level 15, State 1, Line 10
Incorrect syntax near the keyword 'SELECT'.
Msg 102, Level 15, State 1, Line 10
Incorrect syntax near ')'.
When I run the following query
SELECT TOP 100 *
FROM
(
SELECT [TimeStamp],[MeterID],[Value]
FROM access_AMIData) AS source
PIVOT
(
SUM(Value)
FOR MeterID IN (SELECT MeterNumber FROM access_tblFcppPvMeterList)
) as pvt
ORDER BY TimeStamp
The error comes with this line:
FOR MeterID IN (SELECT MeterNumber FROM access_tblFcppPvMeterList)
If I change this line to:
FOR MeterID IN (1,2,3,4)
It works perfectly...how can I specify a select Query inside the IN?
You can't, at least not in SQL 2005. You could include it in the WHERE clause of your inner query, but you have to hard-code the column names, unless of course you use dynamic SQL.

tsql : how do you query with dashes in column names?

I have the query
select * from products p, products_temp t
where p.ManufacturerPartNumber = t.[INV-PRICE-VENDOR-PART]
where the column names have dashes in them which SQL Server 2005 seems to automatically add brackets to. What is the correct way of accessing this in a query? I've tried with brackets and without the brackets and just end up with errors.
the error I get from sql mgmt studio is
Msg 156, Level 15, State 1, Line 2
Incorrect syntax near the keyword 'where'.
thanks in advance
that is because you have repeated WHERE twice in your statement. Got nothing to do with the square brackets which you will need because of the dashes.
use "current" join syntax:
SELECT
*
from products p
INNER JOIN products_temp t ON p.ManufacturerPartNumber = t.[INV-PRICE-VENDOR-PART]

Resources