How to add commas to numbers in SQL Server? - sql

I am using SQL Server 2012. I need to add commas for numbers. For ex 2000 should be 2,000.
How can I do it? Thank you.
Here is my query :
declare #Term int = 20172;
Select
'Applications' as 'NEW FIRST-TIME FRESHMEN',
Cast(Round(((Cast(y.Last_Term As FLOAT)) * 1), 2) as varchar(10)) as 'Last_Term',
Cast(Round(((Cast(y.Current_Term AS FLOAT))*1),2) as varchar(10)) as 'Current_Term',
Cast(Round(((Cast(y.Current_Term AS FLOAT) - Cast(y.Last_Term AS FLOAT)) * 1), 2) as varchar(10)) as '#Change',
Cast(Round((((Cast(y.Current_Term AS FLOAT) - Cast(y.Last_Term AS FLOAT)) / (Cast(y.Last_Term AS FLOAT)))* 100), 2) as varchar(10)) + '%' as '%Change'
from
(Select
Case
When [ACADEMIC_PERIOD_ALL_SF_COMBINED] = #Term
Then 'Current_Term'
When [ACADEMIC_PERIOD_ALL_SF_COMBINED] = #Term-10
Then 'Last_Term'
End ACADEMIC_PERIOD_ALL_SF_COMBINED,
[APPLICATION_COUNT] Applied
From
[IR_DW].[dbo].[vw_dw_Applied_Admitted_Enrolled_F]
Where
STUDENT_LEVEL ='UG'
and STUDENT_POPULATION ='N'
and ACADEMIC_PERIOD_ALL_SF_COMBINED between #Term-10 and #Term
and ACADEMIC_PERIOD_ALL_SF_COMBINED like '%2') x
Pivot
(sum(Applied)
for ACADEMIC_PERIOD_ALL_SF_COMBINED in ([Current_Term] , [Last_Term])
)y
output

Really belongs in the presentation layer, but in 2012+, you can use Format(). It is a great function, but not a high performer.
Example
Select Format(25302.25,'#,##0')
Returns
25,302

See the similar question here. What you'll do is CAST to MONEY first and then CONVERT to VARCHAR.

Related

Remove trailing 0 when CAST as NVarChar

