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 years ago.
Improve this question
I have an XML string stored in SQL. The program that inserts the XML doesn't tag all the objects inside.
Example of string:
<XMLInfo Sample1="1234" Sample2="1234" Sample3="1234" Sample4="1234" />
I need to delete the sample2 item completely. Other things complicating this is that Sample2 may not be followed by sample3 every time and sample2 does not have a fixed length.
SQL Server:
update test set data.modify('delete XMLInfo/#Sample2');
sql fiddle demo
PostgreSQL (I think there's no xml parsing/modifying functions, but there extensions):
create or replace function remove_attrib(data xml, attr text)
returns xml
as
$$
import xml.etree.ElementTree as ET
r = ET.fromstring(data)
del r.attrib[attr]
return ET.tostring(r)
$$ language plpython3u;
update test set data = remove_attrib(data, 'Sample2');
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I want to select data from a table with clause involving AND and OR.
Here is my sql query:
select *
from WORKTR
where status='Active' OR status='Inactive'and Start_date='2020-02-10'
However, the query above only return 3 from 10 rows in the table which means it does not return the all of the rows that obey the conditions. Please help me to show the right way to write the query.
You should wrap your condition correctly like below
where status='Active' OR (status='Inactive' and Start_date='2020-02-10')
Add parenthesis, according to your needs
select *
from WORKTR
where (status='Active' OR status='Inactive)'and Start_date='2020-02-10'
OR
select *
from WORKTR
where status='Active' OR (status='Inactive'and Start_date='2020-02-10')
OR
select *
from WORKTR
where status IN ('Active','Inactive') and Start_date='2020-02-10'
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 6 years ago.
Improve this question
I need to export values from an Sqlite table with 3 Columns to a CSV File.
Should write a loop and use the Select Query to Dump all Values or is there an inbuilt SQL Function to do this..
Im here
void writecsv()
{
char c[1000];
FILE *fptr;
fptr=fopen("output.csv","w");
}
Not built into the SQLite lib, but you could use the sqlite3 CLI with the commands .mode csv and .output. This is also scriptable by using an init file and reading the SQL commands from stdin/file.
I used the system call to do it
system("sqlite3 -header -csv 'db2.db' 'select * from tdata;' > out.csv");
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
Is it possible to use case statement inside stuff function with for xml path() ?
Yes you can use a 'CASEstatement insideSTUFFwhen usingFOR XML PATH`
SELECT STUFF('alpha',3,5,CASE WHEN 1=1 THEN 'a' ELSE 'b' END) FOR XML PATH('test')
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 8 years ago.
Improve this question
In Sql Server I'd like to be able to return a result form my query if there is an exact match, i.e. I only match on one row, and if there isn't I'd like to return no result at all.
I realize it's fairly easy to handle this in code, once the sql query has returned, but I'd like to be able to do the test in Sql Server, if it's achievable reasonably easily.
If I understand correctly, you ONLY want to get the data back if you match one and only one record.
One way to do this is to use an intermediate result set and COUNT or ##ROWCOUNT
You can put your results into a #TEMP table or #Table variable, then inspect either that table or the ##ROWCOUNT variable to determine if you got one row back. If you do, return the intermediate result set.
If this is SQL Server 2005 or later version, you can use COUNT() OVER like this:
WITH matches AS (
SELECT
...,
TotalMatches = COUNT(*) OVER ()
FROM ...
WHERE ...
)
SELECT *
FROM matches
WHERE TotalMatches = 1
;
The matches CTE finds all rows matching the main requirement and returns them along with the total count of matches. The main query returns the found results only if the additional requirement, total match count of 1, is satisfied.
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 8 years ago.
Improve this question
I have a question about SQL Server's IF clause.
For example - look at this code:
IF (#logpath IS NOT NULL || #ptd IS NOT NULL)
Can I use || in the condition when I use IF statement?
Is this correct?
|| is not valid Transact-SQL syntax. You need to user 'OR' or 'AND'
Example
IF #logpath is not null OR #ptd is not null
BEGIN
-- do something
END
You cannot use the || operator in SQL Server. Please refer the following link which shows the list of logical operators in SQL Server
http://msdn.microsoft.com/en-us/library/ms189773.aspx