prosgres sql of using limit and fetch - sql-server

There are many solutions to the above questions from which i checked and tried but mine is always giving an error
I had some code converted from postgres to sql server but facing some issues
select * FROM errors OFFSET 0 ROWS FETCH NEXT 10 ROWS ONLY
the above in sql server
converted from posgres as:
select * from errors offset #start# limit #total#; works
but not in sql server, what should i do there

In SQL Server, OFFSET...FETCH is part of the ORDER BY clause. Ordering is needed with OFFSET because result set order is undefined without ORDER BY; the same query could return different results even though the underlying data is the same.
Below is example syntax, guessing an an appropriate column name.
SELECT *
FROM Errors
ORDER BY ErrorTimeStamp
OFFSET 0 ROWS FETCH NEXT 10 ROWS ONLY;

Related

SQL Server Determining Hard Coded Date as Larger When It's Not?

An old employee left a massive query behind that I've been debugging and it appears that the issue has come down to SQL Server itself determining a comparison differently than what I would have expected.
I have a table with a column col1 containing the value 20191215 as a datetime.
The part in question is similar to the following:
select case when col1 > '01/01/2020' then 1 else 0 end
This statement is returning 1, suggesting that '12/15/2019' is larger than '01/01/2020'.
I do not need assistance correcting the query, as I have already made changes to do so other than using the comparison the previous employee was using, I am simply curious as to why SQL Server would evaluate this as I have described.
I understand that this is not the typically way SQL Server would store dates as well, would the issue simply be the formatting of the dates?
Current SQL Server version is: SQL Server 2014 SP3 CU3.
SQL Fiddle link that shows the same results
Please note that the link does not contain an exact replica of my case
Edit: Included additional info relevant to actual query.
It is a string comparison not a date comparison:
select case when '12/15/2019' > '01/01/2020' then 1 else 0 end
vs
select case when CAST('12/15/2019' AS DATE) > CAST('01/01/2020' AS DATE) then 1 else 0 end
db<>fiddle demo
I am simply curious as to why SQL Server would evaluate this as I have described.
'12/15/2019' it is a string literal, SQL Server does not know you want to treat a date unless you explicitly express your intention.
I have a table with a column col1 containing the value 20191216
If you are comparing with a column then the data type of column matters and data type precedence rules

How to convert rows of data into a single row through informatica/SQL Server

How to convert diagonal rows into single row in SQL Server 2014 for a particular ID field. Any help would be greatly appreciated
Please review.
My source is SQL Server 2014
Note:
I don't have write access to DB so will not be able to create functions etc.
I have option to get the desired o/p either by taking the source[given below] in Informatica from SQL Server - which is in turn result of a query and achieve the required o/p by applying some transformation or write logic in SQL server -2014. So in short either i can perform logic using Informatica or through SQL Server in the initial level itself to get the desired o/p.
I have several other joins and columns being pulled from different tables along with the below fields
Please review
Regarding Input:
The ID field will be constant, but POS field will be different for a particular ID
There can be 1 to 10 such occurrences of POS field [POS field can have values from 1 -10].DESC1 value from POS =1 will go to DESC1, value of POS 2 will go to desc2 and so on.
Right now I have given only 8 occurrences,but actually there are 10
INPUT:
ID|POS|DESC1|desc2|desc3|desc4|desc5|desc6|desc7|desc8|
1|1|ItemA|null|null|null|null|null|null|null
1|2|null|ItemB|null|null|null|null|null|null
1|3|null|null|Item C|null|null|null|null|null
1|4|null|null|null|ItemD|null|null|null|null
1|5|null|null|null|null|ItemE|null|null|null
1|6|null|null|null|null|null|value-random|null|null
1|7|null|null|null|null|null|null|Check!A|null
1|8|null|null|null|null|null|null|null|123456
OUTPUT:
ID|DESC1|desc2|desc3|desc4|desc5|Desc6|desc7|Desc8
1|ItemA|ItemB|Item C|ItemD|ItemE|value-random|Check!A|123456
This is simplified because I don't know what your current query looks like but you could use MAX() and GROUP BY id.
DEMO
SELECT
ID,
MAX(desc1) AS [desc1],
MAX(desc2) AS [desc2],
MAX(desc3) AS [desc3],
MAX(desc4) AS [desc4],
MAX(desc5) AS [desc5],
MAX(desc6) AS [desc6],
MAX(desc7) AS [desc7],
MAX(desc8) AS [desc8]
FROM dbo.YourTable
GROUP BY ID

