SQL query trim a column? - sql

I am using the following query to populate some data. From column "query expression" is there a way to remove any text that is to the left of N'Domain\
Basically I only want to see the text after N'Domain\ in the column "Query Expression" Not sure how to do this.
SELECT
v_DeploymentSummary.SoftwareName,
v_DeploymentSummary.CollectionName,
v_CollectionRuleQuery.QueryExpression
FROM
v_DeploymentSummary
INNER JOIN v_CollectionRuleQuery
ON v_DeploymentSummary.CollectionID = v_CollectionRuleQuery.CollectionID

At least for SQL Server:
SUBSTRING([v_CollectionRuleQuery.QueryExpression], CHARINDEX('N''Domain\', [v_CollectionRuleQuery.QueryExpression]) + 9, LEN([v_CollectionRuleQuery.QueryExpression])
Give it a try.
I didn't understand if you wanted to include N'Domain\ in your string if that's the case just remove the +9.
In my understanding you want something like this:
SELECT
v_DeploymentSummary.SoftwareName,
v_DeploymentSummary.CollectionName,
SUBSTRING([v_CollectionRuleQuery.QueryExpression], CHARINDEX('N''Domain\', [v_CollectionRuleQuery.QueryExpression]) + 9, LEN([v_CollectionRuleQuery.QueryExpression])
FROM
v_DeploymentSummary
INNER JOIN v_CollectionRuleQuery
ON v_DeploymentSummary.CollectionID = v_CollectionRuleQuery.CollectionID

Try this, It will not change values for QueryExpression without the text N'Domain and does not require that N'Domain is the first text:
SELECT
ds.SoftwareName,
ds.CollectionName,
crq.QueryExpression
STUFF(crq.QueryExpression, 1, charindex('N''Domain\',
REPLICATE('w', LEN('N''Domain\')-1) + crq.QueryExpression), '')
FROM
v_DeploymentSummary ds
JOIN
v_CollectionRuleQuery crq
ON ds.CollectionID = crq.CollectionID
Example:
SELECT
STUFF(x, 1, charindex('N''Domain\',
replicate('w', LEN('N''Domain\')-1) + x), '')
FROM (values('N''Domain\xxx'),('N''Doma'),('xxN''Domain\yyy')) x(x)
Result:
xxx
N'Doma
yyy

In SQL Server, you can use stuff() for this purpose:
SELECT ds.SoftwareName, ds.CollectionName,
STUFF(crq.QueryExpression, 1,
CHARINDEX('Domain\', rq.QueryExpression) + LEN('Domain\') - 1,
'')
FROM v_DeploymentSummary ds INNER JOIN
v_CollectionRuleQuery crq
ON ds.CollectionID = crq.CollectionID;
Note the use of table aliases makes the query easier to write and to read.

Related

SQL - Cannot find either column "c" or user-defined function or aggregate "c" or the name is ambiguos

I'm not really good in SQL and I did some searching and couldn't really figured it out how to use STUFF. I mean it looks simple but when I use it, there's always an error.
Anyway, here is the portion of my code and I just want to combine 2 printer of the rows with the same computer name pulled from database, honestly I'm not even sure if I'm doing it right but please do correct me if I'm wrong. Really need the professionals to guide me and do let me know if the information is not sufficient. Thanks in advance.
Update:
Added more code, and I didn't mention clearly, my query is pulling different data from multiple tables. It may be a bit messy but it got what I wanted, my problem is combining the printer result.
SELECT DISTINCT sc.ComputerName AS 'Computer name',
*
*
*
ISNULL(c.PrinterName <-- error on this c.printername
(
STUFF
((
SELECT
'; ' + c.PrinterName
FROM PrinterList AS c
WHERE c.ComputerName = sc.ComputerName
FOR XML PATH(''), TYPE).value('.','nvarchar(max)'), 1, 2,'')), '') AS 'Printer'
FROM Computer sc
Full Join PCInfo AS a
ON a.ComputerName = sc.ComputerName
Full Join DriverList AS b
ON b.ComputerName = sc.ComputerName
Full Join PrinterList AS c
ON c.ComputerName = sc.ComputerName
WHERE RTRIM(LTRIM(REPLACE(lower(sc.ComputerName), ' ',''))) like '%example%'
The c alias is only available in the namespace of the subquery, but you do not reference it from there.
I would expect something like this:
SELECT c.PrinterName,
STUFF( (SELECT '; ' + c.PrinterName
FROM PrinterList AS c
WHERE c.ComputerName = sc.ComputerName
FOR XML PATH(''), TYPE
).value('.', 'nvarchar(max)'
), 1, 2, '')
) as printers
FROM computer c ;
There is no need for the join in the outer query. I also don't see why an outer join would be desired.

Need to make a new column queryable

I created the column frfpost_short but now I am having trouble making the column usable in a query.
select t.corridor,
s.corridor_code_rb,t.roadway,s.SVYLENG2012,
round(cast(t.frfpost as float),3)) as frfpost_short,
s.FRFPOST,s.BEG_GN
from SEC_FILE_IMPORT_2014 t,NORTH_VAN_DATA_VIEW_MOD_032015 s,
where frfpost_short = s.FRFPOST -- This line is causing problems
-- I would like to make frfpost_short queryable
and t.corridor = s.CORRIDOR_CODE
order by 1
Use a subquery or repeat the expression. Aliases defined in the select are not available in the where:
select t.corridor,
s.corridor_code_rb,t.roadway, s.SVYLENG2012,
round(cast(t.frfpost as float), 3) as frfpost_short,
s.FRFPOST, s.BEG_GN
from SEC_FILE_IMPORT_2014 t join
NORTH_VAN_DATA_VIEW_MOD_032015 s
on round(cast(t.frfpost as float), 3) = s.FRFPOST and
t.corridor = s.CORRIDOR_CODE
order by 1
I fixed your query. In particular, learn to use explicit join syntax.
You can't use aliases in where clause, you need to use below solution
select t.corridor,
s.corridor_code_rb,t.roadway,s.SVYLENG2012,
round(cast(t.frfpost as float),3)) as frfpost_short,
s.FRFPOST,s.BEG_GN
from SEC_FILE_IMPORT_2014 t,NORTH_VAN_DATA_VIEW_MOD_032015 s,
where round(cast(t.frfpost as float),3)) = s.FRFPOST
and t.corridor = s.CORRIDOR_CODE
order by 1

How can I run a SQL query iteratively for every row in a table?

I have the following query:
DECLARE #AccString varchar(max)
SET #AccString=''
SELECT #Acctring=#AccString + description + ' [ ] '
FROM tl_sb_accessoryInventory ai
JOIN tl_sb_accessory a on a.accessoryID = ai.accessoryID
WHERE userID=6
SELECT userID, serviceTag, model, #AccString AS ACCESSORIES FROM tl_sb_oldLaptop ol
JOIN tl_sb_laptopType lt ON ol.laptopTypeID = lt.laptopTypeID
WHERE userID=6
which outputs this:
What I want to be able to do is run this for every userID in a table tl_sb_user.
The statement to get the userIDs is:
Select userID from tl_sb_user
How can I get this to output a row as above for each user?
You are trying to do a string concatenation subquery. In SQL Server, you need to do the string concatenation using a correlated subquery with for xml path. Arcane, but it generally works.
The results is something like this:
SELECT userID, serviceTag, model, #AccString AS ACCESSORIES,
stuff((select ' [ ] ' + description
from tl_sb_accessoryInventory ai join
tl_sb_accessory a
on a.accessoryID = ai.accessoryID
where a.userId = ol.UserId
for xml path ('')
), 1, 11, '') as accessories
FROM tl_sb_oldLaptop ol JOIN
tl_sb_laptopType lt
ON ol.laptopTypeID = lt.laptopTypeID;
You don't have table aliases identifying where the columns come from, so I am just guessing that a.userId = ol.UserId references the right tables.
Also, this substitutes certain characters with html forms. Notably '<' and '>' turn into things like '<' and '>'. When I encounter this problem, I use replace() to replace the values.
Simply leave out the WHERE clause.

SQL CONCAT IF Statement?

Morning All,
Im not to sure how i need to solve my following query... I have the following query which pulls back the desired records in SQL server...
SELECT agenda.AgendaItemNumber,Agenda.AgendaName, AgendaType.AgendaTypeDescription, userdetails.fullName
FROM Agenda
JOIN AgendaType ON AgendaType.AgendaTypeID=Agenda.AgendaTypeID
JOIN UserDetails ON Agenda.AgendaID = Userdetails.AgendaID
WHERE agenda.AgendaTypeID = '2'
AND AgendaItemNumber = AgendaItemNumber
AND AgendaName = AgendaName
AND AgendaTypeDescription = AgendaTypeDescription
AND AgendaItemNumber >= '3'
The above query works but i need to enhance this slightly. It pulls back the following results, which essentially are duplicate records except for the 'fullname' column...
What i would like to do is be able to add some extra code to this query so that when i run the query i am able to display one record for each 'AgendaItemNumber' and for it to concat both of the fullnames for this record. However i have additional AgendaItemsNumbers in this table that only have 1 x user fullname assigned to them. its just these few records within the image file i need to do something clever with.
Maybe there is a better way to complete this task?
Many thanks in advance. Any queries please dont hesitate to ask.
Regards
Betty
SELECT agenda.AgendaItemNumber,
Agenda.AgendaName,
AgendaType.AgendaTypeDescription,
STUFF(( SELECT ';' + FullName
FROM UserDetails
WHERE UserDetails.AgendaID = Agenda.AgendaID
FOR XML PATH('')
), 1, 1, '') AS fullName
FROM Agenda
INNER JOIN AgendaType
ON AgendaType.AgendaTypeID=Agenda.AgendaTypeID
INNER JOIN UserDetails
ON Agenda.AgendaID = Userdetails.AgendaID
WHERE agenda.AgendaTypeID = '2'
AND AgendaItemNumber = AgendaItemNumber
AND AgendaName = AgendaName
AND AgendaTypeDescription = AgendaTypeDescription
AND AgendaItemNumber >= '3'
ADENDUM
The XML extension in SQL-Server allows you to concatenate multiple rows into a single row. The actual intention of the extension is so you can output as XML (obviously), but there are some nifty tricks that are byproducts of the extensions. In the above query, if there were a column name in the subquery (FullName) it would output as <FullName>Joe Bloggs1</FullName><FullName>Joe Bloggs2</FullName>, because there is no column name it simply concatenates the rows (not forming proper XML). The PATH part allows you to specify an additional node, for example if you use PATH('Name') in the above you would get <Name>;Joe Bloggs</Name><Name>;Joe Bloggs2</Name> If you combine Path with a column name you would get Joe Bloggs.
Finally the STUFF just removes the semicolon at the start of the list.

naming columns in excel with Complex sql

I’m trying to run this SQL using get external.
It works, but when I try to rename the sub-queries or anything for that matter it remove it.
I tried as, as and the name in '', as then the name in "",
and the same with space. What is the right way to do that?
Relevant SQL:
SELECT list_name, app_name,
(SELECT fname + ' ' + lname
FROM dbo.d_agent_define map
WHERE map.agent_id = tac.agent_id) as agent_login,
input, CONVERT(varchar,DATEADD(ss,TAC_BEG_tstamp,'01/01/1970'))
FROM dbo.maps_report_list list
JOIN dbo.report_tac_agent tac ON (tac.list_id = list.list_id)
WHERE input = 'SYS_ERR'
AND app_name = 'CHARLOTT'
AND convert(VARCHAR,DATEADD(ss,day_tstamp,'01/01/1970'),101) = '09/10/2008'
AND list_name LIKE 'NRBAD%'
ORDER BY agent_login,CONVERT(VARCHAR,DATEADD(ss,TAC_BEG_tstamp,'01/01/1970'))
You could get rid of your dbo.d_agent_define subquery and just add in a join to the agent define table.
Would this code work?
select list_name, app_name,
map.fname + ' ' + map.lname as agent_login,
input,
convert(varchar,dateadd(ss,TAC_BEG_tstamp,'01/01/1970')) as tac_seconds
from dbo.maps_report_list list
join dbo.report_tac_agent tac
on (tac.list_id = list.list_id)
join dbo.d_agent_define map
on (map.agent_id = tac.agent_id)
where input = 'SYS_ERR'
and app_name = 'CHARLOTT'
and convert(varchar,dateadd(ss,day_tstamp,'01/01/1970'),101) = '09/10/2008'
and list_name LIKE 'NRBAD%'
order by agent_login,convert(varchar,dateadd(ss,TAC_BEG_tstamp,'01/01/1970'))
Note that I named your dateadd column because it did not have a name. I also tried to keep your convention of how you do a join. There are a few things that I would do different with this query to make it more readable, but I only focused on getting rid of the subquery problem.
I did not do this, but I would recommend that you qualify all of your columns with the table from which you are getting them.
To remove the sub query in the SELECT statement I suggest the following:
SELECT list_name, app_name, map.fname + ' ' + map.lname as agent_login, input, convert(varchar,dateadd(ss, TAC_BEG_tstamp, '01/01/1970))
FROM dbo.maps_report_list inner join
(dbo.report_tac_agent as tac inner join dbo.d_agent_define as map ON (tac.agent_id=map.agent_id)) ON list.list_id = tac.list_id
WHERE input = 'SYS_ERR' and app_name = 'CHARLOTT' and convert(varchar,dateadd(ss,day_tstamp,'01/01/1970'),101) = '09/10/2008'
and list_name LIKE 'NRBAD%' order by agent_login,convert(varchar,dateadd(ss,TAC_BEG_tstamp,'01/01/1970'))
I used parentheses to create the inner join between dbo.report_tac_agent and dbo.d_agent_define first. This is now a set of join data.
The combination of those tables are then joined to your list table, which I am assuming is the driving table here. If I am understand what you are trying to do with your sub select, this should work for you.
As stated by the other poster you should use table names on your columns (e.g. map.fname), it just makes things easy to understand. I didn't in my example because I am note 100% sure which columns go with which tables. Please let me know if this doesn't do it for you and how the data it returns is wrong. That will make it easier to solve in needed.