I have a Function created which will convert a decimal to Feet and Inches but its currently rounding the inches and I need it to convert the inches and the remaining balance to a fraction.
CREATE FUNCTION FeetInchesFraction(#V_INCHES DECIMAL)
RETURNS VARCHAR(20)
AS
BEGIN
RETURN(
SELECT CAST(FLOOR(#V_INCHES/12.0) AS VARCHAR)+''' '+CAST(FLOOR(#V_INCHES%12.0) AS VARCHAR)+'"'
)
END
GO
SELECT dbo.FeetInchesFraction(68.5)
GO
The above currently displays 5' 9" and I need it to display 5' 8 1/2"
Please use the function name dbo.udf_ConvertToFraction from here
and then modify your code like this
ALTER FUNCTION FeetInchesFraction(#V_INCHES DECIMAL(18,2))
RETURNS VARCHAR(20)
AS
BEGIN
RETURN(
SELECT CAST(FLOOR(#V_INCHES/12.0) AS VARCHAR)+''' '+CAST(FLOOR(#V_INCHES%12.0) AS VARCHAR)+' ' + dbo.udf_ConvertToFraction(#V_INCHES%1.0) + '"'
)
END
GO
SELECT dbo.FeetInchesFraction(68.5)
GO
Cheers
If fraction is not concern (1/2) then you can try with below script
DECLARE #1 DECIMAL(5,2)
SELECT #1 = 68.5
SELECT CAST(FLOOR(#1/12) AS VARCHAR)+''' ' + CAST(Floor(#1%12) AS VARCHAR) + '.' + CAST(CAST((ABS(#1%12)-Floor(#1%12)) * 10 AS INT) AS VARCHAR) +'"'
Related
I have some code like this:
REPLACE INTO digisob_rekap_peruser(
user_rk,
lokasi_lk,
target_lk,
pertarget //2. data does not enter this column ,
vis_lk,
nsb_lk)
SELECT
jadwal_userid,
lokasi_lk,
target_kunjungan*$x,
vis_lk/target_lk*100 AS pertarge //1. this line,
COUNT(jadwal_userid) AS total,
SUM(SJ_Tsel_3_2) AS jualan
FROM
jadwal,
data_kunjungan,
user,
digisob_rekap_peruser
WHERE
Number 1 has a result but this result does not enter the column in number 2.
The default data types for your percentage calculation is likley causing the data loss.
You need to cast the values to decimals/floats/doubles etc to handle it.
select 1/10
-- 0
select 1/10 * 100
-- 0
select cast(1 as decimal)/cast(10 as decimal)
-- 0.1000000000000000000
select cast(1 as decimal)/cast(10 as decimal) * 100
-- 10.0000000000000000
select cast(cast(1 as decimal)/cast(10 as decimal) * 100 as int)
-- 10
select cast(cast(cast(1 as decimal)/cast(10 as decimal) * 100 as int) as varchar(4)) + '%'
-- 10%
I am trying to format the licenseenum column in my table by removing everything starting the before space and also I would like to remove any character in licenseenum column starting after '-' including '-'
For example:
current data in Licenseenum GA 350-0
What I'm trying to get 350
Here is my code
select Licenseenum, SUBSTRING (Licenseenum, 4, LEN(Licenseenum)-1)
from licensee
this results
350-0
How would I remove -0 from the results?
Thanks for the help
Try it like this
DECLARE #YourString VARCHAR(100)='GA 350-0';
SELECT SUBSTRING(#YourString,CHARINDEX(' ',#YourString,1),CHARINDEX('-',#YourString,1)-CHARINDEX(' ',#YourString,1));
UPDATE 1
This is quite the same, but better to read
DECLARE #YourString VARCHAR(100)='GA 350-0';
WITH Positions AS
(
SELECT CHARINDEX(' ',#YourString,1) AS posBlank
,CHARINDEX('-',#YourString,1) AS posMinus
)
SELECT SUBSTRING(#YourString,posBlank,posMinus-posBlank)
FROM Positions;
UPDATE 2 Avoid the leading blank...
My logic needs small correction in order to cut the blank before the number:
SELECT SUBSTRING(#YourString,posBlank+1,posMinus-posBlank-1)
Would be the same with the first example...
Please try the below code. Its working fine in SQL Server 2012.
DECLARE #Licenseenum varchar(max)
SET #Licenseenum ='GA 350-0'
DECLARE #TempLicense VARCHAR(100)
DECLARE #License VARCHAR(100)
IF (len(#Licenseenum ) - len(replace(#Licenseenum ,' ',''))>=1)
SET #TempLicense = (SELECT REVERSE(LEFT(REVERSE(#Licenseenum ),CHARINDEX(' ', REVERSE(#Licenseenum ), 1) - 1)))
ELSE
SET #TempLicense = #Licenseenum
SELECT #License = (SELECT LEFT(#TempLicense,LEN(#TempLicense) - charindex('-',reverse(#TempLicense),1)))
SELECT #License AS Licenseenum
I have read and tried most of the related topics on this forum, but most of them don't have a lot of feedback.
So I am using SSRS 2008 R2 with Report Builder 3.
I have a simple SP as follows that filters my data using the IN.
But for some reason its not passing the parameters correctly to my sp and only filter's by the first option I select, hope you can assist.
--SP
#Warehouse varchar(max)
AS
BEGIN
SELECT WBal.Warehouse ,WBal.StockCode,((WBal.[Current] - WBal.Bal1)) CMov,((WBal.Bal1 - WBal.Bal2)) P1Mov,((WBal.Bal2 - WBal.Bal3)) P2Mov
,((WBal.Bal3 - WBal.Bal4)) P3Mov,((WBal.Bal4 - WBal.Bal5)) P4Mov,((WBal.Bal5 - WBal.Bal6)) P5Mov,((WBal.Bal6 - WBal.Bal7)) P6Mov
,((WBal.Bal7 - WBal.Bal8)) P7Mov,((WBal.Bal8 - WBal.Bal9)) P8Mov,((WBal.Bal9 - WBal.Bal10)) P9Mov,((WBal.Bal10 - WBal.Bal11)) P10Mov
,((WBal.Bal11 - WBal.Bal12)) P11Mov
FROM
(
SELECT [StockCode]
,[Warehouse]
,(QtyOnHand * UnitCost) [Current]
,(OpenBalCost1 * OpenBalQty1) Bal1
,(OpenBalCost2 * OpenBalQty2) Bal2
,(OpenBalCost3 * OpenBalQty3) Bal3
,(OpenBalCost4 * OpenBalQty4) Bal4
,(OpenBalCost5 * OpenBalQty5) Bal5
,(OpenBalCost6 * OpenBalQty6) Bal6
,(OpenBalCost7 * OpenBalQty7) Bal7
,(OpenBalCost8 * OpenBalQty8) Bal8
,(OpenBalCost9 * OpenBalQty9) Bal9
,(OpenBalCost10 * OpenBalQty10) Bal10
,(OpenBalCost11 * OpenBalQty11) Bal11
,(OpenBalCost12 * OpenBalQty12) Bal12
FROM [SysproCompanyR].[dbo].[InvWarehouse]
Where Warehouse in (SELECT * FROM dba_parseString_udf((#Warehouse), ' '))
) WBal
END
I have the following udf that apparently will split of the string, but im not that good with this so I don't know if my problem perhaps lies with the udf
UDF
ALTER FUNCTION [dbo].[dba_parseString_udf]
(
#stringToParse VARCHAR(8000)
, #delimiter CHAR(1)
)
RETURNS #parsedString TABLE (stringValue VARCHAR(128))
AS
/*********************************************************************************
Name: dba_parseString_udf
Author: Michelle Ufford, http://sqlfool.com
Purpose: This function parses string input using a variable delimiter.
Notes: Two common delimiter values are space (' ') and comma (',')
Date Initials Description
----------------------------------------------------------------------------
2011-05-20 MFU Initial Release
*********************************************************************************
Usage:
SELECT *
FROM dba_parseString_udf(<string>, <delimiter>);
Test Cases:
1. multiple strings separated by space
SELECT * FROM dbo.dba_parseString_udf(' aaa bbb ccc ', ' ');
2. multiple strings separated by comma
SELECT * FROM dbo.dba_parseString_udf(',aaa,bbb,,,ccc,', ',');
*********************************************************************************/
BEGIN
/* Declare variables */
DECLARE #trimmedString VARCHAR(8000);
/* We need to trim our string input in case the user entered extra spaces */
SET #trimmedString = LTRIM(RTRIM(#stringToParse));
/* Let's create a recursive CTE to break down our string for us */
WITH parseCTE (StartPos, EndPos)
AS
(
SELECT 1 AS StartPos
, CHARINDEX(#delimiter, #trimmedString + #delimiter) AS EndPos
UNION ALL
SELECT EndPos + 1 AS StartPos
, CharIndex(#delimiter, #trimmedString + #delimiter , EndPos + 1) AS EndPos
FROM parseCTE
WHERE CHARINDEX(#delimiter, #trimmedString + #delimiter, EndPos + 1) <> 0
)
/* Let's take the results and stick it in a table */
INSERT INTO #parsedString
SELECT SUBSTRING(#trimmedString, StartPos, EndPos - StartPos)
FROM parseCTE
WHERE LEN(LTRIM(RTRIM(SUBSTRING(#trimmedString, StartPos, EndPos - StartPos)))) > 0
OPTION (MaxRecursion 8000);
RETURN;
END
So what im trying to achieve is, I have a sp that populates my values for my parameter as did the setup like follow in ssrs
SELECT DISTINCT (Warehouse)
FROM [SysproCompanyR].[dbo].[InvWarehouse]
And then in my Dataset for my report where I need to apply this looks I have tried various alternatives in the parameter option:
expression
=JOIN(Parameters!Warehouse.Value,",")
When you pass multiple values from SSRS, it sends the values as comma delimited value1,value2,value3....
In you query you are splitting on white string ' '
Where Warehouse in (SELECT * FROM dba_parseString_udf((#Warehouse), ' '))
It should be using comma , instead of the white space ' '
Where Warehouse in (SELECT * FROM dba_parseString_udf((#Warehouse), ','))
I have a column in one of my tables, which is TIME format (00:00:00). I am trying to sum the entire column and display it as same (00:00:00).
I have tried using the following but it is not giving me anywhere near the correct answer.It's giving me 22.12:44:00 and manual calcaulation tells me it should be close to 212:something:something
SELECT SEC_TO_TIME( SUM( TIME_TO_SEC( vluchttijd ) ) ) AS totaltime FROM tbl_vluchtgegevens
Any recommendations?
You can try like this:-
SELECT SEC_TO_TIME(SUM(SECOND(vluchttijd ))) AS totaltime FROM tbl_vluchtgegevens;
or try this(althoug this is not a good approach):
SELECT concat(floor(SUM( TIME_TO_SEC( `vluchttijd ` ))/3600),":",floor(SUM( TIME_TO_SEC( `vluchttijd ` ))/60)%60,":",SUM( TIME_TO_SEC( `vluchttijd ` ))%60) AS total_time
FROM tbl_vluchtgegevens;
Edit:-
Try this:-
select cast(sum(datediff(second,0,dt))/3600 as varchar(12)) + ':' +
right('0' + cast(sum(datediff(second,0,dt))/60%60 as varchar(2)),2) +
':' + right('0' + cast(sum(datediff(second,0,dt))%60 as varchar(2)),2)
from TestTable
Working SQL Fidlle
In MySQL, the TIME type is rather limited in range. Moreover many time function do not accept values greater that 23:59:59, making it really usable only to represent the time of the day.
Given your needs, your best bet is probably to write a custom function that will mimic SEC_TO_TIME but allowing much greater range:
CREATE FUNCTION SEC_TO_BIGTIME(sec INT)
RETURNS CHAR(10) DETERMINISTIC
BEGIN
SET #h = sec DIV 3600;
SET #m = sec DIV 60 MOD 60;
SET #s = sec MOD 60;
RETURN CONCAT(
LPAD(#h, 4, '0'),
':',
LPAD(#m, 2, '0'),
':',
LPAD(#s, 2, '0')
);
END;
And here is how to use it:
create table tbl (dt time);
insert tbl values
('09:00:00'), ('01:00:00'), ('07:50:15'), ('12:00:00'),
('08:30:00'), ('00:45:00'), ('12:10:30');
select SEC_TO_BIGTIME(sum(time_to_sec(dt))) from tbl;
Producing:
+--------------------------------------+
| SEC_TO_BIGTIME(SUM(TIME_TO_SEC(DT))) |
+--------------------------------------+
| 0051:15:45 |
+--------------------------------------+
See http://sqlfiddle.com/#!8/aaab8/1
Please note the result is a CHAR(10) in order to overcome TIMEtype limitations. Depending how you plan to use that result, that means that you may have to convert from that string to the appropriate type in your host language.
This worked for me:
SELECT SEC_TO_TIME(SUM(TIME_TO_SEC(vluchttijd))) AS totaltime FROM tbl_vluchtgegevens;
I select the price 1000000 and I need to format it to $1,000,000. How can I do that in SQL?
To format with commas, you can use CONVERT with a style of 1:
declare #money money = 1000000
select '$' + convert(varchar, #money, 1)
will produce $1,000,000.00
If you want to remove the last 3 characters:
select '$' + left(convert(varchar, #money, 1), charindex('.', convert(varchar, #money, 1)) - 1)
and if you want to round rather than truncate:
select '$' + left(convert(varchar, #money + $0.50, 1), charindex('.', convert(varchar, #money, 1)) - 1)
Creating Function:
CREATE FUNCTION [dbo].[f_FormatMoneyValue]
(
#MoneyValue money
)
RETURNS VARCHAR(50)
AS
BEGIN
RETURN cast(#MoneyValue as numeric(36,2))
END
Using in Select Query:
Select dbo.f_FormatMoneyValue(isnull(SalesPrice,0))SalesPrice from SalesOrder
Output:
100.00
Formatting Money Value with '$' sign:
CREATE FUNCTION [dbo].[f_FormatMoneyWithDollar]
(
#MoneyValue money
)
RETURNS VARCHAR(50)
AS
BEGIN
RETURN '$' + convert(varchar, #MoneyValue, 1)
END
Output:
$100.00
Note: The above sample is for the money field. You can modify this function according to your needs
Hope this helps you..! :D
SELECT FORMAT(price, 'C2', 'en-us')
The SQL Server money datatype is just decimal(10, 4). To my knowledge there is no datatype that will present the way you want.
Adding the dollar sign and commas is something that should belong in the application logic, but if you really must do it through a database object consider adding the dollar sign, and commas every three characters (after the decimal point). In other words, you'll have to convert the int to varchar and do string manipulation.
It depends, however, there's no simple way to do it in standard SQL specs(SQL-92, SQL-2003, etc.).
For PostgreSQL PL/pgSQL and Oracle PL/SQL, you can use to_char to format numbers:
select to_char(1234567.123, 'FM$999,999,999.99')
Which gives output:
$1,234,567.12
See: http://www.postgresql.org/docs/7/static/functions2976.htm