Statement for Searching for a variable and changing it - sql

I hope I could possibly get some help with the problem I am having. Now before I continue I must say I don't have a lot knowledge about SQL or programming in general. I am just getting into it and I might not provide all the necessary information.
Anyways, I am trying to create a statement in SQL that will allow me to add a decimal point to the following number "775" to "77.5".
So I tried a number of variations and at first I thought it would be a simple.
IF ? = "775" SELECT "77.5"
SELECT REPLACE ( ?, "775", "77.5")
and a number of frustration other instances.
I believe that I need to declare that the data I am pulling is an integer, however I am not sure how. I have looked at tutorials and tried some of them and still not luck. If anyone could give me any advice to get on the track to achieving this. That would be great.
Thank you.

hm.. you indicate SQL so:
this would be an UPDATE statement - you want to change some data in the database.
it goes something like this:
UPDATE mytable SET myvalue = 77.5 WHERE myvalue = 775;
or a bit more general for more updates all at once:
UPDATE mytable SET myvalue = myvalue/10 WHERE myvalue > 100;

UPDATE table_name
SET column1 = 77.5
WHERE column1 = 775;
Have a look at the UPDATE statement:

DECLARE #int INT
SET #int = 775
SELECT SUBSTRING(CAST(#int AS VARCHAR(4)), 1, 2) + '.' + SUBSTRING(CAST(#int AS VARCHAR(4)), 3, 1)

Related

REPLACE function for replacing part of string in specific column

I have written the following code:
SELECT *
FROM BMD_MI_OPS.DBH_TELEFONIE
WHERE cast(DATUM_TIJD as date) BETWEEN 1180212 AND 1180217;
UPDATE BMD_MI_OPS.DBH_TELEFONIE
SET QUEUE_NAAM = REPLACE(QUEUE_NAAM, '_DVB', '');
This should take all columns of the table BMD_MI_OPS.DBH_TELEFONIE within the given period in the WHERE statement. Then it should erase every _DVB that appears in the column QUEUE_NAAM. For example, VQ_PAR_EC_00_DVB should become VQ_PAR_EC_00.
I guess I am doing something wrong, any help on how to get this done would be appreciated.
Thanks in advance.
Your statements are not linked, if you want to update your data you need to add a WHERE clause in your UPDATE
For example :
UPDATE BMD_MI_OPS.DBH_TELEFONIE
SET QUEUE_NAAM = REPLACE(QUEUE_NAAM, '_DVB', '')
WHERE CAST(DATUM_TIJD AS DATE) BETWEEN 1180212 AND 1180217;
Selecting rows before your update has no impact on your update, it' just a SELECT

Can I check if SQL REPLACE finds match in single query then update second field?

As title suggests if I were to run a replace query and it's successful, I then want to update a field in that same query if possible.
UPDATE users
SET solved = REPLACE(solved, ',testsolved123', '') AS match if match = true, SET found = found +1;
I realise this statement wouldn't work, I'm just trying to convey the logic I'm after, is there a case method?
From your description, you want to use a where clause:
UPDATE users
SET solved = REPLACE(solved, ',testsolved123', ''),
found = found + 1
WHERE solved like '%,testsolved123%';
I have no idea what the concat() is supposed to be doing.
It also seems like you are storing comma-delimited lists in a single string columns. That is a very, very bad idea.

SQL query with a CASE statement

I am still learning much about SQL queries but found myself stuck on this. I understand I could break this Query into two queries to get the desired functionality, but i would like to do it in one if possible. My problem lies within the Case clause. i have a condition if the in and out columns are between the selected date range, i want to do myColumnValue + .5 else keep it the same. myColumnValue is a double. The problem is, when i add 'myColumnValue + some constant' to the THEN and Else Clause, it makes the update have now effect on the row, but when i remove it and have only constants in it... it works fine. I am using SQL server 2008. I know I am probably missing some part of the query where I have to reinitialize the myColumnValue as a varible, but I am unsure how to do this. If anyone can give me some direction if keeping this into one query, or if there is an easier way to do this without cases, any guidance would be greatly appreciated. Thanks in advance.
UPDATE myTableName
SET myColumnValue=
(CASE
WHEN((In <= '12/1/2011' ) AND (Out >= '12/7/2011' ))
THEN(myColumnValue + .5 )
ELSE(myColumnValue)
END)
WHERE ID = 'someIndex'
The right way is moving case condition to where clause:
UPDATE myTableName
SET myColumnValue=myColumnValue + .5
WHERE ID = 'someIndex'
AND ([In] <= '12/1/2011' )
AND ([Out] >= '12/7/2011' )

SQL select statement

in a string array i have a variable amount of values.
how will an sql statement look if i want to select all the records that are equal with the array variables.
it will look something this:
SELECT * FROM users WHERE username='"+ variable amount of elements in the String array like: Steven, Mike, John ..... +"'"
You might be looking for the IN( ) operator.
SELECT * FROM users WHERE username IN ('chris', 'bob', 'bill');
This? (obviously the "OR username='xxx' is repeated for as many items as you require)
SELECT * FROM users WHERE username='item1' OR username='item2' OR username='item3'
Every one above me is corrrect! There are multiple ways of doing this! A simple google search for 'mysql statement variables' yield tons of help full results including:
http://www.daniweb.com/forums/thread126895.html
Just treat your array like a standard varible with [a number] on the end!
Two tips come from this:
Google / Search your problem first 9 times out of 10 someone else has had the same problem and found a working solution!
And be prepared to look at the answers on here with in 5 mins. This forums probably the quickest you'll ever see. The only thing that slows the community down is typing ! :P
Hope that Helps,
Andy
Another approach, although less efficient, can be used if parsing the string apart is problematic...
DECLARE #listOfNames VARCHAR(2000)
SET #ListOfNames = 'JOE,KELLIE,PETE'
SELECT * FROM users WHERE charindex(","+userName+",",","+#listOfNames+",") > 0
This approach is slower than either of the other answers, but can save you from using dynamic SQL to build a SQL query based on the list of names passed in...

Writing the content of a local variable back to the resultset column?

Is it possible, by using a stored procedure, to fetch an integer column value from resultset into a local variable, manipulate it there and then write it back to the resultset's column?
If so what would the syntax look like?
Something along the following lines should do the trick.
DECLARE #iSomeDataItem INT
SELECT #iSomeDataItem = TableColumName
FROM TableName
WHERE ID = ?
--Do some work on the variable
SET #iSomeDataItem = #iSomeDataItem + 21 * 2
UPDATE TableName
SET TableColumName = #iSomeDataItem
WHERE ID = ?
The downside to an implementation of this sort is that it only operates on a specific record however this may be what you are looking to achieve.
What you are looking for is probably more along the lines of a user-defined function that can be used in SQL just like any other built in function.
Not sure how this works in DB2, but for Oracle it would be something like this:
Create or replace Function Decrement (pIn Integer)
return Integer
Is
Begin
return pIn - 1;
end;
You could use this in a SQL, e.g.
Select Decrement (43)
From Dual;
should return the "ultimate answer" (42).
Hope this helps.
Thanks for the replies, i went another way and solved the problem without using a procedure. The core problem was to calculate a Date using various column values, the column values ahd to to converted to right format. Solved it by using large "case - when" statements in the select.
Thanks again... :-)
Why not just do the manipulation within the update statement? You don't need to load it into a variable, manipulate it, and then save it.
update TableName
SET TableColumnName=TableColumnName + 42 /* or what ever manipulation you want */
WHERE ID = ?
also,
#iSomeDataItem + 21 * 2
is the same as:
#iSomeDataItem + 42
The function idea is an unnecessary extra step, unless most of the following are true:
1) you will need to use this calculation in many places
2) the calculation is complex
3) the calculation can change