SQL select startwith (SQL Server) [duplicate] - sql-server

This question already has answers here:
SQL Server Escape an Underscore
(8 answers)
How do I escape _ in SQL Server? [duplicate]
(3 answers)
Closed 7 years ago.
I need to find a list of all items starting with an underscore: _
The obvious doesn't work:
select * from role where Name like '_%'
This still returns all items.
The following also returns all items.
select * from role where Name like '\_%'
select * from role where Name like '__%'
anyone knows how to escape a underscore? or how else I can achieve this StartWithUnderscore functionality?

I found the answer:
select * from role where Name like '[_]%';

You can escape with any character you want eg
select * from role where Name like '!_%' ESCAPE'!'
You can read more on this page: LIKE (Transact-SQL)

Related

Get everything after and before certain character in SQL [duplicate]

This question already has answers here:
A SQL Query to select a string between two known strings
(18 answers)
Closed 2 years ago.
I have a string like
https:\/\/www.abc.ca\/v3\/homes\/190135717\/responses\/bulk?page=5&per_page=100
I want everything after the equal sign = and before &, i.e the number 5 in SQL Server
Try this below logic using SUBSTRING and CHARINDEX:
Demo Here
DECLARE #T VARCHAR(200) = 'https:\/\/www.abc.ca\/v3\/homes\/190135717\/responses\/bulk?page=5&per_page=100'
SELECT SUBSTRING(#T,CHARINDEX('=',#T,0)+1,CHARINDEX('&',#T,0)-CHARINDEX('=',#T,0)-1)

Passing variable in Group By in SQL Server [duplicate]

This question already has answers here:
I need to pass column names using variable in select statement in Store Procedure but i cannot use dynamic query
(3 answers)
Where can parameters be used in SQL statements?
(2 answers)
Using SQL parameters to DROP INDEX in C#
(2 answers)
Closed 5 years ago.
How can I pass variable in Group BY expression.Here is my code.
and from #Varientsku I want to pass column name
Create Proc spIsUnique
#varientsku nvarchar(max) output
As
Begin
select #varientsku,
IIf (count(*)>1,'Dublicate','True') as Total from product group by
#varientsku
End.
and I get this error.
Each GROUP BY expression must contain at least one column that is not an outer reference.
Help me to solve this
You need dynamic sql
EXEC ('select '+#varientsku+',
IIf (count(*)>1,''Duplicate'',''True'') as Total
from product
group by '+#varientsku)
Note : Make sure #varientsku is sanitized, this code is vulnerable to sql injection

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

How to take out everything before dash(-) in SQL Server? [duplicate]

This question already has answers here:
getting all chars before space in SQL SERVER
(2 answers)
Closed 8 years ago.
I want to extract a certain part of a row. if there is something like this ..." abcd - productA", " zas1234 - productC", how do I extract everything before the dash(-)?
I tried a left(column name,'-') it didnt work. I am on SQL Server 2012.
The column from which I am trying to extract is a varchar column.
It would be great if someone could help me.
Try this,
select SUBSTRING(column_name, 1, CHARINDEX('-', column_name)-1) from table_name;
DECLARE #varStr VARCHAR(MAX)
SET #varStr='abcd - productA'
SELECT CHARINDEX('-',#varStr)
SELECT LEFT(#varStr,CHARINDEX('-',#varStr)-1)

Transact-SQL using variable for table name [duplicate]

This question already has answers here:
A table name as a variable
(10 answers)
Closed 9 years ago.
I have try to use statement below in my SQL. Is it possible by any means to dynamically select table form database. (For example select table name from Comobox and then display data from selected table)
SET #var1 = 'test';
SELECT * From #var1
Dynamic SQL requires that you create a string consisting of the sql statement, and then you execute the string. Dynamic t-sql quotes in string

Resources