how to change datatype of a column in sybase query? - sql

One of my query to Sybase server is returning garbage data. After some investigations i found out that one of the columns with datatype double is causing the issue. If I don't select that particular column then the query returns correct result. The column is question is a double with laarge number of decimal places. I tried to use round function upto 4 decimal places but still i get corrupt data. How can I correctly specify the column in my query to get correct data?
I am using windows 7 box and Sybase Adaptive server enterprise driver. (Sybase client 15.5). I am using 32 bit drivers.
Sample results:
Incorrect result using sybase ASE driver on windows 7 box
"select ric_code as ric, adjusted_weight as adjweight from v_temp_idx_comp where index_ric_code='.AXJO' and ric_code='AQG.AX'"
ric adjweight
1 AQG.AX NA
2 \020 NA
3 <NA> NA
Correct result on windows xp box using Merant driver
"select ric_code, adjusted_weight from v_temp_idx_comp where index_ric_code='.AXJO' and ric_code='AQG.AX'"
ric_code adjusted_weight
1 AQG.AX 0.3163873547
Regards,
Alok

You may try convert to numeric like this:
select ric_code as ric, weight, convert(numeric(16,4), adjusted_weight) as adjweight, currency as currency
from v_temp_idx_comp
where index_ric_code='.AXJO'

Related

Merge records and columns

Data from table A:
id desc
1 huba
1 blub
3 foo
4 bar
And I'd like to have
id desc
1 huba, blub
3 foo
4 bar
So records with the same id should be merged and the desc concatenated.
Unfortunately I can't use string or concat. I get an error if I try to use these functions.
Sybase Version:
Sybase version: Adaptive Server Enterprise/15.5/EBF 19902 SMP ESD#5.1/P/x86_64/Enterprise Linux/asear155/2594/64-bit/FBO/Wed Jun 6 01:20:27 2012
Sybase ASE does not have any functions similar to list(), group_concat(), etc.
While Sybase ASE does provide some XML support, it doesn't provide support for the 'for xml / path()' construct.
And while it's possible to create a funky workaround in ASE 16 (using a table #variable and a user-defined function) ... you're not running ASE 16.
Net result is that you'll need to write some sort of looping construct to accomplish what you want, eg, a cursor that loops through the rows in the table.
NOTE: I'd have to think about the 'merge' idea but since you're running ASE 15.5 and 'merge' isn't available until ASE 15.7 ... I'll put that idea on the back burner for now.
ps - OK, there is a single-query solution but it involves using Application Context Functions (ACFs) (eg, get_appcontext(), set_appcontext()); but that's a very, Very, VERY messy solution ...

How to pad integers with leading zeroes in Netezza SQL

Is it possible for me to change the data input format in Netezza. For example, whenever I import the numbers like 007 or 041 or 063 the output is given as 7 ,41,63 respectively which screws up the business requirement.
Sorry for asking such a silly question as I am new in Netezza.
I found the answer:
select lpad(s.Col_name,3,0) as result
from table name

How to remove numbers or character

select model from Data:
1 2005 TF6 F-150
2 2006 TB7 Expedition 2003
I want to see the data like below
2005 TF6 F-150
2006 TB7 Expedition
I tried different approches but nothing seems to be working
since i have the number at the end of one and two it removes that number as well is there a anyway we could only remove the digits from the end;
Not very clear what you are looking for... But you can search for the first space and get eferything after.
select trim(substr(model, instr(model, ' '))) from Data

Access SQL database - ORDER BY

I'm using an MRP system for stocking inventory where I work. The interface it self isn't the best, so I have decided to open up the database file and do everything manually. I'm having some issues though. I'm trying to sort my database by using ORDER BY. I'm not getting the results I thought I would. It is showing them in this format:
1
10
100
101
101
11
110
111
etc
Instead of
1
2
3
4
5
etc
This is my query
SELECT *
FROM tblStockItems
Order By (`MasterPNo`)
I'm currently working in access, and then database is in the JET format. If you're wondering why I am using access instead of the MRP Interface, it is because later down the line I will be needing to re-organise the whole stock system, so a lot of fields will have their product numbers changed.
Thanks for reading
if possible, change the column type to number
if not, a cast should do it:
ORDER BY Val(MasterPNo)

query column returns multiple readings and needs to be multiple cells in ssrs

I have a cell from a query, returning multiple readings as below with a maximum of 8
|_____readings_____|
|1;2;3;..., 8 |
In my SSRS report, I need each reading to be in a seperate column, e.g
| a | b | c | ...|
| 1 | 2 | 3 | ...|
I am using the 2005 version of ssrs and sql server
Could anyone help? Kind regards
Report-level
You can use the Split function to take a delimited string and return an array; based on this you can specify the element you want from 0-7 to get your eight columns.
In an expression you'd do something like this:
=Split(fields!readings.Value, ";")(0) (1st element) or
=Split(fields!readings.Value, ";")(7) (8th element)
The problem with this is when there is less than 8 elements in the readings field; you will get an error reported - wrapping the expression in an IIf is not enough as this doesn't short circuit in SSRS and any problem string will error regardless.
To deal with these issues you can move the logic to custom code embedded in the report:
Function ElementByNumber(fieldValue As String, elementNumber As Integer) As String
If Split(fieldValue, ";").Length < elementNumber
ElementByNumber = Nothing
Else
ElementByNumber = Split(fieldValue, ";")(elementNumber - 1)
End If
End Function
You can then reference this in the report like:
=Code.ElementByNumber(fields!readings.Value, 8)
Repeat as required for each column you need.
Database level
The other non-SSRS specific workaround would be to handle this, if possible, at the database level, and just use the unpivoted data as a base for a Matrix in the report.
Erland Sommarskog has a series of articles under Arrays and Lists that present any number of methods to split strings in SQL Server; this SO question has a bunch of other alternatives.
Obviously if you're dealing with a fixed Data Source/DataSet this might not be an option.