SQL Server fetch next x number of rows with where clause

Microsoft SQL Server 2008 R2
I am running a large SQL select query that may take hours to complete. So I try to break the query results into smaller sets.
e.g return results 1-10,000 first, then 10,001 - 20000, and so on
I used below code, but it gave me error
SELECT *
FROM PP_ConsolidatedSalesView
WHERE financial_period = '2018-11'
ORDER BY id
OFFSET 10000 ROWS
FETCH NEXT 10000 ROWS ONLY
I use a loop to dynamically change the offset and fetch next values.
The error message is:
Incorrect syntax near 'OFFSET'
Does anyone have an idea why? And is there an alternative solution?
Can you please confirm the database compatibility level. Offset is present in SQL Server 2012. If database is 2008 compatbility mode, then keyword isnt available.
You can check it like below:
USE AdventureWorks2012;
GO
SELECT compatibility_level
FROM sys.databases WHERE name = 'AdventureWorks2012';
GO
More info here: Incorrect syntax near OFFSET command

SQL Server 2008 Express (shared hosting) Times Out When Changing Data Type on Field

I'm changing some bad design on a table. The field I'm trying to change holds IIS7 session id's which are long numbers. I'm trying to change the field from nvarchar(20) to int. There are 349,000 records in the table.
SQL Server Management Studio times out after 35 seconds. However, if I check the query timeout setting for the connection it is set at 600 seconds (and I can't change it either).
Is there another timeout setting that could be causing this problem?
Here's the error message I'm getting:
- Unable to modify table.
Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding.
I've been able to change several other tables with no problem. Of course, they had fewer rows.
This database is on a shared hosting package at Arvixe.com. Do you think this could be part of the problem?
Can you try to run a T-SQL script instead of doing this using the visual designer?
ALTER TABLE dbo.YourTable
ALTER COLUMN YourColumn INT
Now, this will only work if all rows are truly valid INT values! Otherwise, it'll bomb out at some point....
To check if all your rows are truly valid INT, you could run this query:
SELECT * FROM dbo.YourTable
WHERE ISNUMERIC(YourColumn) = 0
This will select all rows that are not valid numerics ... if you get rows here, you have a problem...

How to fix "domain error" in SQL Server 2005 when using LOG() function to get product of set

I have a inline select statement to calculate the product of the set of values.
Since SQL Server 2005 doesn't have a built in Product aggregate function, I am using LOG/EXP to get it.
My select statement is:
(select exp(sum(log(value))) from table where value > 0)
Unfortunately I keep getting the following error:
Msg 3623, Level 16, State 1, Line 1
A domain error occurred.
I've ensured that none of the values are zero or negative so I'm not really sure why this error is occurring. Does anyone have any ideas?
One of the features of the query planner introduced in SQL 2005 is that, in some circumstances where the table statistics indicate it will be more efficient, the WHERE clause of a statement will be processed after the SELECT clause.
(I can't find the Books On-Line reference for this right now).
I suspect this is what is happening here. You either need to exclude the rows where value = 0 before carrying out the calculation - the most reliable way being to store the rows you need in a temporary (#) table - or to modify your query to handle zero internally:
SELECT EXP(SUM(LOG(ISNULL(NULLIF(VALUE,0),1)))) AS result
FROM [table]
The NULLIF\ISNULL pair I have added to your query substitutes 1 for 0 - I think this will work, but you will need to test it on your data.

Resources