I have data as Decimal(15,4) and values can be like 14.0100, or 14.0000, or 14.9999
For integration with other system we have to store this kind of data in NVarChar(MAX) attributes table. When I run CAST(Field AS NVarChar(MAX)) I get string values like 0.0000
What I want is to trim trailing zeros (and period if needed) from those strings because data later used in online transmission and it's much better to send 14 instead of 14.0000
How do I do that?
SQL Server 2012+ you could use FORMAT, with SQL Server 2008 you could use string manipulation:
CREATE TABLE #tab(col DECIMAL(15,4));
INSERT INTO #tab(col)
VALUES (14.0100), (14.0000),
(14.9999), (10), (0),
(-1), (-10), (-12.01), (-12.10);
SELECT
col
,result_2012 = FORMAT(col, '########.####')
,result_2008 = CASE
WHEN col = 0 THEN '0'
ELSE LEFT(col,LEN(col) -
CASE WHEN PATINDEX('%[1-9]%', REVERSE(col)) < PATINDEX('%[.]%', REVERSE(col))
THEN PATINDEX('%[1-9]%', REVERSE(col)) - 1
ELSE PATINDEX('%[.]%', REVERSE(col))
END)
END
FROM #tab;
LiveDemo
You could try casting the Decimal numbers to floats before casting then casting them to NVarChars i.e. CAST(CAST(Field as Float(8)) as NVarChar(MAX)). The only issue you might have with this is if any of your existing numbers have a greater precision than an 8 byte float can deal with which should be easy enough to check for.
It would also be possible to remove the extra zeros after you've casted the numbers as strings using case statements, i.e.
case when right(Field, 4) = '0000' then
left(Field, len(Field) - 5) -- -5 to remove the decimal point
else case when right(Field, 3) = '000' Then
left(Field, len(Field) - 3)
else case when right(Field, 2) = '00' Then
left(Field, len(Field) - 2)
else case when right(Field, 1) = '0' Then
left(Field, len(Field) - 1)
end end end end
simple casting to REAL then back to varch(max) if you want will remove the leading 0s as follows:
declare #a as Decimal(15,4)=14.0010
select cast(cast (#a as real) as varchar(max)) as a2
-- ouptut is 14.001
declare #a as Decimal(15,4)=14.0000
select cast(cast (#a as real) as varchar(max)) as a2
-- output is 14
The Idea here is to use the built in string function of Trimming spaces to TRIM ZEROs from the converted decimal as follows:
declare #d as Decimal(15,4)=11400012.00000, #s0 VARCHAR(MAX), #s VARCHAR(MAX)
set #s0=cast(#d as varchar(max))
set #s=left(#s0,len(rtrim(replace(#s0,'0',' '))))
if right(#s,1)='.' set #s=left(#s,len(#s)-1)
select #s
-- returns 11400012.001
You also can define the function to do this task for you:
CREATE FUNCTION TRIM0(#s0 varchar(max))
RETURNS varchar(max)
AS
BEGIN
declare #s varchar(max)=''
set #s=left(#s0,len(rtrim(replace(#s0,'0',' '))))
if right(#s,1)='.' set #s=left(#s,len(#s)-1)
return #s
END
--With a lot of fantasy
--Tip: to optimize performances, first select values in a #-table, then do this transformation.
select
left( cast(Field as varchar(max)), charindex('.', cast(Field as varchar(max))) - 1) +
Case when reverse(cast(cast(reverse(substring(cast(Field as varchar), charindex('.', cast(Field as varchar)) + 1 ,4)) as int) as nvarchar)) = 0 Then '' Else
'.' + reverse(cast(cast(reverse(substring(cast(Field as varchar), charindex('.', cast(Field as varchar)) + 1 ,4)) as int) as nvarchar)) End

Convert time interval into decimal hours in SQL Server

I have a time interval in the format '88:52:57'
I need to convert it into a decimal hours in the format 88.88
How can I do this?
I have the data initially loaded as a varchar
You can use left, right and substring to extract the values and then do some calculations.
declare #S varchar(8) = '88:52:57';
select left(#S, 2) + substring(#S, 4, 2)/60.0 + right(#S, 2)/60.0/60.0;
If you not always have two digit values you can use parsename to get the values instead.
declare #S varchar(20) = '088:052:057';
select parsename(S.X, 3) + parsename(S.X, 2)/60.0 + parsename(S.X, 1)/60.0/60.0
from (select replace(#S, ':', '.')) as S(X)
Try it like this (best create an UDF from this):
First I split the Time-Variable on its double dots via XML. The rest is simple calculation...
DECLARE #YourTime VARCHAR(100)='88:52:57';
WITH Splitted AS
(
SELECT CAST('<x>' + REPLACE(#YourTime,':','</x><x>') + '</x>' AS XML) TimeParts
)
,TimeFract AS
(
SELECT TimeParts.value('/x[1]','float') AS HourPart
,CAST(TimeParts.value('/x[2]','int') * 60 + TimeParts.value('/x[3]','int') AS FLOAT) Seconds
FROM Splitted
)
SELECT HourPart + Seconds/3600
FROM TimeFract
The result
88,8825
Try this, solution is based on conversions, making it safe, if the format is always (h)h:mi:ss:
DECLARE #S varchar(8) = '88:52:57';
SELECT
CAST(REPLACE(left(#S, 2), ':', '') as int)+
CAST(CAST(CAST('0:'+RIGHT(#S, 5) as datetime) as decimal(10,10)) * 24 as decimal(2,2))
Result:
88.88

How to format numeric value when casting to varchar?

I have query similar to this:
DECLARE #value decimal(8,0) = 1
SELECT (CAST #value AS varchar(8))
How can I get output formatted with leading zeros (00000001, 00000023, 00000623 etc.)?
How can I do that?
It is simple task in .Net or Java, but I must do it inside view.
This should work:
DECLARE #value decimal(8,0) = 1
SELECT RIGHT('0000000' + CAST(#value AS varchar(8)), 8)
Try this
SELECT RIGHT('00000000' + CAST (#value AS varchar(8)),8)
try this:
declare #value varchar(8)='623';
Select ltrim(right(replicate(0,8) + CAST (#value AS varchar(8)),8))
SQL Fiddle demo
try this:
DECLARE #value decimal(8,0) = 1
SELECT REPLICATE('0',8-len(#value))+CAST(#value AS varchar(8))
You can use REPLICATE and RIGHT to do this, like so:
SELECT RIGHT(REPLICATE('0', 8) + CAST(#valueAS VARCHAR(8)), 8)
Live Demo

Formatting a number as a monetary value including separators

I need some help with a sql transformation. This part of query that I have been provided with:
'$' + replace(cast((CAST(p.Price1 AS decimal(10,2)) * cast(isnull(p.Multiplier,1) as decimal(10,2))) as varchar), '.0000', '')
Basically, it ends up being a varchar that looks like this: $26980
I need to insert a comma at the thousand and million mark (if applicable). So in this instance, $26,980
What's the easiest way to do that without having to rewrite the whole thing?
Do it on the client side. Having said that, this example should show you the way.
with p(price1, multiplier) as (select 1234.5, 10)
select '$' + replace(cast((CAST(p.Price1 AS decimal(10,2)) * cast(isnull(p.Multiplier,1) as decimal(10,2))) as varchar), '.0000', ''),
'$' + parsename(convert(varchar,cast(p.price1*isnull(p.Multiplier,1) as money),1),2)
from p
The key is in the last expression
'$' + parsename(convert(varchar,cast(p.price1*isnull(p.Multiplier,1) as money),1),2)
Note: if p.price1 is of a higher precision than decimal(10,2), then you may have to cast it in the expression as well to produce a faithful translation since the original CAST(p.Priced1 as decimal(10,2)) will be performing rounding.
If you really must do it in TSQL you can use CONVERT(), but this sort of thing really doesn't belong in the database:
declare #m money = 12345678
-- with decimal places
select '$' + convert(varchar, #m, 1)
-- without decimal places
select '$' + replace(convert(varchar, #m, 1), '.00', '')
You could turn this into a function, it only goes 50 characters back.
DECLARE #input VARCHAR(50)
SELECT #input = '123123123.00'
SELECT #input = CASE WHEN CHARINDEX('.', #input) > offset +1
THEN STUFF(#input, CHARINDEX('.', #input) - offset, 0, ',')
ELSE #input END
FROM (SELECT 3 offset UNION SELECT 7 UNION SELECT 12 UNION SELECT 18 UNION SELECT 25 UNION SELECT 33 UNION SELECT 42) b
PRINT #input
The offset grows by +1 for each position, because it's assuming you've already inserted the commas for the previous positions.

sql convert text string from 1 integer (8) to 3 integers (888)

I am having a little difficulty with the below code - it return the value I am after, but I need the value 3 digits long - i.e. it returns '1' but I need '001' - any help would be gratefully received
select convert(varchar(3),(select count(ptMatter) + 1 from lamatter where
convert(varchar(10), getdate(), 103)=convert(varchar(10), dateadd, 103)))
Select Right('000000' + convert(varchar(3), Result),3)
From yourTable
for your exact query:
Select Right('000000' + convert(varchar(3), (select count(ptMatter) + 1 from lamatter where convert(varchar(10), getdate(), 103)=convert(varchar(10), dateadd, 103)) ),3)
Look at your database documentation if the lpad function exists. For example, this is the mysql version : http://dev.mysql.com/doc/refman/5.1/en/string-functions.html#function_lpad
edit: sorry, didn't saw the tsql tag... Like said in another answer, the right function is the way to go, like explained here : Most efficient T-SQL way to pad a varchar on the left to a certain length?
What about something like this:
DECLARE #i int;
SELECT #i = 1;
SELECT REPLACE(STR(#i, 3), ' ', '0')