Convert sql subquery to list or varchar - sql-server

I want to know if we can convert a subquery result to comma separated list in varchar datatype.
For eg.
If I have a product table. And I have product image table with foreign key of product.
now I want to list all product with select query that should have a column with list of productImage table's pk list for each product.
I'm using sql server 2005. Can we achieve the above in any way?

Select p.ProductID,
Stuff((Select ','+Cast(ImageID as varchar(10))
From #ProductImages i
Where p.ProductID=i.ProductId
For XML PATH('')
),1,1,''
) as ImageList
From #Products p
Where p.ProductID in (Select ProductID From #ProductImages)
Here are test data I used for this query
Declare #Products Table (ProductID int primary key, ProductName varchar(20))
Declare #ProductImages Table (ProductId int, ImageId int, Primary Key (ProductId, ImageId))
Insert Into #Products
Select 1, 'Product1' Union all
Select 2, 'Product1' Union all
Select 3, 'Product1' Union all
Select 4, 'Product1' Union all
Select 5, 'Product1'
Insert Into #ProductImages
Select 1,1 Union all
Select 1,2 Union all
Select 1,3 Union all
Select 2,4 Union all
Select 2,5 Union all
Select 3,1 Union all
Select 4,3 Union all
Select 4,5
And here is result of query:
ProductID ImageList
--------- ---------
1 1,2,3
2 4,5
3 1
4 3,5
If you want to have ProductID 5 in the list with null for Image list, just remove next line from query:
Where p.ProductID in (Select ProductID From #ProductImages)
You will have one more row in the result (it does not have images assigned):
5 null

I'm not sure if I'm following you, but maybe you can try to add an extra table:
Table Product
Table Images
Table Product_Images.
In this last table you'll have at least 3 columns, PK for Product_Images table, Product_FK and
Images_FK. Then with just
SELECT Image_FK FROM Product_Images WHERE Product_Images.Product_FK = ##;
you'll have a list of Images' PK associated with a product
Hope this will help you,
regards.

Not easily, no. I've found the best way to do these things is to execute the subquery on its own, and assemble the comma-separated list programmatically.
If you really need to do it on the database, you could define a scalar-valued function that you can give the value for the foreign key, and which returns the comma-separated list. You'll have to use a cursor inside that function to make the magic happen though.

Related

JSON_VALUE is not working in where clause in SQL Server

I have JSON data in a column in my table. I am trying to apply where condition on the JSON column and fetch records.
Employee table:
Here is my SQL query:
SELECT ID, EMP_NAME
FROM EMPLOYEE
WHERE JSON_VALUE(TEAM, '$') IN (2, 3, 4, 5, 7, 10)
I am getting an empty result when I use this query. Any help on how to do this?
You need to parse the JSON in the TEAM column with OPENJSON():
Table:
CREATE TABLE EMPLOYEE (
ID int,
EMP_NAME varchar(50),
TEAM varchar(1000)
)
INSERT INTO EMPLOYEE (ID, EMP_NAME, TEAM)
VALUES
(1, 'Name1', '[2,11]'),
(2, 'Name2', '[2,3,4,5,7,10]'),
(3, 'Name3', NULL)
Statement:
SELECT DISTINCT e.ID, e.EMP_NAME
FROM EMPLOYEE e
CROSS APPLY OPENJSON(e.TEAM) WITH (TEAM int '$') j
WHERE j.TEAM IN (2,3,4,5,7,10)
Result:
ID EMP_NAME
1 Name1
2 Name2
As an additional option, if you want to get the matches as an aggregated text, you may use the following statement (SQL Server 2017 is needed):
SELECT e.ID, e.EMP_NAME, a.TEAM
FROM EMPLOYEE e
CROSS APPLY (
SELECT STRING_AGG(TEAM, ',') AS TEAM
FROM OPENJSON(e.TEAM) WITH (TEAM int '$')
WHERE TEAM IN (2,3,4,5,7,10)
) a
WHERE a.TEAM IS NOT NULL
Result:
ID EMP_NAME TEAM
1 Name1 2
2 Name2 2,3,4,5,7,10
JSON_VALUE returns a scalar value, not a data set, which you appaer to think it would. If you run SELECT JSON_VALUE('[2,3,4,5,7,10]','$') you'll see that it returns NULL, so yes, no rows will be returned.
You need to treat the JSON like a data set, not a single value:
SELECT ID, EMP_NAME
FROM EMPLOYEE E
WHERE EXISTS (SELECT 1
FROM OPENJSON (E.TEAM) OJ
WHERE OJ.Value IN (2,3,4,5,7,10))

Remove string portion from inconsistent string of comma-separated values

SQL Server 2017 on Azure.
Given a field called Categories in a table called dbo.sources:
ID Categories
1 ABC01, FFG02, ERERE, CC201
2 GDF01, ABC01, GREER, DS223
3 DSF12, GREER
4 ABC01
5 NULL
What is the syntax for a query that would remove ABC01 from any record where it exists, but keep the other codes in the string?
Results would be:
ID Categories
1 AFFG02, ERERE, CC201
2 GDF01, GREER, DS223
3 DSF12, GREER
4 NULL
5 NULL
Normalising and then denormalising your data, you can do this:
USE Sandbox;
GO
CREATE TABLE dbo.Sources (ID int,
Categories varchar(MAX));
INSERT INTO dbo.Sources
VALUES (1,'ABC01,FFG02,ERERE,CC201'), --I **assume you don't really have the space)
(2,'GDF01,ABC01,GREER,DS223'),
(3,'DSF12,GREER'),
(4,'ABC01'),
(5,NULL);
GO
DECLARE #Source varchar(5) = 'ABC01'; --Value to remove
WITH CTE AS(
SELECT S.ID,
STRING_AGG(NULLIF(SS.[value],#Source),',') WITHIN GROUP(ORDER BY S.ID) AS Categories
FROM dbo.Sources S
CROSS APPLY STRING_SPLIT(S.Categories,',') SS
GROUP BY S.ID)
UPDATE S
SET Categories = C.Categories
FROM dbo.Sources S
JOIN CTE C ON S.ID = C.ID;
GO
SELECT ID,
Categories
FROM dbo.Sources
GO
DROP TABLE dbo.Sources;
Although this seems like a bit overkill, compared to the REPLACE, it shows why normalising it is a far better idea in the first place, and how simple it is to actually do so.
You can use Replace as follows:
update dbo.sources set
category = replace(replace(category,'ABC01',''),', ','')
where category like '%ABC01%'

How to check for a specific condition by looping through every record in SQL Server?

I do have following table
ID Name
1 Jagan Mohan Reddy868
2 Jagan Mohan Reddy869
3 Jagan Mohan Reddy
Name column size is VARCHAR(55).
Now for some other task we need to take only 10 varchar length i.e. VARCHAR(10).
My requirement is to check that after taking the only 10 bits length of Name column value for eg if i take Name value of ID 1 i.e. Jagan Mohan Reddy868 by SUBSTRING(Name, 0,11) if it equals with another row value. here in this case the final value of SUBSTRING(Jagan Mohan Reddy868, 0,11) is equal to Name value of ID 3 row whose Name is 'Jagan Mohan Reddy'. I need to make a list of those kind rows. Can somebody help me out on how can i achieve in SQL Server.
My main check is that the truncated values of my Name column should not match with any non truncated values of Name column. If so i need to get those records.
Assuming I understand the question, I think you are looking for something like this:
Create and populate sample data (Please save us this step in your future questions)
DECLARE #T as TABLE
(
Id int identity(1,1),
Name varchar(15)
)
INSERT INTO #T VALUES
('Hi, I am Zohar.'),
('Hi, I am Peled.'),
('Hi, I am Z'),
('I''m Zohar peled')
Use a cte with a self inner join to get the list of ids that match the first 10 chars:
;WITH cte as
(
SELECT T2.Id As Id1, T1.Id As Id2
FROM #T T1
INNER JOIN #T T2 ON LEFT(T1.Name, 10) = t2.Name AND T1.Id <> T2.Id
)
Select the records from the original table, inner joined with a union of the Id1 and Id2 from the cte:
SELECT T.Id, Name
FROM #T T
INNER JOIN
(
SELECT Id1 As Id
FROM CTE
UNION
SELECT Id2
FROM CTE
) U ON T.Id = U.Id
Results:
Id Name
----------- ---------------
1 Hi, I am Zohar.
3 Hi, I am Z
Try this
SELECT Id,Name
FROM(
SELECT *,ROW_NUMBER() OVER(PARTITION BY Name, LEFT(Name,11) ORDER BY ID) RN
FROM Tbale1 T
) Tmp
WHERE Tmp.RN = 1
loop over your column for all the values and put your substring() function inside this loop and I think in Sql index of string starts from 1 instead of 0. If you pass your string to charindex() like this
CHARINDEX('Y', 'Your String')
thus you will come to know whether it is starting from 0 or 1
and you can save your substring value as value of other column with length 10
I hope it will help you..
I think this should cover all the cases you are looking for.
-- Create Table
DECLARE #T as TABLE
(
Id int identity(1,1),
Name varchar(55)
)
-- Create Data
INSERT INTO #T VALUES
('Jagan Mohan Reddy868'),
('Jagan Mohan Reddy869'),
('Jagan Mohan Reddy'),
('Mohan Reddy'),
('Mohan Reddy123551'),
('Mohan R')
-- Get Matching Items
select *, SUBSTRING(name, 0, 11) as ShorterName
from #T
where SUBSTRING(name, 0, 11) in
(
-- get all shortnames with a count > 1
select SUBSTRING(name, 0, 11) as ShortName
from #T
group by SUBSTRING(name, 0, 11)
having COUNT(*) > 1
)
order by Name, LEN(Name)

Best approach to merge three distinct TSQL select statments

I'm looking to return one tsql statement, that contains four fields, from three separate, unrelated tables.
One table contains a list of objects, say fruits, and for each fruit, I want a sell buy date and best before date.
First statement would therefore look something like:
select fruit from fruit table -- this returns multiple rows
Second statement would look something like:
select sellbuyDate from sellTable -- this returns a single row
and the third would look something like:
select bestbefore from bestTable -- this returns a single row
Don't get to hung up on the table names. I'm working on a legacy system, that we cant change, so need to combine the three table into one.
The underlining table needs to have all the fields returned in a single row, with the second and third results applied to the first statement.
Apples | 12-12-2008 | 12-12-2009
Pears | 12-12-2008 | 12-12-2009
I've implemented the following temp table:
CREATE TABLE #Fruits
(
Fruit VARCHAR(100),
SellBuyDate DATETIME,
BestBefore DATETIME
)
INSERT INTO #Fruits
SELECT Fruit from fruit
INSERT INTO #Fruits
SELECT sellbuyDate from sellTable
INSERT INTO #Fruits
SELECT bestbefore from bestable
SELECT * from #Fruits
This throws an error, because each insert doesn't contain the three fields specified.
any other suggestions would be well received.
You can select them all together by doing a CROSS JOIN by not specifying any join criteria between the three tables as follows:
CREATE TABLE fruit ( fruit_name VARCHAR(30) );
CREATE TABLE sellTable ( sellByDate DATETIME );
CREATE TABLE bestTable ( bestBefore DATETIME );
CREATE TABLE allFruits
(
fruit_name VARCHAR(30),
sellByDate DATETIME,
bestBefore DATETIME
);
INSERT INTO fruit (fruit_name)
VALUES ('apple'), ('pear');
INSERT INTO sellTable(sellByDate)
VALUES ('12/05/2012');
INSERT INTO bestTable(bestBefore)
VALUES ('12/12/2012');
INSERT INTO allFruits (fruit_name, bestBefore, sellByDate)
SELECT f.fruit_name, b.bestBefore, s.sellByDate
FROM fruit f, bestTable b, sellTable s;
SELECT *
FROM allFruits;
maybe you are looking for this answer, although your question could be a little clearer on what that would accomplish
INSERT INTO #Fruits(fruit)
SELECT Fruit from fruit
INSERT INTO #Fruits(sellbuyDate)
SELECT sellbuyDate from sellTable
INSERT INTO #Fruits(bestbefore)
SELECT bestbefore from bestable
SELECT * from #Fruits
the other possible solution is
insert into #Fruits
select Fruit, sellbuyDate, bestbefore from fruit
cross join sellTable
cross join bestable
May be you need this.
SELECT FRUIT,
(SELECT SELLBUYDATE FROM SELLTABLE) AS SELLBUYDATE,
(SELECT BESTBEFORE FROM BESTTABLE) AS BESTBEFORE
FROM FRUIT
or
SELECT FRUIT.FRUIT
, SELLTABLE.SELLBUYDATE
, BESTTABLE.BESTBEFORE
FROM FRUIT
INNER JOIN SELLTABLE ON 1=1
INNER JOIN BESTTABLE ON 1=1
Without the schema of the 3 tables. I would guess its one of the following.
CREATE TABLE #Fruits
(
Fruit VARCHAR(100),
SellBuyDate DATETIME,
BestBefore DATETIME
)
INSERT INTO #Fruits(Fruit,SellBuyDate,BestBefore)
Select Fruit, Sellbuydate,bestbefore
from fruit,selltable,bestable
Or if your tables are set up properly
INSERT INTO #Fruits(Fruit,SellBuyDate,BestBefore)
Select Fruit, Sellbuydate,bestbefore
from fruit f
inner join selltable s
on f.pkey = s.fkey
inner join bestable b
on f.pkey = b.fkey

SQL Server: Joining in rows via. comma separated field

I'm trying to extract some data from a third party system which uses an SQL Server database. The DB structure looks something like this:
Order
OrderID OrderNumber
1 OX101
2 OX102
OrderItem
OrderItemID OrderID OptionCodes
1 1 12,14,15
2 1 14
3 2 15
Option
OptionID Description
12 Batteries
14 Gift wrap
15 Case
[etc.]
What I want is one row per order item that includes a concatenated field with each option description. So something like this:
OrderItemID OrderNumber Options
1 OX101 Batteries\nGift Wrap\nCase
2 OX101 Gift Wrap
3 OX102 Case
Of course this is complicated by the fact that the options are a comma separated string field instead of a proper lookup table. So I need to split this up by comma in order to join in the options table, and then concat the result back into one field.
At first I tried creating a function which splits out the option data by comma and returns this as a table. Although I was able to join the result of this function with the options table, I wasn't able to pass the OptionCodes column to the function in the join, as it only seemed to work with declared variables or hard-coded values.
Can someone point me in the right direction?
I would use a splitting function (here's an example) to get individual values and keep them in a CTE. Then you can join the CTE to your table called "Option".
SELECT * INTO #Order
FROM (
SELECT 1 OrderID, 'OX101' OrderNumber UNION SELECT 2, 'OX102'
) X;
SELECT * INTO #OrderItem
FROM (
SELECT 1 OrderItemID, 1 OrderID, '12,14,15' OptionCodes
UNION
SELECT 2, 1, '14'
UNION
SELECT 3, 2, '15'
) X;
SELECT * INTO #Option
FROM (
SELECT 12 OptionID, 'Batteries' Description
UNION
SELECT 14, 'Gift Wrap'
UNION
SELECT 15, 'Case'
) X;
WITH N AS (
SELECT I.OrderID, I.OrderItemID, X.items OptionCode
FROM #OrderItem I CROSS APPLY dbo.Split(OptionCodes, ',') X
)
SELECT Q.OrderItemID, Q.OrderNumber,
CONVERT(NVarChar(1000), (
SELECT T.Description + ','
FROM N INNER JOIN #Option T ON N.OptionCode = T.OptionID
WHERE N.OrderItemID = Q.OrderItemID
FOR XML PATH(''))
) Options
FROM (
SELECT N.OrderItemID, O.OrderNumber
FROM #Order O INNER JOIN N ON O.OrderID = N.OrderID
GROUP BY N.OrderItemID, O.OrderNumber) Q
DROP TABLE #Order;
DROP TABLE #OrderItem;
DROP TABLE #Option;

Resources