Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I want to use empty value in an IN clause. But following queries don't work identically.
SELECT * from tblTest WHERE colName IN ( '')
SELECT * from tblTest WHERE colName IN (SELECT '''''')
The first works fine but the second has no error but gives empty result.
It's because SELECT '''''' will return you '' and try to compare your values to it, which is not an empty string.
Anyway, it's not quite clear what you're trying to achieve.
That's pretty clear: you want the rows where the column contains an empty string. But if you compare with (select '''''') you dont select an empty string! You select a string that looks like this: ''
This works:
SELECT * from tblTest WHERE colName IN (SELECT '')
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 days ago.
Improve this question
Example:
SubNetwork=ONRM_ROOT_MO,SubNetwork=RadioNode,MeContext=BAS917L
I want to extract only RadioNode
SubNetwork=ONRM_ROOT_MO,SubNetwork=GALRNC1,MeContext=BAT045W
I want to extract only GALRNC1.
You can do it using SUBSTRING and LEFT and charindex
select LEFT(sub1, CHARINDEX(',MeContext', sub1) - 1)
from (
select
SUBSTRING(column1, charindex('SubNetwork=', column1, 2) + LEN('SubNetwork='), 20) sub1
from mytable
) as s
charindex to get the position of your word in this case it is SubNetwork=.
SUBSTRING to Extract characters from a string.
LEFT to Extract characters from a string (starting from left).
Demo here
Please try the following solution.
It is using JSON and will work starting from SQL Server 2016 onwards.
SQL
-- DDL and sample data population, start
DECLARE #tbl TABLE (id INT IDENTITY PRIMARY KEY, tokens NVARCHAR(1024));
INSERT #tbl (tokens) VALUES
(N'SubNetwork=ONRM_ROOT_MO,SubNetwork=RadioNode,MeContext=BAS917L'),
(N'SubNetwork=ONRM_ROOT_MO,SubNetwork=GALRNC1,MeContext=BAT045W');
-- DDL and sample data population, end
SELECT t.*
, SubNetwork = JSON_VALUE(j,'$.SubNetwork')
FROM #tbl AS t
CROSS APPLY (SELECT '{"' + REPLACE(REPLACE(STRING_ESCAPE('~'+tokens,'json')
,',','","')
,'=','":"') + '"}'
) AS t1(j);;
Output
id
tokens
SubNetwork
1
SubNetwork=ONRM_ROOT_MO,SubNetwork=RadioNode,MeContext=BAS917L
RadioNode
2
SubNetwork=ONRM_ROOT_MO,SubNetwork=GALRNC1,MeContext=BAT045W
GALRNC1
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 months ago.
Improve this question
SELECT *
FROM table1
WHERE (#Id IS NULL
OR (a.ID = #Id)
OR (SELECT * FROM Table2 WHERE TestID = #Id) // How to check multiple values here
The above query works fine when TestID has single record. But when more than 1 record present in TestID column, I am trying to implement and not got exact solution.
How to confirm #Id value present in TestId column?
Could you please assist me on this? Thank you
It looks like you need exists
or exists (
SELECT * FROM Table2 WHERE TestID = #Id
)
Use top 1 before * from this way you will always get a single row in case there are multiple rows for the same #ID.
SELECT top 1 * FROM Table2 WHERE TestID = #Id
Though I am not sure how this line works in where clause as it does not return any boolean
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 12 months ago.
Improve this question
I am trying to concat columns with a comma , as the delimiter.
My concern is that any column with NULL will end up with only a , and if two columns are NULL, I'd get ,,.
How to dynamically find and replace these multiple commatas ,,, to a single ,? Please support
If you are using SQL Server 2017+ just use concat_ws which ignores nulls
Eg
select concat_ws(', ','abc',null,'def')
Result: abc, def
Use CONCAT_WS to concatenate several columns and skip nulls:
select concat_ws(' ', 'hallo', null, 'world', null);
returns 'hello world'.
In your case you want a comma as a separator, hence:
select concat_ws(',', col1, col2, col3, col4) from mytable;
Demo: https://dbfiddle.uk/?rdbms=sqlserver_2019&fiddle=b55f64bd22f5e20e23f156ad23ab85e0
One quick trick is using ISNULL or COALESCE to null check.
If not null concat comma+string, else an empty string.
The ideal solution is STRING_AGG or CONCAT_WS which does like string.Join in C#
Another way :
select concat('hallo' + ', ', null + ', ', 'world' + ', ', null + ', ');
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I have two columns in two different tables.
First column is number like 0493484402 and second column is audit_detail like 'addr_mastersubscription has changed from 32488141893 to 32488141973'.
I have to check whether first column value is present or not in second column's entire string.
If the number is not present I need that number as output
You could use LIKE here:
SELECT *
FROM table1 t1
INNER JOIN table2 t2
ON t2.audit_detail LIKE '%' || t1.col1 || '%';
We can also use INSTR:
SELECT *
FROM table1 t1
INNER JOIN table2 t2
ON INSTR(t2.audit_detail, t1.col1) > 0;
I think, with solution of like and instr, you must have some delimeter which comes before and after your string in second column. Else 1 as a fisrt column will match eith 123 in second column.
Looking at the sample data, you can use space as a delimeter. But you will need leading and trailing space compulsory in your second column for matching first column.
You can use something like this:
Select *
Table1 t1 join table2 t2
On ' ' || t2.second_column || ' ' like '% ' || t1.first_column || ' %';
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I have 2 tables: MainTable and ControlTable.
I want to write a query that builds a string representing a file path.
File path will be built dynamically depending on a query result between two tables.
Main table has the following columns:
ControlNumber
CustomerID
CustomerStatement
The Control table has only one column: ControlNumber
I need to write a query that checks if Main table has a ControlNumber defined in Control Table.
If there is a match, I append \FolderA to my FilePath
If no match, I append \FolderB
Ending result will be something like this:
C:\Customers\FolderA or C:\Customers\FolderB
I suspect I need to use left join
How can I do that?
You're right that you want a left join. Combine that with a case...when expression to determine the value:
select
*,
case
when Control.ControlNumber is not null
then '\FolderA'
else '\FolderB'
end as FilePath
from main
left join control on main.ControlNumber = control.ControlNumber
It's not clear where the rest of the path comes from; maybe it's static and you want to concatenate it with the value from the case expression:
'c:\customers' + -- or concat() or || depending on sql dialect
case when Control.ControlNumber is not null then '\FolderA' else '\FolderB' end as FilePath
SELECT 'C:\' || CustomerID || '\FolderA'
FROM MainTable
WHERE EXISTS
( SELECT 1 FROM ControlNumber WHERE ControlTable.ControlNumber = MainTable.CustomerID )
UNION
SELECT 'C:\' || CustomerID || '\FolderB'
FROM MainTable
WHERE NOT EXISTS
( SELECT 1 FROM ControlTable WHERE ControlTable.ControlNumber = MainTable.ControlNumber)