sql query to append + or - and leading zeros - sql-server

trying to query a table that stores prices but need a way to append + if number is >1 and - if <1 and append up to 8 zeros after the + or - sign , and round to 2 decimal places so if table contains
Current table: prices
1000.3334
Expected return: prices
+00001000.33
Tried the following query but it does not round to 2 decimal places and does not add the + or - sign
SELECT RIGHT('00000000'+ CONVERT(VARCHAR,prices),11) AS NUM FROM SALES;

While I agree with others, yuck, this belongs in your front end / presentation layer:
SELECT CASE WHEN prices < 0 THEN '-' ELSE '+' END
+ RIGHT(CONCAT(REPLICATE('0',9),
ABS(CONVERT(decimal(11,2),prices))), 11)
FROM dbo.sales;
Working example in this fiddle.

Related

Generate 6 digit alpha numeric value

I have the following code; the only issue with it is that sometimes it generates a digit alphanumeric code.
I am trying to modify it such that it always returns a 6 digit code. Any thoughts on how to tweak this?
I have another procedure the purges duplicates and it re-runs, so that is not an issue.
SET #HashCode = NULL
SELECT
#HashCode = ISNULL(#HashCode, '') + SUBSTRING('23456789ABCDEFGHJKLMNPQRSTUVWXYZ', (ABS(CHECKSUM(NEWID())) % 36) + 1, 1)
FROM
[master]..[spt_values] AS [spt_values] WITH (NOLOCK)
WHERE
[spt_values].[type] = 'P'
AND [spt_values].[number] < 6
A very simple way of achieving this is selecting the first 6 LEFT characters in the string generated by the NEWID() function:
SELECT LEFT(NEWID(), 6)
Examples:
4DF32A
DC70D5
8793B2
3D8416
EE2838
Note, because the NEWID() function generates a unique identifier (GUID), consisting of hexadecimal characters (A-F & 0-9), there is a chance the output will consist of only numbers or only letters; which may or may not fit your purpose if you require a strict alphanumeric value.
Examples:
946983
831814
DDFDBB
You can use it like this:
SET #HashCode = NULL
SELECT
#HashCode = LEFT(NEWID(), 6)

Google Spreadsheet MAX + Join of other cells

I have a spreadsheet like this - I can't figure out how to dynamically search for this.
I want to find the MAX Score Value + output the related name
(no worries, in another column I've got the Score calculated as clean numbers without letters).
(QUERY doesn't really work because once I change a score, it doesn't update that output)
use:
=SORTN(SORT({x2:x, y2:y}, 2, 0), 9^9, 2, 1, 1)
where:
x2:x - column with names
y2:y - column with values
2 - sort values
0 - in descending order
9^9 - return all rows
2 - 2nd mode of sortn eg. group by
1 - names column
1 - in ascending order

Remove the trailing zeros after Decimal points without truncating/ approximating the value in SQL server

Have decimals stored as varchar.
I have a column with value 0.0375000. I need to convert this into 0.0375.
When I did
convert(decimal(8, 7), substring(column, 0, 1) + '.' + substring(column, 2, 8)))
I got the result as 0.0375000.
I want to remove all the trailing zeros and the result I want is 0.0375
How can I do this?
If 2012+ The #'s indicate an optional display
Select format(0.0375000,'0.######') Returns 0.0375
Select format(0.037502,'0.######') Returns 0.037502
Sorry didn't see stored as varchar()
Select format(cast(somecolumn as decimal(18,8)),'0.######')
if you only need 4 decimal places, you want decimal 5,4 (assuming your number to the left of the decimal point fits into 1 digit , if you need 2 digits, choose decimal(6,4) for example )
select convert(decimal(5,4), substring(column,0,1)+'.' +substring(column,2,8) )
decimal data type https://msdn.microsoft.com/en-gb/library/ms187746.aspx
--SQL Code for easy removing trailing zeros.
select CONVERT(DOUBLE PRECISION,'2.256000')
--Result will be 2.256

select integer before a certain character

hie am trying to select the integer value before the char C in my SQL database table which contains the information below.
240mm2 X 15C WIRING CABLE
150mm2 X 3C flex
10mm2 x 4C swa
so far i have used the query
select left ('C',CHARINDEX ('C',product_name)) from product
and i get 'C' on my results which is correct. Now am stuck does anyone know how i can modify the above select query to get a result which only lists the integers for eg
15
3
4
Two observations: the integer before "C" has a space before it and there is no space between the integer and "C".
If these are generally true, then you can do what you want using substring_index():
select substring_index(substring_index(product_name, 'C', 1), ' ', -1) + 0 as thenumber
The + 0 simply converts the value to a number.
If you're doing this in SQL Server you could try the following:
Select Substring(product_name,
PATINDEX('% [0-9]%',product_name) + 1,
PATINDEX('%[0-9]C%',product_name) - PATINDEX('% [0-9]%',product_name)
) as num
from Product
This assumes that there is a space before the number and always a C after the number with no space.
It works out the starting point and then the length based on the start and end and performs a substring with the results.
You could use a combination of instring and substring.
First get the position of the C
Then substring till C
It goes like this:
SELECT INSTR('foobarbar', 'bar');
= 4
And then you select substring from 1 to 4.

SubString of value with several 0s

The data in the table looks like this
ID Value
1 5006049
2 5006050
How do I select a substring so that I get
R6049
R6050
Keeping in mind that the values are sequential starting from
5000001 = R1
to
5999999 = R999999
Just substract
SELECT 'R' + CAST(VALUE - 5000000 as VARCHAR(6))
FROM table
SqlFiddle
I think as easily as this:
Select 'R'+Substring(convert(VARCHAR(7), Value), 4,7)
Which will give R0001 (do you want the zeros?)
If you don't want the zeros / only looking to remove the top digit:
Select 'R'+ convert(VARCHAR(6),Value - 5000000)

Resources