Oracle SQL help decimal(15,6) on Select - sql

I am having an issue in writing on the SQL for ETL purpose.
My Oracle DB has a column A that holds 999999999997259 or 4121.375515. I need to transform this value to decimal(15,6). The datatype decimal(15,6) is on the target where the data will be loaded.
Any suggestions.
TIA

Following is the way to round the value to 6 decimal place. Condition: It should contain
valid values.
select cast(round(ColumnaA,6) as DECIMAL(15,6)) from table;

Related

Truncate not workng in MySQL

I guess the correct way of truncating a value in MySQL is truncate(value,limit); but that doesn't seem to be working here it needs an extra table name;
select truncate(94204.27348,2);
ERROR at line1:
ORA-00923: FROM keyword not found where expected
Assuming you are using Oracle, you must supply a table reference on your select query.
Oracle select queries require a table reference always.
You can do this....
Select truncate(94204.27348,2) -- see further comment below
From dual
;
Dual is a special table that allows these sort of queries.
Also, I think you might mean to use the TRUNC function.
As others have pointed out, the TRUNCATE query is not quite the same, it will erase the contents of the table that you supply it.
In Mysql You should use round. Truncating table will remove the data from the table and reset all your auto increment values.
SELECT ROUND(94204.27348,2);
Result - 94204.27
This will round the value and display only two decimal places.

New column has to be generated based on an existing column in SQL Server

I have a table in my SQL Server database. I am showing my table with some data in image called input.
And from this input I want to add one derived column in that column the data should be below format. I am showing my expected output in image called output.
How can I achieve my expected output with a SQL query?
We have lots of records in the table, but the maximum length of CODE column is 4. Means the last value in that column is 9999 only.
Please suggest how I can get my expected output with a simple SQL query.
Best Regards,
Phani Kumar.
SELECT *,
'C' + RIGHT('000'+CONVERT(VARCHAR(4),Code),4) [YourColumn]
FROM dbo.YourTable;

SQL Server DBLlink to oracle returns numbers as string

I have an Oracle database containing my data and an SQL Server database getting the data from Oracle through DBLink.
Problem is - all numbers from the Oracle tables are accepted at the SQL Server as nvarchar. As a result, when i try to filter the query in the SQL Server with some_number_field = 0 i get:
"Conversion failed when converting the nvarchar value '3.141' to data type int."
This also happens if i try to select "some_number_field * 1" or similar expressions.
Any idea ?
Today I ran into the same kind of problem. It seems that Oracle field with datatype NUMBER are shown as nvarchar where querying through a linked server. However, NUMBER(x,y) not.
E.g. colB is the NUMBER field from an Oracle View (or table)
Try this:
SELECT colA, CAST(colB AS DECIMAL(23,2)) colB
FROM OPENQUERY(LINKED_SERVER_NAME, 'select * from myView')
Note: the DECIMAL(xx,y) values depends of course on your data. Also, remember, if your NUMBER column is a repetitive fraction (eg. 33.33333333 etc), you need to place a round() on the oracle side otherwise the CAST will throw an error.

Unable to get a result that is in front of my eyes?

I have a database that uses fields of type VARBINARY(18) to store the primary keys
A sample PK: 0x001B7431C732005C4785A14F168EBD1FC5E4
When I try to run a simple query such as
SELECT * FROM mytable WHERE ID = '0x001B7431C732005C4785A14F168EBD1FC5E4'
I get no results, even though I can see the PK in mytable.
Does anyone know why this would be happening and how to fix it? I'm using SQL 2008 R2.
Thank you!
Don't use quotes.
That represents a string literal not a binary literal. The effect of it is an implicit cast of all the binary column data to string using the code page of your default collation. This will not match.
The below is all you need.
SELECT *
FROM mytable
WHERE ID = 0x001B7431C732005C4785A14F168EBD1FC5E4

What kind of SQL clause is this? Any way to convert it to SQL?

what kind of SQL is this?
SELECT IFNULL(SUM(prenotazione.VALUTAZIONE),0) AS somma,
COUNT(*) AS numero
FROM `prenotazione`
WHERE prenotazione.USER_ID=18793 AND
prenotazione.PRENOTAZIONE_STATO_ID IN (10,11)
I'm using propel as my ORM.
Any way to convert that kind of SQL to Mysql SQL?
This query is valid in MySQL. It selects all rows from the prenotazione table where the user_id is 18793 and the prenotazione_stato_id is 10 or 11. The resulting rows are summarized: in the numero column you get the number of rows found, in the somma column you get the sum of the valutazione values. If no rows were selected, SUM() would return NULL. To prevent this, IFNULL([expr1],[expr2]) is applied, which returns [expr1] if it is not null, and [expr2] if it is null. This makes sure you always return a number.
There is no easy way to do this with Propel, since your result cannot be easily mapped to a Propel object. The best thing you can do is use the underlying database layer (PDO) to escape your parameters and handle the resultset, and you don't open an extra database connection or something like that.
When considering portability, Standard SQL is your friend. This query can be very easily transformed into Standard SQL-92:
Terminate the statement with a semi-colon.
Replace IFNULL with COALESCE.
Remove the single quotes from the table name.
With better spacing it could look like this:
SELECT COALESCE(SUM(prenotazione.VALUTAZIONE), 0) AS somma,
COUNT(*) AS numero
FROM prenotazione
WHERE prenotazione.USER_ID = 18793
AND prenotazione.PRENOTAZIONE_STATO_ID IN (10,11);
That said, for MySQL you probably would need to undo step 3... which leads me to suspect it was MySQL syntax in the first place.
Using Babelfish to give a rough translation from Italian to English results in
SELECT IFNULL(SUM(reservation.APPRAISAL),0) AS sum,
COUNT(*) AS number
FROM `reservation`
WHERE reservation.USER_ID=18793 AND
reservation.RESERVATION_STATE_ID IN (10,11)
Share and enjoy.