How to make a morphological search? - sql-server

Good day.
I have query:
SELECT *
FROM Firm
WHERE name LIKE 'АВТОМОБИЛЬ%'
Tell me please how use morphological search with this query on MsSQL 2008 ?

Declare #find NVarchar(20)='test'
SELECT *
FROM Firm
WHERE name LIKE '%'+#find+'%' OR phone LIKE '%'+#find+'%'

Just run the below Query
SELECT *
FROM Firm
WHERE name LIKE N'АВТОМОБИЛЬ%'
-- ^ Magical Code
SQL Fiddle

Related

SQL Server fast random sampling

I have been looking for methods for fast sampling from sql table and I found this very useful article:
https://www.sisense.com/blog/how-to-sample-rows-in-sql-273x-faster/
The query looks like:
select *
from users
where id in (select round(random() * 21e6)::integer as id
from generate_series(1, 110)
group by id -- Discard duplicates
)
limit 100
However for SQL Server, I got errors like:
'random' is not a recognized built-in function name.
Do we have some SQL Server counter part of this fast sampling method? Thanks.
I tried edit as:
select top 100 * from users
where id in (
select round(RAND() * 21e6) CAST integer as id
from generate_series(1, 110)
group by id -- Discard duplicates
)
still having error:
The round function requires 2 to 3 arguments.
For T-SQL it is RAND(), not RANDOM()
Also, apart from the comment regarding having to use TOP(n) instead of limit - you also have to use CAST or CONVERT for the data type conversion. And generate_series is not a built-in function in sql server.
From Sean's comment: TABLESAMPLE . Would be the solution, now it is extremely fast to sample.

Using Full-Text-Search in order to find partial words (SQL Server 2008)

I'm trying to build a facebook like search for my software.
I'd like to query the table customers.
I've set up a FULLTEXT Index and tried the next query
SELECT * FROM Customer where CONTAINS(*,'*ann*')
The query does return all the customers named Ann, but it doesn't return all the customers name Anne.
Is there a way to create prefix search on SQL Server 2008 using FTS?
I've found a solution to my problem.
The query should be:
select * from Customers where contains(*, '"ann*"')
The quotes are the important part.

How can I concatinate a subquery result field into the parent query?

DB: Sql Server 2008.
I have a really (fake) groovy query like this:-
SELECT CarId, NumberPlate
(SELECT Owner
FROM Owners b
WHERE b.CarId = a.CarId) AS Owners
FROM Cars a
ORDER BY NumberPlate
And this is what I'm trying to get...
=> 1 ABC123 John, Jill, Jane
=> 2 XYZ123 Fred
=> 3 SOHOT Jon Skeet, ScottGu
So, i tried using
AS [Text()] ... FOR XML PATH('') but that was inlcuding weird encoded characters (eg. carriage return). ... so i'm not 100% happy with that.
I also tried to see if there's a COALESCE solution, but all my attempts failed.
So - any suggestions?
Answering an old post, just thought it needed an update for newer versions of SQL Server:
For SQL Server 2017 use STRING_AGG(expression, seperator)
GROUP_CONCAT is MySQL.
Prior to SQL 2017, you can also do something like (snipped from our current code base on SQL Server 2016):
SELECT CarId, NumberPlate,
(STUFF(( SELECT ', ' + b.Owner
FROM Owners b
WHERE b.CarId = a.CarId
FOR XML PATH('')
)
,1,2,'')) AS Owners
FROM Cars a
ORDER BY NumberPlate
Links to STRING_AGG
https://database.guide/the-sql-server-equivalent-to-group_concat/
https://learn.microsoft.com/en-us/sql/t-sql/functions/string-agg-transact-sql?view=sql-server-2017
Link to STUFF:
https://learn.microsoft.com/en-us/sql/t-sql/functions/stuff-transact-sql?view=sql-server-2017
and finally links to FOR XML:
https://learn.microsoft.com/en-us/sql/relational-databases/xml/for-xml-sql-server?view=sql-server-2017
Try the solution to this question:
How to create a SQL Server function to "join" multiple rows from a subquery into a single delimited field?
:)
Use GROUP_CONCAT
SELECT CarId, NumberPlate
(SELECT GROUP_CONCAT(Owner)
FROM Owners b
WHERE b.CarId = a.CarId) AS Owners
FROM Cars a
ORDER BY NumberPlate

How to check if I'm currently the database owner for SQL 2000/2005/2008

I can't remember how to do this in a TSQL query. It was a one-liner, good for testing before running DML. The query was something similar to SELECT IS_DBO() or SELECT IS(DBO).
You're probably looking for the IS_MEMBER function:
SELECT IS_MEMBER('db_owner')

How to get the data from linked servers using queries

I have created a linkedserver as ravikiran-vm which is the virtual machine in my desktop.
Now I have a database called kiran which contains an employ table.
To retrieve employ data, I do the following:
select * from ravikiran-vm.kiran.employ
but it shows the error "Incorrect syntax near '-'."
Can anyone help me, please?
Thanks in advance.
Thanks guys with ur support it working fine...
Now i hav to schedule this as a new job.when i execute it as normal it shows o/p.
but when i cinfigure the same query as sqlserver agent job it gives error and query not executing...Plz help me in this regard
Thanks in advance
I think you should change the name of the linked server, as the - char is reserved in SQL.
You could try surrounding the name with brackets, but it becomes boring
Also, you should include the schema name in the query, or double point to use the default one:
so, you can try:
select * from [ravikiran-vm].kiran.dbo.employ
select * from [ravikiran-vm].kiran..employ
Or whatever your schema be.
You have to use OPENQUERY:
SELECT * FROM OPENQUERY([ravikiran-vm],'SELECT * FROM KIRAN..EMPLOY')
to get data from linked server you use 4 part notation
Server.Database.Schema.Table
since you have an invalid character in your name(-) you need to add brackets around the name
select * from [ravikiran-vm].kiran..employ
You probably also don't want all the data returned
Usually direct queries should not be used in case of linked server because it heavily use temp database of SQL server. At first step data is retrieved into temp DB then filtering occur. There are many threads about this. It is better to use open OPENQUERY because it passes SQL to the source linked server and then it return filtered results e.g.
SELECT * FROM OPENQUERY(Linked_Server_Name , 'select * from TableName where ID = 500')
1- Link the server
EXEC sp_addlinkedserver 'OracleSvr',
'Oracle 7.3',
'MSDAORA',
'ORCLDB'
2-SELECT
SELECT *
FROM OPENQUERY(OracleSvr, 'SELECT name, id FROM albert.titles')
3-UPDATE
UPDATE OPENQUERY (OracleSvr, 'SELECT name FROM albert.titles WHERE id = 101')
4-INSERT
INSERT OPENQUERY (OracleSvr, 'SELECT name FROM albert.titles')
VALUES ('NewTitle');
5-DELETE
DELETE OPENQUERY (OracleSvr, 'SELECT name FROM albert.titles WHERE name = ''NewTitle''')
i just copied from here (http://www.sqlservercentral.com/Forums/Topic916320-392-1.aspx)
Select * from likedservername.databasename.dbo(schema).tablename
Ex1:
select * from [Bse].[Bse].[dbo].Binary
Else
Select * from openquery (linkedservername, 'select * from databasename.dbo(schema).tablename');
Ex2:
select * from openquery ([Bse], 'select * from [Bse].[dbo].Binary');

Resources