Comparing value with comma separated values in select statement - sql

this is how my table looks like :
Now i want to retrieve Field1 for 'AAA'
I tried something like :
select Field1 from table where CommaSeparatedList='AAA'
But it didn't work.
So How can I achieve this ??

select field1
from table
where ',' + replace(commaseparatedlist, ' ', '') + ',' like '%,AAA,%'

I suggest that you try:
SELECT Field1 FROM table WHERE CommaSeparatedList LIKE '%AAA%'

If you just need the column to contain the value you are looking for, you could do something like this:
SELECT Field1
FROM table
WHERE CommaSeparatedList LIKE '%AAA%'

Related

How can I use contains to look for keyword including a space?

I want to use contains with search a word that has single space on left and right side. But it is not working..
SELECT *
FROM mytable
WHERE Contains(name, ' Hertz ');
Any help is appreciated..
You Can LTRIM RTRIM the column and can get the values.
create table #temp
(
name varchar(50)
)
insert into #temp values(' hertz')
insert into #temp values('hertz ')
insert into #temp values('hertzx')
insert into #temp values('hertz')
select * from #temp where LTRIM(RTRIM(name)) like '%hertz%'
If you want to search if it has space in start or end use below query.
select * from #temp where name like ' %'
select * from #temp where name like '% '
Try with this:
SELECT *
FROM mytable
WHERE name=' Hertz '
If Hertz is just an example, you can use:
SELECT *
FROM mytable
WHERE name like ' % '
which will return all the names with an space in the first and last position
Try as below.
SELECT *
FROM mytable
WHERE name LIKE '% Hertz %'
Learn more about like operator
You could try this
SELECT *
FROM mytable
WHERE name Like '% Hertz %'
To search with spaces included, you need to enclose your expression within double quotes, like :
SELECT *
FROM mytable
WHERE CONTAINS(name, '" Hertz "');
Of course this also can be written with LIKE (although it will be less efficient when dealing with large data sets) :
SELECT *
FROM mytable
WHERE name LIKE '% Hertz %');
If you use like operator, you no need to give spaces between that like below
select * from mytable where name like '%her%'
And also if you want the data of which having spaces, you can use like below
select * from mytable where name like '_%hertz%_'
If we use underscore, it will skip the particular position element and search the data.
Try this
Select * from mytable where replace(name,' ','')='Hertz'
You can also use ltrim & rtrim to remove spaces at run time to match the name values.
You can try this
SELECT a.name
FROM mytable a
WHERE a.name like ' %% '
OR
SELECT a.name
FROM mytable a
WHERE a.name like' % '
You can find the live demo Demo here

SQL Server 2012, Combine multiple fields into one

I've tried
'field1'+'field2'+'field3' as combination
and tried:
((((field_1||'_')||field_2_yes)||'_')||field_3_yes) AS combination
tried:
field1 || field2 as combination
i've also tried Concat, coalesce, can't seem to get it to work.
Also I am querying SQL server through excel if that helps.
Expected output is a column named combination with the field1field2field3 data as the rows within combination column. I'm am not trying to create the literal string field1field2field3 but combine the data from these fields into a string and display in new column.
The issue is with the data types as to why these fields are not combining.
Could you please try without aposthropes?:
select combination = (field1 + field2)
from table
In case you need to put a space between the values, if field1 and field2 is varchar, then you can do:
select combination = (field1 + ' ' + field2)
from table
If field1 and field2 is not varchar, then you can cast like below and use in your main query:
cast(field1 as varchar(50))
So do you want only to display 2 fields as a one field?
Field1 = 'hello'
field2 = 'world'
select field1 + field2 as concatfield from table
select concat(field1, field2) as concatfield from table
This will give you output like that:
helloworld
If you want to add space then try this:
select field1 + ' ' + field2 as concatfield from table
select concat(field1, ' ', field2) as concatfield from table
This will give you output like that:
hello world
And then exacly the same if you want to update a field in DB
update table
set concatfield = field1 + ' ' + field2 --or concat(field1, ' ', field2)
--where 1=1
It is pretty basic
declare #Field varchar(10) = 'value'
select #Field + #Field + #Field as comb
select '#Field' + '#Field' + '#Field' as comp
comb
------------------------------
valuevaluevalue
comp
------------------
#Field#Field#Field

