I got a problem with converting values.
My Source values are:
1
2
3
4
1 - abcd
2 - xyzabcdefgh
abcdefgh
lmnop
I need the output as
1.00
2.00
3.00
4.00
The problem I have is that some fields contain letters only, and there are also those that contains contain both letters and digits.
I only need to format those numbers only:
1 as 1.00
2 as 2.00
The fields containing letters need to remain the same.
Any help please?
Try something like
select case
when MyVarcharColumn NOT LIKE '%[^0-9]%' then convert(decimal(18,2), MyVarcharColumn)
else MyVarcharColumn
end as MyVarcharColumn
from x
Might not be the most elegent/efficient solution, but it should work
I also Agree with Joe
You can do what Lee said, or enclose within a try catch
Declare #str varchar(10)
Set #str = '1'
Begin Try
Select Convert(Decimal(10,2), #str) -- scale 10, precision 2
End Try
Begin Catch
Select 'Not Numeric'
End Catch
try this
select
case when col1 NOT LIKE '%[^0-9]%'
then convert(varchar(50), convert(decimal(18,2), col1))
else col1 end as col1 from mysource
Related
Good day
I have :
TableX
Column1
John Smith 007
Tera Name 111
Bob Eva 554
I need
TableX
Column1 Column2
John Smith 007 007
Tera Name 111 111
Bob Eva 554 554
I created code but not work. I think there must be join to recognise columns.
ALTER TABLE [dbo].[TableX]
ADD Column2 varchar (50);
UPDATE [dbo].[TableX] SET
Column1=Column2
WHERE select SUBSTRING([Column1], PATINDEX('%[0-9]%', [Column1]
), LEN([column1]))
Thanks for help
If the number you want to extract is always at the end, then you can use:
PATINDEX('%[^0-9]%', REVERSE(Column1))
to get the index of the first character that is not a number, starting from the end.
So, to extract the number you can use:
RIGHT(Column1, PATINDEX('%[^0-9]%', REVERSE(Column1)) - 1)
Hence, the UPDATE will look like this:
UPDATE [dbo].[TableX]
SET Column2 = RIGHT(Column1, PATINDEX('%[^0-9]%', REVERSE(Column1)) - 1)
Demo here
Assuming required part length = 3
UPDATE [dbo].[TableX] SET
column2 = RTRIM(right(column1, CHARINDEX('/', column1) +3))
I have records like
Price 0
Price 23
Price 555
When I use SELECT ... cast(Price as decimal(10,2))
I get this weird error : if price is 0 = I get .00 not 0.00. All other variations work (23 goes to 23.00 as I would like). How to fix zeros?
If you are using SQL Server >= 2012 you can use format:
declare #Price int = 0
select format(#Price, 'N') --General format
select format(#Price, 'C') --Currency format
Instead of variable you can use appropriate table columns in your table
i have the following T-SQL statement:
select top 10 value1 from table1 where condition1 = 12345
result:
5449.0
228231.0
0.0
3128.0
6560.0
4541.0
2119.0
0.0
0.0
4183.0
the data type of value1 = "[char] (20) COLLATE Latin1_General_CS_AS NULL"
please note, each result line has 20 chars i.e. "5449.0______________" filled up with spaces.
I want to sum all this columns. how can i convert these values to a summable data type?
Use cast or convert:
-- for demo
declare #v char(20)
set #v = '228231.9 '
-- real magic
select cast(#v as real)
So this is the select in your case:
select cast(value1 as real) from table1 where condition1 = 12345
select sum(cast(value1 as real)) from table1 where condition1 = 12345
I'm having trouble with my database because its got auditing worked in and things are hard. I need to compare the current change with the last change so I thought I'd include a grouping column. But when I run my code, some of the column values are . It goes 1,2,3,4,5,6,7,8,9, . What the ... idon'tevenknow ?
This is what I'm doing:
DECLARE #Person char(11), #DonationYTD decimal(10, 2), #OldPerson char(11), #OldDonationYTD decimal(10, 2), #Group int;
SET #Group=1;
DECLARE TempCursor CURSOR FOR Select PersonID, DonationYTD FROM MyTable;
OPEN TempCursor;
FETCH NEXT FROM TempCursor INTO #Person, #DonationYTD ;
WHILE ##FETCH_STATUS=0
BEGIN
IF( #Person != #OldPerson)
SET #Group=1;
IF( #Person = #OldPerson AND #DonationYTD!=#OldDonationYTD)
SET #Group=#Group+1;
UPDATE MyTable SET CHANGEGROUP=#Group WHERE PersonID=#Person AND DonationYTD=#DonationYTD;
SET #OldPerson = #Person;
SET #OldDonationYTD = #DonationYTD;
FETCH NEXT FROM TempCursor INTO #Person, #DonationYTD ;
END
CLOSE TempCursor;
DEALLOCATE TempCursor;
SELECT PersonID, DonationYTD, Changegroup FROM MyTable
1 15.00 1
1 15.00 1
1 20.00 1
2 3.00 1
2 4.00 2
2 15.00 3
2 8.00 4
2 4.00 5
2 15.00 6
2 3.00 7
2 3.00 7
2 9.00 8
2 9.00 8
2 10.00 9
2 14.00 *
2 14.00 *
If I try to do anything with Changegroup it tells me it can't convert varchar symbol * to integer.
Why am I getting an asterisk? How can I fix it?
You are encountering the issue described here
When integers are implicitly converted to a character data type, if
the integer is too large to fit into the character field, SQL Server
enters ASCII character 42, the asterisk (*).
From which I deduce your column must be [var]char(1). This odd behaviour does not occur for the newer n[var]char types, as discussed here.
The fix would be to change the column datatype. Ideally to a numeric datatype such as int but if it must be string at least one long enough to hold the intended contents.
How does SQL Server know to retrieve these values this way?
Key someMoney
----------- ---------------------
1 5.00
2 5.002
3 5.0001
Basically, I'm wondering how to know how many decimal places there are without much of a performance hit.
I want to get
Key someMoney places
----------- --------------------- ----------
1 5.00 2
2 5.002 3
3 5.0001 4
Money has 4 decimal places....it's a fixed-point data type.
http://msdn.microsoft.com/en-us/library/ms179882.aspx
Is SQL Server 'MONEY' data type a decimal floating point or binary floating point?
So this is a huge ugly hack, but it will give you the value you're looking for...
DECLARE #TestValue MONEY
SET #TestValue = 1.001
DECLARE #TestString VARCHAR(50)
SET #TestString = REPLACE(RTRIM(REPLACE(CONVERT(VARCHAR, CONVERT(DECIMAL(10,4), #TestValue)), '0', ' ')), ' ', '0')
SELECT LEN(#TestString) - CHARINDEX('.', #TestString) AS Places
This produces the correct results, but I'm not sure if it performs well enough for you and I haven't tried it with data other than the examples you listed:
;
with money_cte ([Key], [someMoney])
as
(
select 1, cast(5.00 as money)
union
select 2, cast(5.002 as money)
union
select 3, cast(5.0001 as money)
)
select [Key], [someMoney], abs(floor(log10([someMoney] - round([someMoney], 0, 1)))) as places
from money_cte
where [someMoney] - round([someMoney], 0, 1) <> 0
union
select [Key], [someMoney], 2 as places
from money_cte
where [someMoney] - round([someMoney], 0, 1) = 0
The client is formatting that. SQL Server SSMS or whatever. SQL Server is returning a full money value in the data stream and it takes a full 8 bytes. (http://msdn.microsoft.com/en-us/library/cc448435.aspx). If you have SQL Server convert to varchar, it defaults to 2 decimal places
Notice that the Stack Overflow data browser doesn't even show the same results you have:
https://data.stackexchange.com/stackoverflow/q/111288/
;
with money_cte ([Key], [someMoney])
as
(
select 1, cast(5.00 as money)
union
select 2, cast(5.002 as money)
union
select 3, cast(5.0001 as money)
)
select *
, CONVERT(varchar, someMoney) AS varchar_version
, CONVERT(varchar, someMoney, 2) AS varchar_version2
FROM money_cte