How to add leading zeros to decimal value in tsql - sql-server

i have weight column in a table where weight must be inserted with following format '09.230'. Weight column is of varchar type. so value from front end comes as '9.23' it should get converted to above mentioned format i.e.(09.230). I am able to add trailing zero but adding leading zero is a problem.
This is what i have done to add trailing zero
CAST(ROUND(#Weight,3,0) AS DECIMAL (9,3))
Suppose #Weight = 6.56 output with above comes out be '6.560' but output wanted as '06.560'.

RIGHT('0'+ CONVERT(VARCHAR, CAST(ROUND(#Weight,3,0) AS DECIMAL (9,3))), 6)
This
takes your expression,
converts it to a varchar (retaining the trailing zeros, since the source data type was decimal),
adds a 0 in front of it, and
trims it to 6 characters by removing characters from the front, if needed (e.g. 012.560 -> 12.560, but 06.560 -> 06.560).
Do note, though, that this only works for numbers with at most two digits before the decimal point: 100.123 would be truncated to 00.123!

Related

T-SQL removing leading fixed string with filling zeros

I need to remove leading characters with variable zeros in my column. The string always starts with '049' + filling zeros + some number. I need to extract the number after leading zeros.
04912040 -> 12040
04901204 -> 1204
04900100 -> 100
04900012 -> 12
04900008 -> 8
I have found this solution and added replace for the leading '049' to be replaced with '000':
SUBSTRING(mycolumn, PATINDEX('%[^0]%', REPLACE(mycolumn, '049', '000')), LEN(mycolumn))
However, this won't work if my string looks like 04904901, since instead of 4901 I will get 1.
Just remove the first 3 characters and convert it to an int:
SELECT CONVERT(int,STUFF(YourColumn,1,3,''))
FROM dbo.YourTable;
db<>fiddle

SQL Server how to get money type's decimal digits? [duplicate]

This question already has answers here:
Get the number of digits after the decimal point of a float (with or without decimal part)
(6 answers)
Closed 4 years ago.
The column CostPrice of table t1 is money type.
The data in column CostPrice likes this:
141.1938
0.00
147.1041
119.592
1.23
I use this sql to get the decimal digits:
select distinct len(CostPrice-floor(CostPrice))-2 from t1;
But the result is only 2,this is not right,in fact,the result should be 2,3,4.
So how to fix the sql?
Added:
StackOverflow does not allow me to flag the flagging but asked me to edit the question instead, so:
This Question is not a duplicate to this one. The existing question is on the "float" datatype and this one is on "money", Converting "money" to "varchar" like in the "existing anser" will always return the same number of decimal places, so it does not answer this question.
You could (independantly from regional settings):
multiply the value by 10,000
convert the result to integer
convert the integer to a string
add 4 leading zeroes to the left (just in case...)
take the 4 characters from the right (the former decimal places)
replace each zero by a blank character
remove the trailing blanks using rtrim
return the length of the remaining string
To put this in an expression, it would be:
LEN(RTRIM(REPLACE(RIGHT('0000' + CONVERT(varchar(20), CONVERT(int, CostPrice*10000)), 4), '0', ' ')))
Try this
DECLARE
#money money = 141.1938
SELECT
#money
,SUBSTRING(CONVERT(varchar(20),#money,2), CHARINDEX('.', CONVERT(varchar(20),#money,2)) + 1, LEN(CONVERT(varchar(20),#money,2))) as RESULT

TSQL Decimal To Text

I have the following field called - Amount. It's a decimal(18,2).
So a value of 70.26
What I want to create for the sake of a file out is something like -
00000007026
Where it's a varchar (11), with leading zeros to make up the 11 characters, if only 4 exist in the example above, but also I want the decimal removed as well
You can try something like this :
SELECT RIGHT('00000000000' + REPLACE(CAST(Amount AS VARCHAR(11)),'.',''),11)
Explanation
The CAST transforms the number into a string, for further manipulations.
The REPLACE removes the decimal point.
The I add 11 zeros, no matter what will be the number here. But taking only 11 characters from the RIGHT, will give what you want.
This will not work for negative numbers, as the zeros will be added at the left of the - sign.
declare #str varchar(11)
set #str='00000000000'
declare #p decimal(18,2)
set #p=70.80
select RIGHT(#str+replace(cast(#p as varchar),'.',''), 11 )

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

Character Width of Number Values in TSQL?

MSSSQL 2008.
I have 3 Ints, 1 BigInt, 1 Float, and 1 DateTime value.
I am trying to concatenate them all into a single Char value and not lose any precision, which should let me create a single unique long value.
What are would the total character width be if I could make all of the numbers Chars and then combine them? The DateTime should go to MMDDYYHHMMSS.
Thanks.
INTs can be up to 10 digits.
BIGINTs can be up to 19 digits.
Floats could be anything. They have 38 digits of precision, but could be an enormous number with limited precision (1.79E + 308). You don't want that as a string. If your application has knowledge of what the actual range of values the float could be, you could make an application decision for a specific number of digits.

Resources