Not Null - Spaces on field

I have the below query that shows me the records in Oracle that are not null but some of the records contain spaces such as '',' ', etc.
How can I modify the query so it will ignore empty spaces?
select * from table where field1 is not null
Many Thanks.
If you problem is empty or extra space you can do something like this..
select * from table where replace(field1,' ','') is not null
You should use trim or replace function
e.g.
1.
select * from table
where field1 is not null
and trim(field1) != ''
;
2.
select * from table
where field1 is not null
and replace(field1,' ')
;
p.s null is not empty data ! it is unknown.
select * from table where field1 is not null and trim(field1) <> ''

Select 2 columns in one and combine them

Is it possible to select 2 columns in just one and combine them?
Example:
select something + somethingElse as onlyOneColumn from someTable
(SELECT column1 as column FROM table )
UNION
(SELECT column2 as column FROM table )
Yes, just like you did:
select something + somethingElse as onlyOneColumn from someTable
If you queried the database, you would have gotten the right answer.
What happens is you ask for an expression. A very simple expression is just a column name, a more complicated expression can have formulas etc in it.
Yes,
SELECT CONCAT(field1, field2) AS WHOLENAME FROM TABLE
WHERE ...
will result in data set like:
WHOLENAME
field1field2
None of the other answers worked for me but this did:
SELECT CONCAT(Cust_First, ' ', Cust_Last) AS CustName FROM customer
Yes it's possible, as long as the datatypes are compatible. If they aren't, use a CONVERT() or CAST()
SELECT firstname + ' ' + lastname AS name FROM customers
The + operator should do the trick just fine. Keep something in mind though, if one of the columns is null or does not have any value, it will give you a NULL result. Instead, combine + with the function COALESCE and you'll be set.
Here is an example:
SELECT COALESCE(column1,'') + COALESCE(column2,'') FROM table1.
For this example, if column1 is NULL, then the results of column2 will show up, instead of a simple NULL.
Hope this helps!
To complete the answer of #Pete Carter, I would add an "ALL" on the UNION (if you need to keep the duplicate entries).
(SELECT column1 as column FROM table )
UNION ALL
(SELECT column2 as column FROM table )
DROP TABLE IF EXISTS #9
CREATE TABLE #9
(
USER1 int
,USER2 int
)
INSERT INTO #9
VALUES(1, 2), (1, 3), (1, 4), (2, 3)
------------------------------------------------
(SELECT USER1 AS 'column' from #9)
UNION ALL
(SELECT USER2 AS 'column' from #9)
Would then return : Result
Yes, you can combine columns easily enough such as concatenating character data:
select col1 | col 2 as bothcols from tbl ...
or adding (for example) numeric data:
select col1 + col2 as bothcols from tbl ...
In both those cases, you end up with a single column bothcols, which contains the combined data. You may have to coerce the data type if the columns are not compatible.
if one of the column is number i have experienced the oracle will think '+' as sum operator instead concatenation.
eg:
select (id + name) as one from table 1; (id is numeric)
throws invalid number exception
in such case you can || operator which is concatenation.
select (id || name) as one from table 1;
Your syntax should work, maybe add a space between the colums like
SELECT something + ' ' + somethingElse as onlyOneColumn FROM someTable
I hope this answer helps:
SELECT (CAST(id AS NVARCHAR)+','+name) AS COMBINED_COLUMN FROM TABLENAME;
select column1 || ' ' || column2 as whole_name FROM tablename;
Here || is the concat operator used for concatenating them to single column and ('') inside || used for space between two columns.
SELECT firstname || ' ' || lastname FROM users;

SQL Server statement Where myfield content giving sub-string

I'm looking for a SQL Server statement to retrieve records Where myfield content giving sub-string.
Another possibility is to use LIKE:
SELECT
MT.column_1,
....
FROM
My_Table MT
WHERE
some_column LIKE '%' + #search_string + '%'
You can use CharIndex
Select * From YourTable
Where
CharIndex('yoursubstring', myfield) > 0
Try PatIndex.
SELECT *
FROM Table
WHERE patindex('%string%', data ) > 0
select * from mytable where myfield like '%literalstring%'
or
select * from mytable where myfield like '%' + #stringvar + '%'
...not really clear on whether your substring is a literal or a variable