SQL Server 'FETCH FIRST 1 ROWS ONLY' Invalid usage - sql

I am trying to convert a Db2 query to SQL Server, I came across a construct I am not familiar with: FETCH FIRST 1 ROWS ONLY.
This is the query working on db2:
select * from products.series where state = 'xxx' order by id
FETCH FIRST 1 ROWS ONLY
and the error I am getting on SQL Server:
Invalid usage of the option FIRST in the FETCH statement.
I have tried replacing FIRST with NEXT which seems to be admitted in SQL Server, but with no success.
I am using SQL Sever 2014

Try with OFFSET clause
select * from products.series where state = 'xxx' order by id
OFFSET 0 ROWS
FETCH NEXT 1 ROWS ONLY

use top:
select top 1 * from products.series where state = 'xxx' order by id

You can use top() finction'
select top 1 * from table

SELECT TOP 1 * FROM (select * from products.series where state = 'xxx') as tmp ORDER BY id

Related

Get 2nd from string in SQL

I have a requirement in SQL where I am getting a string and need to get the top 2nd row.
I am using SQL version 2014
I have a string which I am to spitting using the character 'M'
string is as follows: 3908K88513K1992K898593M
if I pass input: 88513 I need to get the output as 898593
Another example is
string: 24572K12345K10981K19809K
if the input is 19809 output should be: 12345
I have tried below code but it is failing at 2nd input example string:
SELECT TOP 1 T.*
FROM (
SELECT TOP 2 *
FROM splitfunction((
SELECT number
FROM name
WHERE id = 100709
), 'K')
) AS T
ORDER BY 1 DESC;
As Jarlh mentioned in the comments, instead of using TOP, use OFFSET and FETCH:
SELECT ss.* --This should be your columns
FROM dbo.contact c
CROSS APPLY dbo.dba_splitstring(c.structureno,'M') ss
WHERE c.recno = 100719
ORDER BY {????} --Your ORDER BY was missing in your query
OFFSET 1 ROWS FETCH NEXT 1 ROWS ONLY;
I've also reformulated your query, as all those subqueries weren't necessary.

I want to find the longest varchar in a specific column of a SQL Server table

Here's an example:
ID = INT IDENTITY
DESC = VARCHAR(5000)
ID | Desc
---|-----
1 | a
2 | aaa
3 | aa
The SQL query here should return 3 and the word itself i.e., aaa? Since the longest value is aaa with 3 characters?
The output should be:
aaa 3
You can use order by and limit the results to one row:
select description, len(description)
from t
order by len(description) desc
offset 0 row fetch first 1 row only;
Or use:
select top (1) description, len(description)
from t
order by len(description) desc;
With OFFSET & FETCH:
OFFSET is specifying how many rows from the top of the result set you will ignore ...
And then the FETCH is used to limit the result to the number of rows you specify.
The combination of this two will return the desired result because the result set is ordered by len(descr).
select descr, len(descr)
from test
order by len(descr) desc
OFFSET 0 ROWS
fetch first 1 rows only;
With TOP:
TOP will limit the reultset to the number of rows you specify. In this example 1. Because it is ordered by the lenght of the column desc then the first row will be the one you are looking for.
select top 1 descr, len(descr)
from test
order by len(descr) desc
HERE IS A DEMO for both examples
Note for the end:
Use aliases (I will demonstrate on one of this two examples)
select descr as Description, len(descr) "Description length"
from test
order by len(descr) desc
OFFSET 0 ROWS
fetch first 1 rows only;
You can put them between double quotes or not, use keyword as or not, it is up to you but the result looks better with them.
Both ROW and ROWS keyword is ok for OFFSET and FETCH clause. So what ever you want to use you can...
You can try TOP as shown below. Here is the official documentation of TOP (Transact-SQL).
Select
top 1 Id, description, len(description) as MaxLength
from YourTable order by len(description) desc
Live Demo
This will do what you want on Oracle (one of the tags on the question):
SELECT descript,length(descript)
FROM t WHERE length(descript)=( SELECT max(length(descript)) FROM t);
Keep in mind that if there are two (or more) records with that length, you will get them all. You could add a AND rownum < 2 clause but that will give you which ever entry the database chooses to give.
If you want the first record of those with the longest field, a subquery will be required:
SELECT descript,length(descript) from (
select descript from t order by length(descript) desc, descript asc)
WHERE rownum < 2;
There is also the offset 0 row fetch... version listed earlier:
SQL> select descript, length(descript)
2 from t
3 order by length(descript) desc
4 offset 0 row fetch first 1 row only;
DESCRIPT LENGTH(DESCRIPT)
-------------------- ----------------
defg 4
Mind you, this only works on my "12.2.0.1.0" Oracle. If I try the same thing on my "11.2.0.4.0" Oracle, I get "ORA-00933: SQL command not properly ended" with that last line. Apparently this is newer, maybe that is why I have never seen it before.

