i have the computed field in TSQl which computed size field but how can i get ride of Zero from decimals
forexample
RIM DIAMETER = 12.0
RIM WIDTH= 10.5
the computed field SIZEdisplayed as 12.0X10.50 i want the result to be displayed as 12X10.5
ALTER TABLE dbo.[RIMS] ADD [SIZEtst] AS (
CASE
WHEN CONVERT (VARCHAR(50), [RIM DIAMETER],0) IS NULL THEN ''
ELSE CONVERT (VARCHAR(50), [RIM DIAMETER],0)
end +
'X'+
CASE
WHEN CONVERT (VARCHAR(50), [RIM WIDTH],0) IS NULL THEN ''
ELSE CONVERT (VARCHAR(50), [RIM WIDTH],0)
end
Consider Format()
Declare #D decimal(10,2) = 12.0
Declare #W decimal(10,2) = 10.5
Select concat(format(#D,'0.#'),'X',format(#W,'0.#'))
Returns
12X10.5
Or the implicit conversion using float
Declare #D float = 12.0
Declare #W float = 10.5
Select concat(#D,'X',#W)
This was intended as a demonstration of format function and/or the implicit conversion
[SizeTst] AS (concat(CONVERT([float],[RIM Diameter]),'X',CONVERT([float],[RIM Width])))
or
[SizeTst] AS (concat(format([RIM Diameter],'0.#'),'X',format([RIM Width],'0.#')))
-- you can define table like this
create table [RIMS](
[RIM DIAMETER] float,
[RIM WIDTH] float
)
Related
How can I convert (money/int)*100 to a float datatype?
a.ResidualValue is of type money, while ListPriceCar is of type int.
The following outputs a number with a comma as the decimal separator, so I presume it is still a money field, and not a float as I wish:
CONVERT(float, CONVERT(float, REPLACE(CONVERT(nvarchar(10), a.ResidualValue), ',', '.'))/ListPriceCar)*100 AS 'Residual value %'
Result: 62,825
What is issue with simple cast as below:
DECLARE #money AS money
SET #money = $2345234.35
SELECT #money, CAST(#money AS float)
What's the value of ResidualValue? If ResidualValue contains a comma, then comma is your decimal char. You shouldn't convert it into a point then. You can divide a money value by an int value.
declare #a money = 10.5;
declare #b int = 20;
select (#a / #b) * 100
select cast((#a / #b) * 100 as float)
In your example, the result was already given as an float. You can test this by running the following example code.
--INPUT VARIABLES
declare #ResidualValue money = 5000.54
declare #ListPriceCar int = 15000
--OUTPUT VARIABLE
declare #ResultCalculation as sql_variant;
--CALCULATION
set #ResultCalculation = CONVERT(float, CONVERT(float, REPLACE(CONVERT(nvarchar(10), #ResidualValue), ',', '.'))/#ListPriceCar)*100
--GET INFORMATION ON THE OUTPUT VARIABLE
select sql_variant_property(#ResultCalculation,'BaseType') AS 'Base Type',
sql_variant_property(#ResultCalculation,'Precision') AS 'Precision',
sql_variant_property(#ResultCalculation,'Scale') AS 'Scale',
sql_variant_property(#ResultCalculation,'TotalBytes') AS 'TotalBytes',
sql_variant_property(#ResultCalculation,'Collation') AS 'Collation',
sql_variant_property(#ResultCalculation,'MaxLength') AS 'MaxLength';
There is an easier way to calculate your answer:
CAST(#ResidualValue/#ListPriceCar * 100 as float)
How do I convert a numbers in the columns with values like 20160912 into date formats of the form 09/12/2016 and order them by the dates in the date format.
You can use cast and convert built-in functions. Depending on what type is 20160912 you can do following.
A) int
declare #d int=20160912
select convert(varchar(20),convert(date,convert(varchar,#d)),101)
--step by step
declare #dStr varchar(20)
set #dStr = convert(varchar,#d) --'20160912'
-- or = cast(#d as varchar)
declare #dDate date --or datetime
set #dDate = convert(date, #dStr) --2016-09-12 (this is external representation)
--show in MM/dd/yyyy format
select convert(varchar(20), #dDate, 101) --magic 101 for MM/dd/yyyy
--09/12/2016
B) varchar just omit innermost conversion
I am using two columns which have Date and float values, now I need to combine both the columns as a single string. For date I am able to convert it to string. But for the float values it is rounding up the decimal value which should not be the case.
For example my float value is 204.8 and date id 2014-11-11. Now when I combine as string it should result as '2014-11-11 204.8' But it is showing me as '2014-11-11 204' when I convert float value. I am using this query,
DECLARE #myDateTime DATETIME
DECLARE #StandardCost INT
SET #myDateTime = '2011-12-24 00:00:00.000'
SET #StandardCost = 204.8
SELECT LEFT(CONVERT(VARCHAR, #myDateTime, 120), 10)+ ' ' + CAST(#StandardCost AS VARCHAR(MAX))
Result: 2011-12-24 204
Can anyone help me in getting exact float value with decimals as string.
You have #StandardCost defined as an int. It will not hold the decimal portion. Change that to a numeric or some other datatype that will support decimal places.
First, you need to declare its as FLOAT, instead of INT
Then, you can try using STR() function
DECLARE #StandardCost FLOAT
SET #StandardCost = 204.8
SELECT STR(#StandardCost,10,1)
As #Sean Lange said change the INT variable to a FLOAT
DECLARE #myDateTime DATETIME
DECLARE #standardcost FLOAT; -- <-------- Changed INT to FLOAT
SET #mydatetime = '2011-12-24 00:00:00.000';
SET #standardcost = 204.8;
SELECT LEFT(CONVERT(VARCHAR, #mydatetime, 111), 10) + ' ' + CAST(#standardcost AS VARCHAR(MAX));
I have a situation like this
I got a column with 'money' type, 2 decimal . Example data:(65.00)
I need to add 12 zero / 000000000000 to it so that the output would be like this:
(65.00 convert to 6500) + (000000000000) = 000000006500
Output: 000000006500
How can I achieve this?. Thank you for your help and suggestion
You can do this with a couple of casts, multiplying by 100, and using REPLICATE('0') to pad with the requisite number of zeroes).
I'm assuming you DO want up to 2 x trailing decimals, but no more.
DECLARE #value MONEY;
SET #value = 65.123;
DECLARE #intValue BIGINT;
SET #intValue = CAST(#value * 100.0 AS BIGINT);
SELECT REPLICATE('0',12-LEN(#intValue)) + CAST(#intValue AS NVARCHAR(20));
Returns 000000006512
If you need to do this on a set, a CTE can be used for the intermediate step, e.g.
WITH cte AS
(
SELECT CAST(MoneyField * 100.0 AS BIGINT) AS intValue
FROM SomeTable
)
SELECT
REPLICATE('0',12-LEN(cte.intValue)) + CAST(cte.intValue AS NVARCHAR(20))
FROM cte;
Fiddle here
It is Possible .But output Column should be in the type of varchar(15) .If you want to do further operation of your output you have to convert that into int or whatever
SELECT CONCAT(REPEAT('0',12-LENGTH(65.00)),(65.00*100));
I have a field value productlength of 0.123. This is from a view and has a data type of varchar.
I need to convert it to a float or numeric value so as o perform math comparisons.
convert(float,productlength)
and
cast(productlength as float) both do not work.
error varchar cant be converted to float or somethiing liek that.
From what I have read varchar can simply not be converted to a numeric string?
Any clever ways around this?
You can convert varchars to floats, and you can do it in the manner you have expressed. Your varchar must not be a numeric value. There must be something else in it. You can use IsNumeric to test it. See this:
declare #thing varchar(100)
select #thing = '122.332'
--This returns 1 since it is numeric.
select isnumeric(#thing)
--This converts just fine.
select convert(float,#thing)
select #thing = '122.332.'
--This returns 0 since it is not numeric.
select isnumeric(#thing)
--This convert throws.
select convert(float,#thing)
Use
Try_convert(float,[Value])
See
https://raresql.com/2013/04/26/sql-server-how-to-convert-varchar-to-float/
DECLARE #INPUT VARCHAR(5) = '0.12',#INPUT_1 VARCHAR(5)='0.12x';
select CONVERT(float, #INPUT) YOUR_QUERY ,
case when isnumeric(#INPUT_1)=1 THEN CONVERT(float, #INPUT_1) ELSE 0 END AS YOUR_QUERY_ANSWERED
above will return values
however below query wont work
DECLARE #INPUT VARCHAR(5) = '0.12',#INPUT_1 VARCHAR(5)='0.12x';
select CONVERT(float, #INPUT) YOUR_QUERY ,
case when isnumeric(#INPUT_1)=1 THEN CONVERT(float, #INPUT_1) ELSE **#INPUT_1** END AS YOUR_QUERY_ANSWERED
as #INPUT_1 actually has varchar in it.
So your output column must have a varchar in it.