how to find a character in string in sql? - sql

i am working with sql and got stuck in issue,
i am using sql server 2012 r2.
i have a table (info) with 2 columns id (int) and names varchar(max).
in the names column i have stored coma(,) separated list of names, like marry,rita,johan,david,.
now i want to get the id where names is david.
select id from info where names='david'
how it is possible in sql, i know we can do it in c# by using string.substing but is it possible in sql?

Try this:
SELECT id FROM info
WHERE names LIKE '%david%'
It will return the id of the record if the column names contains 'david'.
Read more about LIKE operator here.

Related

SQLite, LIKE column name

What query could I use in sqlite to get the names of columns beginning with (for example) "thing" in a DB
Such as if they were formatted like this:
"thing_column1"
"thing_column2"
"thing_data"
etc.
You can use pragma_table_info() with the table's name:
SELECT name
FROM pragma_table_info('tablename')
WHERE name LIKE 'thing%'
You can use this query:
SELECT GROUP_CONCAT(name) AS columns
FROM pragma_table_info('tablename')
WHERE name LIKE 'thing%'
which returns only 1 column columns with a string value like 'thing_column1,thing_column2,thing_column3' and you can use it to construct a SELECT statement in your application.

Not getting all the rows in result while using wildcard pattern matching in sql query

I have a database named student which has a column named id which contains varchar type data as follows:
id
050350014831
050350015337
050350015338
050350015339
...
I have written the following SQL query to retrieve the id that begins with 05035 using wildcard pattern matching :
select id from student
where id like '05035%';
But I am getting 050350014831 in my output but not getting 050350015337, 050350015338, 050350015339 in the result. I am using SQL Server Management Studio. Is there anything wrong in the query?

T-SQL: Exclude Columns from a SELECT statement based on string

My overall goal is to create new tables by selecting columns in existing tables with certain patterns/tags in their column names. This is in SQL Server.
For example, I have a common need to get all contact information out of a company table, and into its own contact table.
I haven't been able to find a programmatic approach in SQL to express excluding columns from a SELECT statement based on string.
When looking to options like the COL_NAME function, those require an ID arg, which kills that option for me.
Wishing there was something built in that could work like the following:
SELECT *
FROM TABLE
WHERE COL_NAME() LIKE 'FLAG%'
Any ideas? Open to anything! Thanks!!
One trick could be to firstly using the following code to get the column names you desire:
select * from information_schema.columns
where table_name='tbl' and column_name like 'FLAG%'
Then concatenate them into a comma-delimited string and finally create a dynamic sql query using the created string as the column names.

Retrieve data from LIKE Query

I have a database table named 'manufact' which has columns like below.
Whats the SQL command to Retrieve the manufacturer details whose names start with letter A to N.
Select * from manufacturer where manu_name like '[A-N]%';

SQL Select Command with Unicode string is not Retrieving the Expected Data

I am using SQL Server 2008 R2. I want the retrieve the row from table in which data is other than than English.
But when I type the command it returns me nothing.
SELECT *
FROM PARTY
WHERE NAME LIKE 'رانا عطا ربانی'
ORDER BY SRNO
Any suggestions, how to retrieve record like that?
Have you tried declaring the string literal as unicode?
SELECT * FROM PARTY
WHERE NAME LIKE N'رانا عطا ربانی'
ORDER BY SRNO
[This will show you all string-related columns in your database, and their collation: Collation conflict SQL Server 2008 ]