I have a table made up of one column that has a 100 character string in each row. A second column was added to house the result. I needed to amend certain fix position elements and planned to do the following:
UPDATE myData
SET newData = REPLACE(oldData,SUBSTRING(eftnwsfull, 16,2),'OC')
The element at position 16,2 is '17'. But, if there are other parts of the string (not at position 16,2) that happen to be '17' are getting changed to 'OC' as well.
I'm baffled to understand how this can happen as I'm specifying the exact position of where to make the replacement. What am I doing wrong?
Try STUFF
UPDATE myData
SET newData = STUFF(oldData, 16, 2, 'OC')
Here are a couple of ways (please test as the offsets may be one off) ..
SET newdata = SUBSTRING(oldData, 1, 15) + 'OC' + SUBSTRING(oldData, 18, LEN(oldData) - 17)
or
SET newdata = LEFT(oldData, 15) + 'OC' + RIGHT(oldData, LEN(oldData) - 17)
Related
I am trying to get rid of "40-S8-7710" Within the string without specifically giving the replace function "40-S8-7710" due to the fact there will be other strings that may contain "50-S2-7710", "11-42-7710" etc..
The code below removes "40-S8-7710" but it also removes everything after.
I am wanting to return "10-7190 20-2071 30-2061 S1-AOUT04X188"
Any Ideas?
Dim NewString As String
Set NewString = "10-7190 20-2071 30-2061 40-S8-7710 S1-AOUT04X188"
NewString = Replace(NewString , Mid(NewString , InStr(NewString , "7710") - 6), "")
MsgBox(NewString)
I was able to figure out how perform the action i was looking for by doing this
NewString= (Replace(NewString, (Mid(NewString, (InStr(NewString, "7710") - 7), 11)), ""))
This would return "10-7190 20-2071 30-2067 S1-AOUT04X188"...... "7710" is the only unique number I could use to pull since the 6 characters to the left of "7710" could be different depending what a user enters. Thanks for the help though.
Left([string],24)+Right([string],13)
Hi I am tring to fire this query
update txnblackout
set enddate= ''TO_DATE('25/02/2012','dd/mm/yyyy')''
where idsequence='1'
But it is not working giving error
ORA-00933: SQL command not properly ended
Can you please justify it. Why it is happening?
This is the java code i am using to create query : -
Map<String, String> l_script_columns = new HashMap<String, String>();
l_script_columns.put("startdate", "'TO_DATE('"
+ txtStartDate.getText().trim() + "','"
+ FieldMapperHelper.DATE_Format + "')'");
please let me know if you want more clarification.Thanks.
why ever are you doing the escaping?
with txnblackout as
(select trunc(sysdate + level) enddate /* trunc drops time component */ , level idsequence
from dual
connect by level <= 10 /* this will generate 10 rows with an incrementing 'level' value */ )
select enddate
, idsequence
from txnblackout
where enddate = to_date('20/10/2012','dd/mm/yyyy')
;
thus you should be safe to drop the extra quotes, they are not necessary.
update txnblackout
set enddate= TO_DATE('25/02/2012','dd/mm/yyyy')
where idsequence=1;
(note this assumes idsequence is a number)
At the end of the day, if you are passing in a non-parameterized dynamic sql query into Oracle, you'll need it to look like this:
"update txnblackout
set enddate= TO_DATE(''25/02/2012'',''dd/mm/yyyy'')
where idsequence=1;"
(please note I am still assuming that idsequence is a NUMBER type and not a varchar)
Check this out though, try to be safe!
You also won't have to worry about escaping if you parameterize it!
Preventing SQL Injection in Java
Your query above you want to keep the date or the text, so in the first case
update txnblackout
set enddate= TO_DATE('25/02/2012','dd/mm/yyyy')
where idsequence='1'
The id sequence being text or number doesn't matter, in the second case you want to keep the text:
update txnblackout
set enddate= 'TO_DATE(''25/02/2012'',''dd/mm/yyyy'')'
where idsequence='1'
I think you want the first case please check.
Best regards
In this case you have to try this one:
Map<String, String> l_script_columns = new HashMap<String, String>();
l_script_columns.put("startdate", "'||TO_DATE('"
+ txtStartDate.getText().trim() + "','"
+ FieldMapperHelper.DATE_Format + "')||'");
then final result will be:
update txnblackout
set enddate= ''||TO_DATE('25/02/2012','dd/mm/yyyy')||''
where idsequence='1'
Try putting ';' at the end of your command...
You command should look like :
update txnblackout
set enddate= ''TO_DATE('25/02/2012','dd/mm/yyyy')''
where idsequence='1';
my question is pretty simple but hard to find an answer for though search engines.
I simply want to update a field in the database, using that fields old value to add another value. I'm using the following at the moment:
$this->Advertisement->saveField('total_views', '(total_views + 1)', false);
But this gives me the next query:
UPDATE `advertisement` SET `total_views` = '(total_views +1)', `modified` = '2011-08-26 10:44:58' WHERE `advertisement`.`id` = 16
This is wrong and it should be:
UPDATE `advertisement` SET `total_views` = (total_views +1), `modified` = '2011-08-26 10:44:58' WHERE `advertisement`.`id` = 16
The problem is where it puts (total_views +1) between quotes.
Does anyone have an idea on how to get this working?
$this->Advertisement->updateAll(
array('Advertisement.total_views' => 'Advertisement.total_views + 1'),
array('Advertisement.id' => 1)
);
Just in case anyone else gets stuck with this, the spaces in the query are important.
works: Advertisement.total_views + 1
does not work: Advertisement.total_views+1
$this->Advertisement->updateAll(array('Advertisement.total_views'=>'Advertisement.total_views+1'), array('Advertisement.id'=>$id));
I found that for a similar issue I was able to resolve it in the following manner,
$this->Advertisement->updateAll(
array('Advertisement.total_views = Advertisement.total_views + 1'),
array('Advertisement.id' => 1)
);
A previous developer created a table that stores the absolute path to files in our server. I want to convert them to relative paths instead.
I already wrote the portion that properly strips the string down to a relative path. My issue is understanding how to basically update each record, with a new version of its own string.
Here is what I originally tried:
UPDATE LFRX_Attachments
SET [File] = (SELECT TOP 1 SUBSTRING([File], PATINDEX('%Files\%', [File]) + 6, LEN([File]))
FROM LFRX_Attachments A
WHERE [Type] = 4 AND AttachmentId = A.AttachmentId)
However, this tanked in epic fashion by just overwriting every record to have the value of the first record in the table. Any suggestions?
UPDATE LFRX_Attachments
SET [File] = SUBSTRING([File], PATINDEX('Files\', [File]) + 6, LEN([File]))
WHERE [Type] = 4
From a readability/maintenance standpoint, you're better off selecting for the data you want to alter, then iterating through the result set and updating each record separately.
Does this work for you?
UPDATE LFRX_Attachments SET [File] = SUBSTRING([File], PATINDEX('Files\', [File]) + 6, LEN([File]))
I'm using Entity Framework, and I have a COMMENT entity. A COMMENT has a DATEMODIFIED property, which is a Nullable Date. I'm trying to build a query that will filter COMMENTs by date, so I create a startDate object, and do the following:
Dim q As ObjectQuery(Of COMMENT) = _
(From c In model.COMMENT Select c)
If startDate.HasValue Then
q = q.Where(Function(c) startDate.Value <= c.DATEMODIFIED)
End If
The problem is that q.toList() is not returning any comments, even though I think it should. All comments in the database have DATEMODIFIED values, and even if I pass in DateTime.MinValue as the startDate, the query still doesn't match any entities.
I set a breakpoint before the If-Statement and used the Visual Studio Watch Window to try and see what's going on:
q.ToList()(0).DATEMODIFIED 'Returns the expected date
startDate.Value 'Returns the expected date
startDate.Value <= q.ToList()(0).DATEMODIFIED 'Returns True...
But once once it hits the q = q.Where(predicate) part, q.ToList() no longer returns any entries. I'm stumped.
UPDATE: Oops, I forgot that, with LINQ to Entities, all WHERE expressions are translated into SQL calls instead of being post-processed in code-- so the debugging suggestions below won't necessarily work.
Therefore, I'd start by running the same generated SQL statement against your database, and validating whether the SQL generated by your Entity Framework provider is actually returning the data you expect. #Craig Stuntz's comment above is definitely on the right track here to help you do this. Once you have the parameterized SQL, I'd try executing that SQL directly from your code (using System.Data.OracleClient) and validating that you actually get results back from that query. Remember to inject the same parameter values that you get from ObjectQuery.Parameters. Alternatively, you could stick the parameters in yourself and execute the query from your Oracle client app of choice.
If you don't get results from that SQL, then it's possible that devArt's provider is building the query incorrectly.
you can ignore what's below here, since it applies to troubleshooting LINQ-to-Objects but not LINQ-to-Entities
Some ideas to diagnose this:
first, try this in your watch window:
q.Where(Function(c) startDate.Value <= c.DATEMODIFIED).Count()
I'm assuming this will return zero, but it's worth eliminating as many other variables to make sure you're really not getting any results.
Next, I'd try is to define your LINQ query a bit differently-- instead of appending the Where() separately, try using two queries, like this:
Dim q As ObjectQuery(Of COMMENT)
If startDate.HasValue Then
q = (From c In model.COMMENT Where startDate.Value <= c.DATEMODIFIED Select c)
Else
q = (From c In model.COMMENT Select c)
End If
If this works, then there's something wrong with how the Where clause is being attached to your existing LINQ query-- perhaps a bug in your DBMS's entity-framework provider?
If that still doesn't work, the next step I'd take to diagnose would be to verify that the code inside the where clause is being called, and checking the values passed into that code. I couldn't figure out how to set intra-line breakpoints in VB like one can do in C#, but you can easily (temporarily) refactor your lambda into a separate function and set the breakpoint there. Like this:
Sub Main()
Dim testDate As Date = New Date(2005, 1, 1)
Dim x = New List(Of Date?)
x.Add(New Date(2009, 1, 1))
x.Add(New Date(2008, 1, 1))
x.Add(New Date(2007, 1, 1))
x.Add(New Date(2006, 1, 1))
x.Add(New Date(2005, 1, 1))
x.Add(New Date(2004, 1, 1))
x.Add(New Date(2003, 1, 1))
x.Add(New Date(2002, 1, 1))
x.Add(New Date(2001, 1, 1))
Dim y = From n In x Select n
y = y.Where(Function(val) test(val, testDate))
Dim z = y.ToArray()
End Sub
Function test(ByVal date1 As Date, ByVal date2 As Date) As Boolean
test = date1 >= date2
End Function
Check the values being sent into your comparison function-- are they valid? Does the comparison return what you expect it to?