SQL server 2008 not supporting LIMIT [duplicate] - sql-server

This question already has answers here:
MySQL LIMIT clause equivalent for SQL SERVER
(5 answers)
Closed 9 years ago.
I am trying to use LIMIT into SQL server 2008 but its not working, what can I do, I am new to SQL I have used mySQL before.
Here is the SQL query I am trying:
SELECT * FROM konto WHERE id_e_klientit = 9 ORDER BY koha_relizimit DESC LIMIT 1

Try SELECT TOP 1 * ...
Also see http://www.w3schools.com/sql/sql_top.asp for more details.

In MSSQL it's apparently something like SELECT TOP x * FROM ...
SELECT TOP 1 * FROM konto WHERE id_e_klientit = 9 ORDER BY koha_relizimit DESC

Related

Calculate Date Difference in SQL Server 2019 [duplicate]

This question already has answers here:
Possible to store value of one select column and use it for the next one?
(4 answers)
How to use a calculated column to calculate another column in the same query using a subquery
(2 answers)
SQL Server Reference a Calculated Column
(6 answers)
Using CASE on a Calculated Column in SQL Server 2012
(2 answers)
Closed 6 months ago.
I am trying to calculate the date difference and keep getting a column name error. I am new to SQL and learning from books and YouTube. Any assistance would be appreciated. I commented out the code not working
declare #rpDT datetime
set #rpDT = getdate()
SELECT [OrgKey]
,[visID]
,[visPatID]
,[visInternal]
,[visName]
,[visAssignedNS]
,[visAssignedRoom]
,[visAssignedBed]
,[visAdmitDT]
,isnull([visDischargeDT],#rpDT)as disDT
,datediff(day,[visAdmitDT],disDT) as Pt_days
FROM [MH_Pharmacy_Hub].[MC].[dbo_Visits]
SQL Server and most of the RDBMS do not allow to reference column at the same level. The closest equivalent to achieve similar effect is CROSS APPLY:
declare #rpDT datetime = getdate();
SELECT [OrgKey]
,[visID]
,[visPatID]
,[visInternal]
,[visName]
,[visAssignedNS]
,[visAssignedRoom]
,[visAssignedBed]
,[visAdmitDT]
,s.disDT
,datediff(day,[visAdmitDT],s.disDT) as Pt_days
FROM [MH_Pharmacy_Hub].[MC].[dbo_Visits]
CROSS APPLY (SELECT COALESCE([visDischargeDT],#rpDT)) AS s(disDT);

Query not running in SQL Server [duplicate]

This question already has answers here:
How do I 'subtract' sql tables?
(14 answers)
Closed 4 years ago.
I have this select statement that I am running in SQL Server. But it's throwing an error:
select count(*)
from
(select zip from A
minus
select zip from B)
Error:
Incorrect syntax near select
What is the issue here? I have also tried aliasing the subquery but same error happens.
There is nothing called minus in SQL Server, you need to use except.
Note, except in SQL Server is equivalent to minus of Oracle
Following query will work.
select count(*) ct
from
(
select zip from A
except
select zip from B
)t
Another issue with your code is that you need to give a alias name to the inner table you are creating.

Linked Server Querying [duplicate]

This question already has answers here:
SQL Server Linked Server Example Query
(16 answers)
Closed 5 years ago.
I have the table name which is in the linked server.How to write a T-SQL query to find the database name (In linked server) from that table name?
I have no idea about linked server querying.Kindly help me
Thank you.
SELECT * FROM <linkedservername>.INFORMATION_SCHEMA.TABLES
or
SELECT * FROM <linkedservername>.INFORMATION_SCHEMA.TABLES
WHERE TABLE_NAME = 'XXXXX'
or
select * from <linkedservername>.master.sys.databases
for get a list of all Databases
SELECT * FROM (nameoflinkedserver).INFORMATION_SCHEMA.TABLES
SELECT * FROM LinkServerName.DatabaseName.SchemaName.TableName
Example
Select * from MYLINKSERVER.MYDATABASE.MYSCHEMA.MYTABLE

Get all the Table names in particular database SQL Server [duplicate]

This question already has answers here:
How do I get list of all tables in a database using TSQL?
(20 answers)
Closed 6 years ago.
I am using SQL Server 2014 and want to get all the tables name in a particular database STUDENT through a SQL query.
How is it possible?
Thanks
You want to query sys.objects and look for everything with the type description 'USER_TABLE'. You could use a query like this;
SELECT
*
FROM STUDENT.sys.objects
WHERE type_desc = 'USER_TABLE'
The FROM clause has the usual format: DatabaseName.SchemaName.TableName.
Or as marc_s mentions, you can use sys.tables instead;
SELECT
*
FROM STUDENT.sys.tables

Update Query for string replace in sql server [duplicate]

This question already has answers here:
How to replace a string in a SQL Server Table Column
(10 answers)
Closed 8 years ago.
i have table,
id Name
1 aaaaAAbbbb
2 bbbbAAaaaa
3 aabbAAbbaa
This is my table,i want to update 'AA' with 'BB' using sql server 2008
update myTable set [Name] = REPLACE([Name], 'AA', 'BB')
REPLACE applies to SQL Server 2008 upwards.
You can use
REPLACE(Name,'AA','BB');
and update you column.

Resources