Where clause concat with comma - sql

I am trying to write what I thought would be a simple where clause. The FULLNAME string is formatted like this ("Doe, John"), hence the where clause and concat function.
Here is the query:
SELECT *
FROM table
WHERE concat(LAST_NM, ', ', FIRST_NM) = FULLNAME;
The query fails with the following error:
Error Executing Database Query. [Macromedia][SQLServer JDBC Driver]
[SQLServer]Incorrect syntax near ','.

Is your FullName something like "Doe, John"?
Is your FullName complete?
if not
SELECT *
FROM table
WHERE concat(LAST_NM, ', ', FIRST_NM) like ('%' + FULLNAME + '%')

Although I do not see anything wrong with your query, I would like to suggest a way to troubleshoot, by trying to find if it is a specific raw values that cause the concat function to break.
The idea is to iterate through the raws printing each one's values so you'll know which one (if at all) fails it, and it might be easier to take it from there to the next step.
--Copy your table data to a temporary table
--E.g.
SELECT *
into #table
FROM
(
select 'Doe' as LAST_NM , 'Jhon' as FIRST_NM
union
select 'Ryan' as LAST_NM , 'Jack' as FIRST_NM
union
select 'Dylan' as LAST_NM , 'Bob' as FIRST_NM
) a
--Iterate through the rows to see which one causes the issue
DECLARE #I INT = 1
DECLARE #TableCount INT = (select count(*) from #table)
DECLARE #FN varchar(100)
DECLARE #LN varchar(100)
WHILE #I <= #TableCount
BEGIN
SET #FN = (select Top 1 FIRST_NM from #table)
SET #LN = (select Top 1 LAST_NM from #table)
print(#I)
print(#FN)
print(#LN)
print('------------------')
delete top (1) from #table
SET #I = #I + 1
END

Related

SQL return values if row count > X

DECLARE #sql_string varchar(7000)
set #sql_string = (select top 1 statement from queries where name = 'report name')
EXECUTE (#sql_string)
#sql_string is holding another SQL statement. This query works for me. It returns all the values from the query from the statement on the queries table. From this, I need to figure out how to only return the results IF the number of rows returned exceeds a threshold (for my particular case, 25). Else return nothing. I can't quite figure out how to get this conditional statement to work.
Much appreciated for any direction on this.
If all the queries return the same columns, you could simply store the data in a temporary table or table variable and then use logic such as:
select t.*
from #t t
where (select count(*) from #t) > 25;
An alternative is to try constructing a new query from the existing query. I don't recommend trying to parse the existing string, if you can avoid that. Assuming that the query does not use CTEs or have an ORDER BY clause, for instance, something like this should work:
set #sql = '
with q as (
' + #sql + '
)
select q.*
from q
where (select count(*) from q) > 25
';
That did the trick #Gordon. Here was my final:
DECLARE #report_name varchar(100)
DECLARE #sql_string varchar(7000)
DECLARE #sql varchar(7000)
DECLARE #days int
set #report_name = 'Complex Pass Failed within 1 day'
set #days = 5
set #sql_string = (select top 1 statement from queries where name = #report_name )
set #sql = 'with q as (' + #sql_string + ') select q.* from q where (select count(*) from q) > ' + convert(varchar(100), #days)
EXECUTE (#sql)
Worked with 2 nuances.
The SQL returned could not include an end ";" charicter
The statement cannot include an "order by" statement

nested select query with table name in sqlite

I am trying to select all distinct values from all tables that start with a specific name, like: 'logs_2020_12_01', 'logs_2021_01_02', ..To select all tables with this specific name is straight forward:
SELECT name FROM sqlite_master WHERE type='table' AND name LIKE 'logs_%';
The select I want for one individual table is:
SELECT DISTINCT batch FROM logs_2021_01_27;
but I cannot find a way to combine it to make the selection from all tables. I tried a couple of things but it does not work, like:
SELECT DISTINCT batch FROM (SELECT name FROM sqlite_master WHERE type='table' AND name LIKE 'logs_%')
any ideas?
thanks
What about using Dynamic SQL, stored your tables information into a temp table with id column and set it to identity.
CREATE TABLE #temp ---identity column will be used to iterate
(
id INT IDENTITY,
TableName VARCHAR(20)
)
INSERT INTO #temp
SELECT name FROM sqlite_master WHERE type='table' AND name LIKE 'logs_%';
-- choose your own results with where conditions
DECLARE #SQL VARCHAR(MAX)
DECLARE #Count INT = 1
DECLARE #Table VARCHAR(20)
WHILE #COUNT <= (SELECT COUNT(*) FROM #temp)
BEGIN
select #table = TABLENAME FROM #temp WHERE id = #Count
SELECT #sql = 'SELECT DISTINCT(batch) FROM '+ #table
PRINT #SQL
SET #Count = #Count + 1
END
after your print result looks good, change it to EXEC(#SQL), thanks
SQLite does not support dynamic sql.
You have to select the column batch from each of all the tables and combine them with UNION so the duplicates are removed:
SELECT batch FROM logs_2020_12_01 UNION
SELECT batch FROM logs_2020_12_02 UNION
......................................
SELECT batch FROM logs_2020_12_30 UNION
SELECT batch FROM logs_2020_12_31
If you don't know the full names of the tables, you can get them with this statement:
SELECT name
FROM sqlite_master
WHERE type = 'table' AND name LIKE 'logs/_%' ESCAPE '/'
and then use a programming language to construct a SELECT statement with UNION to get the results that you want.

SQL - Storing multiple records in a variable separated by commas

I have two variables, 1 varchar named cust_ref, and 1 int named associated_ids. What I'm trying to accomplish is the following:
You provide cust_ref with a value which will usually result in between 1+ rows being returned from the Customer table. I am concerned with gathering all customer_id records for that cust_ref and storing them in the associated_ids variable seperated by commars.
This is the SQL I have so far, and obviously is only loading one of the customer_id records into the variable. Based on this example I would like select #associated_ids to return the following 75458,77397,94955
declare #cust_ref varchar(20) = 'ABGR55532'
declare #associated_ids int
select distinct #associated_ids = customer_id
from dbo.Customer
where cust_ref = #cust_ref
select #associated_ids
select *
from dbo.Customer
where cust_ref = #cust_ref
Here is the results from the above, as you can see there are actually 3 associated_ids that I need stored in the variable in this example but my command is capturing the largest, I want all 3 seperated by commars.
declare #cust_ref varchar(20) = 'ABGR55532' --from your code
DECLARE #result varchar(100)
set #result =
(SELECT distinct (customer_id + ' ')
FROM dbo.Customer
where cust_ref = #cust_ref --from your code
ORDER BY (customer_id + ' ')
FOR XML PATH (''))
SELECT REPLACE(RTRIM(#result),' ',',')
You could try something like this ... obviously, some adjustment will be needed:
create table x (id varchar(50),num int)
insert into x (id,num) values ('75458','20')
insert into x (id,num) values ('77397','20')
insert into x (id,num) values ('94955','20')
and then,
create function GetList (#num as varchar(10))
returns varchar(100)
as
begin
declare #List varchar(100)
select #List = COALESCE(#List + ', ', '') + id
from x
where num = #num
return #List
end
Then, use something like this to get the values:
select distinct num,dbo.GetList(num) from x
Here you go
DECLARE #cust_ref varchar(20) = 'ABGR55532' --from your code
DECLARE #result varchar(100)
set #result =
(SELECT distinct (cast(customer_id as varchar) + ' ')
FROM dbo.Customer
where cust_ref = #cust_ref --from your code
ORDER BY (cast(customer_id as varchar) + ' ')
FOR XML PATH (''))
SELECT REPLACE(RTRIM(#result),' ',',')

How to split a comma separated data

I have a procedure and input is comma separated like '1,2,3'.
I would like to query like
SELECT * FROM PERSON WHERE PERSON_ID IN(1,2,3).
Please note that PERSON_ID is integer.
I've seen this type of question so often I posted a blog on it here.
Basically you have three options (to the best of my knowledge)
The LIKE version that Gordon Lindoff suggested.
Using a split function like so.
DECLARE #InList varchar(100)
SET #InList = '1,2,3,4'
SELECT MyTable.*
FROM MyTable
JOIN DelimitedSplit8K (#InList,',') SplitString
ON MyTable.Id = SplitString.Item
Or using dynamic SQL.
DECLARE #InList varchar(100)
SET #InList = '1,2,3,4'
DECLARE #sql nvarchar(1000)
SET #sql = 'SELECT * ' +
'FROM MyTable ' +
'WHERE Id IN ('+#InList+') '
EXEC sp_executesql #sql
SearchList = ',' + inputSearchTerm + ','; /* e.g. inputSearchTerm is '1,2,3' */
SELECT * FROM PERSON WHERE CONTAINS(SearchList, ',' + cast(PERSON_ID as varchar) + ',');
Because contains seems like overkill (it is designed for fuzzy searching and uses a full text index), because charindex() is not standard SQL, and I abhor answers where varchar does not have length, let me give an alternative:
SELECT *
FROM PERSON
WHERE ','+#SearchList+',' like '%,'+cast(PERSON_ID as varchar(255))+',%';
The concatenation of commas for #SearchList makes sure that all values are surrounded by delimiters. These are then put around the particular value, to prevent 1 from matching 10.
Note that this will not be particularly efficient, because it will require a full table scan.
Here's a way of doing it using a recursive CTE:
declare #SearchList varchar(20)
set #SearchList= '1,2,3'
;with cte as
(select case charindex(',',#SearchList)
when 0 then cast(#SearchList as int)
else cast(left(#SearchList,charindex(',',#SearchList)-1) as int)
end searchVal,
case charindex(',',#SearchList)
when 0 then ''
else right(#SearchList,
len(#SearchList)-charindex(',',#SearchList) )
end remainStr
union all
select case charindex(',',remainStr)
when 0 then cast(remainStr as int)
else cast(left(remainStr,charindex(',',remainStr)-1) as int)
end searchVal,
case charindex(',',remainStr)
when 0 then ''
else right(remainStr,
len(remainStr)-charindex(',',remainStr) )
end remainStr
from cte
where remainStr > ''
)
select p.*
from cte
join person p on cte.searchVal = p.person_id
SQLFiddle here.

Finding Uppercase Character then Adding Space

I bought a SQL World City/State database. In the state database it has the state names pushed together. Example: "NorthCarolina", or "SouthCarolina"...
IS there a way in SQL to loop and find the uppercase characters and add a space???
this way "NorthCarolina" becomes "North Carolina"???
Create this function
if object_id('dbo.SpaceBeforeCaps') is not null
drop function dbo.SpaceBeforeCaps
GO
create function dbo.SpaceBeforeCaps(#s varchar(100)) returns varchar(100)
as
begin
declare #return varchar(100);
set #return = left(#s,1);
declare #i int;
set #i = 2;
while #i <= len(#s)
begin
if ASCII(substring(#s,#i,1)) between ASCII('A') and ASCII('Z')
set #return = #return + ' ' + substring(#s,#i,1)
else
set #return = #return + substring(#s,#i,1)
set #i = #i + 1;
end;
return #return;
end;
GO
Then you can use it to update your database
update tbl set statename = select dbo.SpaceBeforeCaps(statename);
There's a couple ways to approach this
Construct a function using a pattern and the PATINDEX feature.
Chain minimal REPLACE statements for each case (e.g. REPLACE(state_name, 'hC', 'h C' for your example case). This seems is kind of a hack, but might actually give you the best performance, since you have such a small set of replacements.
If you absolutely cannot create functions and need this as a one-off, you can use a recursive CTE to break the string up (and add the space at the same time where required), then recombine the characters using FOR XML. Elaborate example below:
-- some sample data
create table #tmp (id int identity primary key, statename varchar(100));
insert #tmp select 'NorthCarolina';
insert #tmp select 'SouthCarolina';
insert #tmp select 'NewSouthWales';
-- the complex query updating the "statename" column in the "#tmp" table
;with cte(id,seq,char,rest) as (
select id,1,cast(left(statename,1) as varchar(2)), stuff(statename,1,1,'')
from #tmp
union all
select id,seq+1,case when ascii(left(rest,1)) between ascii('A') and ascii('Z')
then ' ' else '' end + left(rest,1)
, stuff(rest,1,1,'')
from cte
where rest > ''
), recombined as (
select a.id, (select b.char+''
from cte b
where a.id = b.id
order by b.seq
for xml path, type).value('/','varchar(100)') fixed
from cte a
group by a.id
)
update t
set statename = c.fixed
from #tmp t
join recombined c on c.id = t.id
where statename != c.fixed;
-- check the result
select * from #tmp
----------- -----------
id statename
----------- -----------
1 North Carolina
2 South Carolina
3 New South Wales