SQLplus script to format text as numeric - formatting

I am selecting rows from a table, but some of the columns are a text type, but they always have numeric data in them. How can I format them as numbers?
e.g. column quantity heading 'Quantity' format 999,999
However, since the column in the table is text, the numeric formatting is ignored.

You will need to TO_NUMBER the column in your query.

To render with thousand seperators, you'll need to...
to_char(to_number(quantity), '999,999')

Thanks Steve,
I can now have:
column quantity heading 'Quantity' format 999,999
select TO_NUMBER(quantity) as quantity from Sales
And I get a right justified report.

Related

How to Filter Data with Multiple Parameters

I'd like to tally a series of data based on the day and user name. The data is being fed from a query, and I am not looking to use a pivot table because I would like to archive the data past what the program I am pulling the data from stores. Below is a sample of the data I have collected.
I want to tally the Column D "FULL_PLLT_QTY", but only for the date in Column G "SHIFT_DT" and the Column I "Name".
EX. I want to tally Column D for 6/7/2107 for Smith, R.W.
Is there a way to do this for a large range of dates and names? Also, the names appear on multiple dates. Any help with this would be much appreciated!
=SUMIFS(D:D,G:G,"6/7/2017 0:00",I:I,"Smith, R.W.")
Matching the date condition will depend on the exact format of column G. The above assumes it is just a string. If ti is a date then you probably need to use =DATE(2017,7,6) instead of the literal string "6/7/2017 0:00".

How to filter an excel columns which contain date/time as string

I have an excel sheet to filter a Column. The column relates to total experience of a person. The values are like 5years 2Months, 32Years 6Months etc... all the values are in String format. I need the following functionality.
when i enter >5 in a textbox(which i will create in a form), it should display only experience which are less than 5(filtering) . I need an idea how to do this in vba.
Can anyone help..? I just need a way to do this.
Consider the following screenshot. Column a has the unfortunate text with years and months.
Column B splits out the years. Column C splits out the months. Column D has the total number of months for the time frame. With this in place, you can filter by any of the columns using the filter options of the Autofilter built into an Excel table.
The formulas are as follows:
Years: =MID([#total],1,FIND("Years",[#total])-1)+0
Months: =MID(SUBSTITUTE([#total],"Months",""),FIND(" ",[#total])+1,99)+0
Duration in months: =([#years]*12)+[#months]
Now just use the filters in the drop down butttons of the column headers and there is no need for VBA at all.

SQL Server : aggregate function doesn't work?

I have imported a price list from a csv to my SQL Server database. That has worked fine. But now some weird stuff. Table is named PRICE which includes a column (and some more) Endprice and a total of 761 rows. All datatypes are varchar(50).
SELECT MAX(Endprice)
FROM PRICE
When I want this simple SQL statement to show the highest price in the column, I get a wrong result. I don't know why.
I get 98,39 as a result, but that's definitively wrong, it must be 100,73.
Here you can see a part of the data:
And now the wrong MAX() result:
BUT when I'm using the MIN function I get the highest one!? The min is somewhere at ~50 (not shown in the screenshot part).
`
The resultset of SELECT Endprice FROM PRICE is correct. I am at my wit's end.
This is because your column is a varchar, so it is determining the min or max based on characters. The column should be a decimal or money type, so it sorts by the value of your number (instead of an alphabetic sort like you are getting now).
Alphabetic sort: 9 is more than 1, thus 98.39 is the max.
The reason is because price is a varchar().
Here are two solutions:
order by len(price), price
This works assuming that all the price values have the same structure.
Or:
order by cast(price as float)
If you could have non-numeric values (always a danger when storing numbers in the wrong data type):
order by (case when isnumeric(price) = 1 then cast(price as float) end)
Or better yet:
alter table alter column price money
Then you don't have to worry about having the wrong type for the column.
Your problem is Endprice columns is varchar(50), therefore it is comparing strings not numbers, which means that a 9>1 no matter what cames next of the first digit. You have to convert it to a number before the max!
Also you really should consider in doing what #a_horse_with_no_name suggested change your column into a number like column type.
This is a example on how you solve your actual problem
select max(cast(endprice as money)) from sample
See it here: http://sqlfiddle.com/#!3/767f6/1
Note that I used . as a decimal separator it will depend on your database language setup.

Replace existing DATE format with another format in SSRS expression

I have a table which has a TEST column which contains many values:
TEST
2014-04-02
2014-04-03
2014-04-04
WEEKLY TOTAL
PRIOR WEEK
12 WEEK
How can I write an expression using IIF statement so if the row contains a date then change the date to a different format?
For example:
The first row has 2014-04-02 (yyyy-mm-dd) which should be changed to 02-Apr (dd-mmm)
The last row has 12 week which should not be touched because it's not a date
Solution 1:
=iif(isDate(Fields!Test.Value)
,FORMAT(
CDATE(iif(isDate(Fields!Test.Value), Fields!Test.Value, Nothing))
, "dd-MMM"
)
,Fields!Test.Value)
Solution 2:
Handle it in SQL itself using CASE WHEN
IF all of your dates have a '-' AND none of your text has a '-':
=IIF(Fields!TEST.Value like "*-*", FORMAT(CDATE(Fields!TEST.Value), "dd-MMM") ,Fields!TEST.value)
Not tested, but should work. Edited to return an actual date.
Edit: after some more experimentation, it dawned on me that what you want would result in the SSRS column having an inconsistent type (date/string), and I don't think it is possible to have both string types and date types in the same column.
My mistake was not to realize this sooner.
In order to return actual dates, you will need to clean/filter your data.

How can I trim the end of a money data type in sql

Say I have a column that is a money data type in a table, $53125, and I want to update the table's 'sales' column, so that it would instead be $53 for this cell (as an example). Is this possible?
In mysql, the syntax looks like this
UPDATE orders
SET sales = CONCAT("$", FLOOR(sales/1000))
Decimal data types, only serve floating point numbers. To convert to currency, you would have to use the CONCAT method.
To round down to the nearest 1,000, you use the floor method.