How can i use the replace statement in query - sql-server-2012

I am trying to query a table that has values separated by commas as follows:
SELECT ID, NAME,FULLNAME,STATUS,STORE
FROM EMPLOYEE
WHERE STORE IN (SELECT '''' + REPLACE('001,002',',',''',''') +'''')
ORDER BY STORE
when I run the query above, it produces no results,
but when I run it like this:
SELECT ID, NAME,FULLNAME,STATUS,STORE
FROM EMPLOYEE
WHERE STORE IN ('001','002')
ORDER BY STORE
I get like 500 records.
And when I try this:
SELECT ('''' + REPLACE('001,002',',',''',''') +'''')
I get the result '001','002'
so my question is, why the first script does not work, and produces no results?
Is there something I must add to the script for it to work?
please advise.

What if I had this scenario
SELECT ID, NAME,FULLNAME,STATUS,STORE
FROM dbo.EMPLOYEE
WHERE STORE IN (
SELECT t2.ID
FROM (
SELECT Value = REPLACE('001,002', ',', '.')
) t
CROSS APPLY (
VALUES
(PARSENAME(t.Value, 1)),
(PARSENAME(t.Value, 2))
) t2 (ID)
)
AND STATUS IN (
SELECT t2.ID1
FROM (
SELECT Value1 = REPLACE('A,T,L', ',', '.')
) t1
CROSS APPLY (
VALUES
(PARSENAME(t1.Value1, 1)),
(PARSENAME(t1.Value1, 2))
) t2 (ID1)
)
ORDER BY STORE
I tried that, and it didn't work, so I am just wondering if it works for more than 1 condition.

Related

TSQL get column names from dynamic query with formatting/conditions

I need to identify list of columns and its original value from dynamic SQL.
Example: I have this SQL statement:
#tsql = N'SELECT A.ID AS PROCESSID, COALESCE(STEP_ID,'''') AS programmid, NAME FROM DBO.TBL_LOG A'
As per this solution It will just return column name. I need both column name and its original value.
Expected result :
Can anyone please help with this? Thank you in advance.
Below code works for me temporary , If there will be too complex statement in select clause than it will not work.
;WITH CTE AS
(
SELECT NAME , SUBSTRING(LTRIM(#TSQL),7,CHARINDEX('FROM',LTRIM(#TSQL),0)-7) as QRY FROM sys.dm_exec_describe_first_result_set (#TSQL, null, 0)
)
,COLPOS as
(
select NAME,QRY, CHARINDEX(NAME ,QRY,LEN(QRY) - CHARINDEX(REVERSE(NAME),REVERSE(QRY),0)- LEN(NAME)) AS POS
FROM CTE
)
,POS AS (
SELECT NAME,QRY,POS,LAG(POS) OVER (order by (select 1)) LGPOS,LEAD(POS) OVER (order by (select 1)) LEADPOS
,LAG(NAME) OVER (order by (select 1)) LGNAME
FROM COLPOS
)
SELECT NAME , LTRIM(RTRIM(SUBSTRING (QRY,COALESCE(LGPOS+LEN(LGNAME),0),POS+LEN(NAME)-COALESCE(LGPOS+LEN(LGNAME),0)))) AS EXPRESSION
FROM POS

SQL - multi select query returning no results

I have a query where I'm trying to select a Row Number from a table that meets a certain criteria from a separate table.
The current query returns 0 results when I'm expecting 1 number
SELECT
RowNum
FROM
(SELECT
ID, Name, RowNum = ROW_NUMBER() OVER (ORDER BY ID)
FROM
tblEncroachmentTypes) AS temp
WHERE
temp.Name LIKE (SELECT EN_TYPE
FROM LakeEncroachments
WHERE EN_ID = '0526')
I have created a temp table to try and simplify it, but it still returns no results
select RowNum
from #temp1
where #temp1.Name like (select EN_TYPE from LakeEncroachments where EN_ID = '0526')
I'm trying to give as much information as possible, but not sure what else I need.
If you need to use like, you might need to add the wildcards:
SELECT RowNum
from (Select ID, Name, RowNum = ROW_NUMBER() over (order by ID) from tblEncroachmentTypes) as temp
where temp.Name Like '%'+(Select EN_TYPE from LakeEncroachments WHERE EN_ID = '0526')+'%'
reformat looks like this:
select RowNum
from (
select ID
, name
, RowNum = row_number() over (
order by ID
)
from tblEncroachmentTypes
) as temp
where temp.name like '%' + (
select EN_TYPE
from LakeEncroachments
where EN_ID = '0526'
) + '%'
Also, if your sub query for like returns more than one value, you'll need a different approach.
if your subquery give multiple rows, use this query
WITH temp as
(SELECT ID, Name, ROW_NUMBER() OVER (ORDER BY ID) as RowNum
FROM tblEncroachmentTypes) AS temp
SELECT temp.RowNum
FROM LakeEncroachments
INNER JOIN temp ON temp.Name LIKE REPLACE(REPLACE(LakeEncroachments.EN_TYPE, '-', '% '), ' ', '% ') + '%'
WHERE EN_ID = '0526'

SQL Having count logic

i need help on HAVING COUNT , i have a result set of data below:
CREATE TABLE #tmpTest1 (Code VARCHAR(50), Name VARCHAR(100))
INSERT INTO [#tmpTest1]
(
[Code],
[Name]
)
SELECT '160215-039','ROBIN'
UNION ALL SELECT '160215-039','ROBIN'
UNION ALL SELECT '160215-046','SENGAROB'
UNION ALL SELECT '160215-046','BABYPANGET'
UNION ALL SELECT '160215-045','JONG'
UNION ALL SELECT '160215-045','JAPZ'
UNION ALL SELECT '160215-044','AGNES'
UNION ALL SELECT '160215-044','AGNES'
UNION ALL SELECT '160215-041','BABYTOT'
UNION ALL SELECT '160215-041','BABYTOT'
UNION ALL SELECT '160215-041','BABYTOT'
i want to show only the rows that have the same code but different name , so in this case my expected result is below since those are have the same code but different name:
160215-045 JAPZ
160215-045 JONG
160215-046 BABYPANGET
160215-046 SENGAROB
but when i try to group the two columns then use the having count, below is my query:
SELECT [Code], [Name] FROM [#tmpTest1]
GROUP BY [Code], [Name] HAVING COUNT([Code]) > 1
It gives me wrong result below which have the rows that have the same code and name, it is the opposite of what i want.
160215-044 AGNES
160215-041 BABYTOT
160215-039 ROBIN
How can i get my expected output ?
Thanks in advance, any help would much appreciated.
I believe this query will give you the result you want, although your original question is a bit unclear.
SELECT t1.[Code], t1.[Name]
FROM [#tmpTest1] t1
INNER JOIN
(
SELECT [Code]
FROM [#tmpTest1]
GROUP BY [Code]
HAVING COUNT(DISTINCT [Name]) > 1
) t2
ON t1.[Code] = t2.[Code]
Follow the link below for a running demo:
SQLFiddle
If you want rows with the same code and name, then use window functions:
select t.*
from (select t.*, count(*) over (partition by code, name) as cnt
from #temptest1 t
) t
where cnt >= 2;
From your comment
if there is 1 different name for the codes , i want to show those
records for me to know that there is one differs to others..
This sounds like an exists query because you want to check if another row with the same code but different name exists.
select * from [#tmpTest1] t1
where exists (
select 1 from [#tmpTest] t2
where t2.code = t1.code
and t2.name <> t1.name
)

Select from table with multiple values

My query is
SELECT *
FROM tblalumni_member
WHERE username IN ( SELECT *
FROM Split('ramesh,sagar,pravin', ',') )
AND Is_Approved = 1
and username field contains multiple values separated by comma. Like ramesh,sachin,pravin.
It should give all rows if any of get matched in selected result.
Can you please help me?
In case of SQL Server perhaps something like
SELECT *
FROM tblalumni_member m
WHERE Is_Approved = 1 AND
EXISTS(
SELECT * FROM Split(m.username, ',')
INTERSECT
SELECT * FROM Split('ramesh,sagar,pravin', ',')
)

Use a named custom column in SQL 2005 in WHERE clause?

Can I name a custom column in the SELECT statement and reference that in the WHERE clause without duplicating code?
For example;
SELECT RIGHT(REPLICATE('0', 5) + RTRIM(SOME_ID)), 5) AS DISPLAY_ID
FROM dbo.MY_TABLE
WHERE DISPLAY_ID LIKE '%005%'
Only much more complicated. I want to maintain this code in one place only but SQL Server 2005 forces me to duplicate the custom SELECT in the WHERE clause.
I believe this was possible in Microsoft SQL Server 2000 but no longer in 2005.
Thanks.
You can do this using either a SUB SELECT or a CTE function
SELECT *
FROm (
SELECT RIGHT(REPLICATE('0', 5) + RTRIM(SOME_ID), 5) AS DISPLAY_ID
FROM MY_TABLE
) sub
WHERE DISPLAY_ID LIKE '%005%'
OR
;WITH Tbl AS(
SELECT RIGHT(REPLICATE('0', 5) + RTRIM(SOME_ID), 5) AS DISPLAY_ID
FROM MY_TABLE
)
SELECT *
FROM Tbl
WHERE DISPLAY_ID LIKE '%005%'
One of the times that I am aware of that you can use the column alias, is when you wish to ORDER BY that column alias.
EDIT:
Multiple CTE blocks
DECLARE #MY_TABLE TABLE(
SOME_ID INT
)
DECLARE #Your_TABLE TABLE(
SOME_ID INT
)
;WITH Table1 AS(
SELECT *
FROM #MY_TABLE
),
Table2 AS(
SELECT *
FROM #Your_TABLE
)
SELECT *
FROM Table1 t1 INNER JOIN
Table2 t2 ON t1.SOME_ID = t2.SOME_ID
You can wrap it using a subselect a bit cleaner, like this:
SELECT DISPLAY_ID
FROM (SELECT RIGHT(REPLICATE('0', 5) + RTRIM(SOME_ID)), 5) AS DISPLAY_ID
FROM dbo.MY_TABLE) SubTable
WHERE DISPLAY_ID LIKE '%005%'