sql calculate column value then order by it

I have the sql below, and I then calculate one of the fields (market_cap) within the application code not the sql. I'd like to calculate it then order the records based on the top 7 calculated from it.
Proposed-ish Sql:
SELECT TOP 7 'market_cap' as [market_cap_calc] ??, [tradingcode],[last],[prvclose],[currentsharesonissue]
FROM [PricesCurrent]
ORDER BY tradingcode DESC
Asp code for field market_cap:
If Not IsNull(rs("last")) Then
marketcap = 0
If Not IsNull(rs("currentsharesonissue")) And Cdbl(rs("last")) <> 0 Then marketcap = (CDbl(rs("last")) * CDbl(rs("currentsharesonissue")))/1000000.0
End If
If marketcap = 0 And Not IsNull(rs("prvclose")) Then
If Not IsNull(rs("currentsharesonissue")) And CDbl(rs("prvclose")) > 0 Then marketcap = (CDbl(rs("prvclose")) * CDbl(rs("currentsharesonissue")))/1000000.0
End If
So basically it needs to check if last or prvclose is empty and then multiply either one by currentsharesonissue to get marketcap, and then get the top 7 rows based on that marketcap figure, how do I go about this? make a temporary table first?
You can do this with coalesce and a cte
WITH CTE AS(
SELECT
coalesce([last],[prvclose],0) * currentsharesonissue as CalculatedColumn
FROM [PricesCurrent])
select top 7 * from cte order by CalculatedColumn desc
COALESCE() is going to take the first NOT NULL value and multiply it by the currentsharesonissue column. Then, we just get the TOP7 as you stated, in DESCENDING order (assumed).

Check string starts with specific letter

Please help me with below query , I want to check if string is starts with 'G' or 'F' in where condition along with existing condition. here is the query
first query :-
SELECT Top 1 LC_ID, ISNULL(LC_UD, 0) as Record
FROM CONTRACT
WHERE LC_ID = 'F01'
output
F01 | 1 ( if available) else no record will be return.
second query:
IF LC_ID starts with 'F%' or 'G%'
How i can integrate both the query into one so that if there is no record available for 'F01' value, it will check if LC_ID starts with F & G then return
output
F04 | 1
else no record will be return.
You want to prioritize the values being returned. Because you want only one, you can do this with ORDER BY:
SELECT Top 1 LC_ID, COALESCE(LC_UD, 0) as Record
FROM CONTRACT
WHERE LC_ID LIKE '[FG]%'
ORDER BY (CASE WHEN LC_ID = 'F01' THEN 1 ELSE 2 END);
Note: This assumes you are using SQL Server (based on the syntax).
#gordon's answer is clever, though probably specific to Sql Server. Here's a different approach. The "Top 1" feature is also sql server, but could be avoided through additional techniques.
SELECT Top 1 LC_ID, ISNULL(LC_UD, 0) as Record
FROM CONTRACT
WHERE LC_ID = 'F01'
UNION
SELECT Top 1 LC_ID, ISNULL(LC_UD,0) As Record
FROM CONTRACT
WHERE NOT EXISTS (SELECT * FROM CONTRACT WHERE LC_ID = 'F01')
AND LC_ID LIKE '[FG]%'

Is there an alternative to TOP in MySQL?

I want to know the alternative of the TOP keyword as in MySQL. I have read about TOP in SQL Server.
Is there any alternative to this in MySQL, or any other method in MySQL from which we can get same functionality?
Ordering and limiting the results:
SELECT field1, field2
FROM myTable
ORDER BY field1 ASC
LIMIT 10
You can use the LIMIT keyword (See the documentation of the SELECT instruction) -- it goes at the end of the query :
select *
from your_table
where ...
limit 10
to get the top 10 lines
Or even :
select *
from your_table
where ...
limit 5, 10
To get 10 lines, startig from the 6th (i.e. getting lines 6 to 15).
yes, there is the limit clause.
Example:
SELECT * FROM `your_table` LIMIT 0, 10
This will display the first 10 results from the database.
mysql equivalent of top and you can find further more about LIMIT in MySql Doc