SQLite: Update field with result of another query - sql

I have a database with one table, let's say 'tablename'. Now, I want the first column of that table, named 'text' to be updated with the text it containts PLUS some new text which comes as result from another query executed over 'tablename2'. So, I want something like this:
UPDATE tablename SET text="current text" + (SELECT * FROM tablename2 where ID=12);
If 'text' value is 'Result not available' I want to append ' here', so that the field value is 'Result not available here'
How is this possible? Thanks

Try the concatenation operator (||):
UPDATE tablename SET text='current text'||(SELECT * FROM tablename2 where ID=12);
And I'm not sure, but you should use ' instead of " .

I probably misunderstood your question, but let's try:
http://sqlfiddle.com/#!5/959e5/2
UPDATE Table1
SET text = text ||
CASE
WHEN text = 'Result not available' THEN ' here'
ELSE (SELECT text FROM Table2 where id = 12)
END;

Related

Replace Function - Handling single quotes and forward slashes in SQL SERVER 2008

I want to replace some data from my database where single quotes and slashes are present.
The line below is exactly how it appears in the database and I only want to remove 'F/D', from the record.
('P/P','F/D','DFC','DTP')
Been using varations of
UPDATE tablename SET columnname = REPLACE(columnname, '''F/D,''', '')
WHERE RECORDID = XXXXX
Also been using varations of
UPDATE tablename SET columnname = REPLACE(columnname, 'F/D,', '')
WHERE RECORDID = XXXXX
Seems like it should be a simple fix but I haven't had any luck yet - all suggestions are appreciated.
The reason your's doesn't work is because you aren't including the quotes. You are looking for F/D, and 'F/D,' and your data it is 'F/D',.
If it's simply 'F/D' from all values you want removed, then you also need to remove a comma and the quotes. This method removes 'F/D' and then, any double commas (in case 'F/D' is in the middle of the string).
declare #var varchar(64) = '(''P/P'',''F/D'',''DFC'',''DTP'')'
select replace(replace(#var,'''F/D''',''),',,',',')
--update tablename
--set columnname = replace(replace(columnname,'''F/D''',''),',,',',')
--where RECORDID = 1324
If you want to replace the second element in the string, here is a way:
select
#var
--find the location of the first comma
,charindex(',',#var,0)
--find the location of the second comma
,charindex(',',#var,charindex(',',#var) + 1)
--Put it all together, using STUFF to replace the values between this range with nothing
,stuff(#var,charindex(',',#var,0),charindex(',',#var,charindex(',',#var) + 1) - charindex(',',#var,0),'')
Your first version should work fine if the comma is in the right place:
UPDATE tablename
SET columnname = REPLACE(columnname, '''F/D'',', '')
WHERE RECORDID = XXXXX;
Note that this will not replace 'F/D' if it is the first or last element in the value. If that is an issue, I would suggest that you ask another question.

SQL: How to make a replace on the field ''

I have a very but tricky question for you guys. So, listen I have a field with spaces and numbers in one of my table columns. The key part is transform the content in a decimal field. The drawback is basically that for some rows I could get something like:
' 1584.00 '
' 156546'
'545.00 '
' '
So, to clean up my column, I have done a LTRIM and RTRIM so spaces gone. So now for a couple of records where the record were just spaces the new content is ''. Finally I need to convert this result to a decimal.
Issue: The thing is that for field that contend just the spaces the new result is '' and I'm not able to apply a REPLACE on this because it's a blank and the code below doesn't work:
SELECT REPLACE('','','0')
-- Final current verison
SELECT CAST(COALESCE(REPLACE(REPLACE([Gross_Weight],' ','0'),',',''),'0') AS DECIMAL(13,3))
How could I figure it out?
thanks so much
SELECT COALESCE(NULLIF(MyColumn, ''), 0)
This has the side-effect that you will also turn NULL values into 0, which you might not want. If that's a problem then a simple CASE statement should do the trick:
SELECT CASE WHEN MyColumn = '' THEN 0 ELSE CAST(MyColumn AS DECIMAL(10, 4)) END
Obviously you'll also have to incorporate any other manipulations that you're already doing.
No need for replace, just concatenate a zero to your column, like
SELECT RTRIM('0' + LTRIM(column))
I presume your data is in a table.
Lets call this table 'DATA' and the column 'VALUE'
Then you might use the below query
UPDATE DATA SET VALUE = 0 where VALUE = ''
To select the value do the below
select case ltrim(rtrim([Gross_Weight])) when ''
THEN 0
ELSE ltrim(rtrim([Gross_Weight])) END
Let me know if i get the requirement wrong.

SQLite: order so that results with same starting letter would come first?

SQLite 3.7.11
Given I have these two records in a SQLITE table, with the field called name:
"preview"
"view"
If I run the query:
SELECT * FROM table WHERE name LIKE "%" + $keyword + "%" ORDER BY name
with $keyword set to "vi"
I would get the results in this order:
1) "preview"
2) "view"
Is there a way to order so that the names whose first letter is the same as the first letter of the keyword (in this example "v") would come first?
You can sort by the position of your keyword in the search string
SELECT * FROM your_table
WHERE name LIKE concat('%', '$keyword', '%')
ORDER BY POSITION('$keyword' IN name)
name
Not sure if it can be done less verbose, but this should work. I used ? as a placeholder for your variable. The query as you posted it doesn't seem to be correct SQL.
SELECT * FROM table1
WHERE name LIKE '%' || ? || '%'
ORDER BY (CASE WHEN SUBSTR(Name,1,1) = SUBSTR(?,1,1) THEN 0 ELSE 1 END),
name

Retrieve records which one field value doesn't end with space

The database in question is MS SQL. I have this table and this field in varchar data type.
The field contains values such as:
1. " ABC DEF"
2. "ABC DEF "
3. " ABC DEF "
4. " CCC DEF "
5. " CCC DEF"
The double quotes are just used to show empty spaces in the values.
Now, how do I retrieve all records where this field doesn't end with an empty space? I thought the following SQL should work, but it doesn't!
SELECT * FROM MyTable WHERE MyField like '%DEF'
It retrieves all the above records. A working SQL should only retrieve records 1 and 5.
Any help is much appreciated. Thank you.
What about
SELECT * FROM yourTable where yourField LIKE "% "
or NOT LIKE, if that's what you want.
You can use This,
SELECT * FROM MyTable WHERE PATINDEX('%[ ]',[MyField]) = 0
Try this,
SELECT * FROM MyTable WHERE '.' + LTRIM(MyField) + '.' = '.' + RTRIM(LTRIM(MyField)) + '.'
I guess this is not the correct way but this will give the expected output
WHERE LEN(RTRIM(myField)) = LEN(myField)
The len function ignores spaces at the end so you need to do something like add the . at the end.
SELECT * FROM MyTable where LEN(MyField) = DATALENGTH(MyField)
LEN and trailing spaces

sql update part string and trim trailing value

I am trying to update a string but I also need to wipe out the rest of the values following the found string. I can't use the replace since the value at the end will change but the middle part will stay the same for many records. I haven't seen a post for removing the trailing portion of a string if you don't know it's exact value or location within the string.
I am using Oracle for my database thru SQL Developer to update the data.
"keep this data" "search on keyword" "wipe out trailing data" "result data"
xyz # psu.edu xyz
Column data value = xyz#psu.edu
I would search for a record with columnname like '#%'
Remove "#%"
End result value = xyz
Column data value = ABCabc123
I would search for a record with columnname like 'abc%'
Remove "abc%"
End result value = ABC
I have not seen any answers with this type of update. Please help!
select substr('xyz#psu.edu',1, instr('xyz#psu.edu','#') - 1) from dual
select substr('ABCabc123',1, instr('ABCabc123','abc') - 1) from dual
You might need to check in case the string you want is not found:
SELECT CASE
WHEN INSTR('xyz#psu.edu','#', 1, 1) > 0
THEN SUBSTR('xyz#psu.edu',1,INSTR('xyz#psu.edu','#', 1, 1)-1)
ELSE 'xyz#psu.edu'
END as "Result Data"
FROM DUAL