Oracle: How to round and update all numbers in a table? [closed] - sql

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a table with several columns, with DATA_TYPE FLOAT, NUMBER. There are whole numbers and decimal digits with decimal places.
e.g. 234, 4, 0, 23.000000004, 234,4444, ...
Assignment:
I want that the numbers have 2 decimal places at maximum. If more, then round up resp. off!
The wish is, to execute the .sql script via sqldeveloper. Easier approaches are welcome!
Synopsis:
ROUND numbers if they exceed 2 decimal places
UPDATE the value
utilization on several choosen columns
.sql script
sqldeveloper preferred

The simplest possible thing would by something like
UPDATE table_name
SET column1_name = round(column1_name, 2 ),
column2_name = round(column2_name, 2 ),
...
columnN_name = round(columnN_name, 2 )
where you enter however many columns you want to modify. If you want to dynamically generate the script, you could write an anonymous PL/SQL block that used the dba|all|user_tab_columns data dictionary view to generate the appropriate SQL statement for each table and use EXECUTE IMMEDIATE or DBMS_SQL to execute the dynamically generated SQL statement. That's quite a bit more effort to write, debug, and maintain, though so it's probably only worthwhile if you want it to work automatically in the future when new columns are added to the table.
If you have FLOAT columns, be aware that floats are inherently imprecise. Even if you round to 2 decimal digits, there is no guarantee that the value that is stored will always be 2 decimal digits. You may find values that are infinitessimally largers or smaller than you'd expect. If you really want to ensure that all numbers have 2 a particular precision, those columns should be defined as numbers not floats.

Related

What would be the pros and cons of the extensive use of NOT NULL values? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 5 years ago.
Improve this question
What would be the pros and cons of the extensive use of NOT NULL values? And what would be a good method to amend the use of not nulls within an existing schema?
Pros:
Not Null fields don't require Null variable, so it saves bit of space if the column is lesser in length (e.g. 1 char) and you mostly will have values there.
While coding in application program, you don't need to worry about the null values and don't need to do extra coding for null indicator (e.g. in COBOL).
The fields are forced to have a value, e.g. a 'Y'/'N' situation. If you want to force people to answer a Yes/No question, use Not Null.
While write queries you don't need to write something like 'Where col <> 'Y' and col IS NOT NULL'. So, query size is reduced.
While joining tables, you don't need to worry if the Joined column will have a Null.
It may also help in aggregate functions especially if you want to have a default value like '1' in some int column.
Also helps in ensuring relational integrity as Null values are Not checked for RI.
Cons:
Counter to point 1 above, If you have a large field and you use Not Null then you may be wasting space if you don't want to have that field populated most of the times.
While inserting rows, you'll need to make sure to pass a value for a Not Null column.
You'll need to decide beforehand what values (or default) you want for that column.
If you use Null, then you can intelligently make use of Null values without much coding e.g. suppose you have a frequency of 'M'onthly and 'Y'early and you want to introduce a new frequency of 'S'emi-monthly, then if NULLs are allowed then you don't need to populate that column with a 'S'. You'll know if it is Null then that means Semi-monthly. You can get away with introducing a new frequency. You can handle that Null in your program.
You can't do this if column is Not Null.
If your application requirements change, then you need to change that Not Null value whereas a Null can remain a Null.

SQL Server column select query behaving strangely [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
When I do a select * over the table, the column Qty & QtyPending show a value of 6. However explicitly selecting the column names shows different values. Can anyone shed some light as to why this behavior is occurring?
This is a legacy system and database used is SQL Server 2000. The column data types are smallint.
So I have explicitly updated QtyPending to 6 using an Update query. This column now shows correct value.
Also added locstockid to the query, column Qty still shows different values.
Whatever I see in the image provided both the query have different LocStockId which means they can have different values
First :
LocStockId = 152319
Second :
LocStockId = 153219
I think you have mistyped.

Rearrange the characters in a string alphabetically using SQL [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I am looking to take a string field and rearrange the characters into alphabetical order. For example, if it was a name field and the name was MICHAEL SMITH it would change to ACEHHIILMMST.
The main purpose is to look for duplicates in a field like name where someone could have made a typo and put in the name as MICHEAL SMITH or MICHAEL SMTIH. Another option I thought of would be to assign a number value to each letter (1-26) and then if the sum of those values and the length of the name field are the same it would be considered a duplicate. Problem with that is I do not know how to sum up numbers in a string field.
Also, I am using Oracle SQL so the functions would be based on those available.
Any ideas or at least places to start?
EDIT
I am not trying to ask for the code for how to do this, I am asking more if it is possible or not and if possible, what would my starting place be (types of functions, techniques, etc)
Check utl_match. It can be used to measure the similarity of two strings.
select utl_match.edit_distance_similarity('MICHAEL SMITH','MICHEAL SMITH') from dual
85
select utl_match.edit_distance_similarity('MICHAEL SMITH','MICHELLE SMITH') from dual
79
select utl_match.edit_distance_similarity('MICHAEL SMITH','FRANKIE JONES') from dual
8

What are sql function possible positions in sql statement? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am looking for all the possible positions in a sql statement that a sql function can be called. It is my first time to use sql functions and in my code I need to detect whether users' input contains sql function, if so, the input is not valid.
The position in sql statement means SELECT clause or other clause like FROM, WHERE......
I am using SQL Server
So is there any suggestions for detect sql functions in a sql statement or all the possible positions for the function in a statement?
SQL functions are allowed in any place in a SELECT where a column is allowed, i.e. anywhere in the SELECT list, in WHERE, JOIN, GROUP BY, HAVING, ORDER BY.
You'll need a SQL parser to know for sure if it's a function...
If I understood your question, you are asking how and where to check about SQL code entered by the user.
Where:
I think you might need to check it in the user interface, check the textbox or files or whatever the input stream of the user is. This shall not be done in the sql server ASAIK
How:
usually you can use parameters in your SQL statements so that any value passed by the user is passed by a parameter and if it contains a SQL code, it will not be executed.

Overcoming the reserved word "IN" in sql server [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
Just for reference I am using SQL Azure.
I noticed when I am trying to select data from a table based on a license plate and the state of that plate I get no results back if the state is "IN". I realize the word "IN" is reserved in SQL server; however, I am containing that within quotes in my query. I currently am in testing phase and have only one record in the table which has a lisence plate 287YGB and state IN.
If I write my query as follows I get nothing back.
SELECT MakeModel, CitizenID, VehicleID FROM tblVehicles WHERE tblVehicles.Lisence = '287YGB' AND tblVehicles.PlateState = 'IN'
If I write my query this way I get back my result. But this is not good enough.
SELECT MakeModel, CitizenID, VehicleID FROM tblVehicles WHERE tblVehicles.Lisence = '287YGB'
And finally, if I write my query this way I get the only row in the table.
SELECT MakeModel, CitizenID, VehicleID FROM tblVehicles
From these tests I can see that the last where parameter is causing the problem. I am assuming it is due to the fact that the word "IN" is reserved. Is there a way around this?
Reserved words usually only cause problems if you're using them as field names, and in that case you need to wrap them with brackets ("[]") to eliminate the problem. I will amost guarantee you that your PlateState has some garbage in it, so you need to either trim it first (LTRIM(RTRIM(PlateState)) = 'IN') or use Like '%IN%' instead, and this will return the results you expect.
try this
SELECT MakeModel, CitizenID, VehicleID FROM tblVehicles WHERE tblVehicles.Lisence = '287YGB' AND LTRIM(RTRIM(tblVehicles.PlateState)) = 'IN'