Concatenate derived currency symbol and value in SQL - sql

I am using MSSQL (2012) and need to return a numeric value that is cast in decimal to include the relevant currency symbol. I have returned the currency symbol using CASE as follows:
SELECT (CASE (Currency_ID) WHEN (1) THEN '£' WHEN (2) THEN '$' END) AS Currency_Symbol
I would then like to concatenate this with the numeric value, which I have had to CAST to correct the decimal places as follows:
SELECT CAST(Price AS decimal(10,2)) AS Unit_Price
However, I seem unable to use CONCAT or any other method to do this, and as I understand it I won't be able to reference the 'Currency_Symbol' value in the same SELECT statement.
Any ideas?

declare #Currency_ID int, #dec decimal(8,2)
set #Currency_ID = 1;
set #dec = 123.45
SELECT (CASE (#Currency_ID) WHEN (1) THEN '£' WHEN (2) THEN '$' END) + cast(#dec as varchar)
Modified .. If you are using a table and the Currency_ID and Unit_Price is from a table
SELECT (CASE (Currency_ID) WHEN (1) THEN '£' WHEN (2) THEN '$' END) + cast(Unit_Price as varchar) FROM SOMETABLE

Related

First CAST then concatenate (||) in Firebird

How does one cast and then join elements together in Firebird?
SELECT
ARTICLE_NAME ||','||CAST
FROM
(select cast(QUANTITY as varchar(50)) from PREORDER_ITEM)
I also tried
with DATA as ( select cast(QUANTITY as varchar(50) ) from PREORDER_ITEM )
SELECT
ARTICLE_NAME ||','||QUANTITY
FROM
DATA
Why doesn't this work?
There are multiple problems with your query:
When you do select cast(QUANTITY as varchar(50) ) from PREORDER_ITEM you have only one (1) column in the derived table. In other words, there is no ARTICLE_NAME in either query.
For a select expression of a derived table or common table expression, all columns must have an explicit column name. So, for an expression you need to specify an explicit alias using (AS <alias>). In other words, there is no column CAST in your first query, and no column QUANTITY in the second.
CAST is a reserved word, so when used as a column name like in your first query, it must be delimited by double quotes.
So, use:
select article_name || quantity
from (
select article_name, cast(quantity as varchar(50)) as quantity
from preorder_item
)
or
with data as (
select article_name, cast(quantity as varchar(50)) as quantity
from preorder_item
)
select article_name || quantity
from data
As an aside, you don't need to explicitly cast. When concatenating values, Firebird will automatically coerce to a string value, so you can also use:
select article_name || quantity
from preorder_item

Cast or convert varchar in SQL Server to INT

I am trying to convert the following to an integer so that I can do calculations, from 123.456.789 to 123456789.
However, when I try
CAST([Total number] AS INT)
OR
CONVERT(int, [Total number])
then I get an error
Conversion failed when converting the varchar value '123.456.789' to data type int.
Declare #val varchar(100)='123.456.789'
SELECT CAST (REPLACE (#val,'.','') AS INT) As Intval
Assuming the dot is not a fractional part divider and your number is integer (not float/decimal) - you can just remove dots from your string prior to conversion something like:
CAST(replace([Total number], '.', '') AS int)
You can use try_convert or try_cast even if it has any special characters it will return null in that case
Select try_convert(int, replace([Total number], '.', ''))
Is this your requirement as below
Declare #val TABLE (ID INT IDENTITY,Val varchar(100))
INSERT INTO #val
SELECT '356.345.789' UNION ALL
SELECT '123.456.333' UNION ALL
SELECT '444.124.444' UNION ALL
SELECT '347.456.555' UNION ALL
SELECT '123.666.789' UNION ALL
SELECT '678.432.789' UNION ALL
SELECT '789.456.111' UNION ALL
SELECT '123.457.123' UNION ALL
SELECT '333.111.789'
SELECT ROW_NUMBER()OVER(Order by (SELECT 1)) AS Rno,
CAST(REPLACE (Val,'.','') AS INT) As Intval
FROM #val

trim sql variant

I have column sql variant, which has the following meanings: 100, 150, D1
I'm trying to convert all numbers in the columns into letters (such as D1) according to specific logic in case when. But 150 has spaces and the CASE WHEN doesn't work.
Here's the query I'm using:
Select *,
Case When LTrim(Cast(AttributeValue As NVarchar(Max))) Between 0 And 200 Then 'D1'
Else 'Other'
End
From MyTable As SCR With (NoLock);
I tried the following
LTRIM(CAST column AS VARCHAR(MAX))
but now I get error:
Conversion failed when converting the nvarchar value 'D1' to data type int
How can I remove spaces from sql_variant?
As per your comments edited to use BIGINT due to having larger numbers and leave the column the same if it is not BETWEEN 0 and 400
SELECT *
,CASE
WHEN ISNUMERIC(LTRIM(CAST(AttributeValue AS NVARCHAR(MAX)))) = 1
AND CAST(LTRIM(CAST(AttributeValue AS NVARCHAR(MAX))) AS BIGINT) BETWEEN 0 AND 400 THEN 'D1'
ELSE AttributeValue
END
FROM
MyTable AS SCR WITH (NOLOCK)
You can use the ISNUMERIC() function to determine which of your sql_variants are integers and which are not.
The reason your code is failing isn't because of the trim it is because you are comparing a VARCHAR with an INTEGER so SQL is trying to automatically re cast your final string as an integer which in the case of D1 is not numeric so it causes a conversion error.
Also note that you cannot use sql_variant directly in the ISNUMERIC() function so cast to a varchar first.
Here is an entire example of you to show you how it works:
DECLARE #MyTable AS TABLE (AttributeValue SQL_VARIANT)
INSERT INTO #MyTable VALUES
(CAST(' 150' AS VARCHAR(100)))
,(CAST('D1' AS VARCHAR(100)))
SELECT *
,CASE
WHEN ISNUMERIC(LTRIM(CAST(AttributeValue AS NVARCHAR(MAX)))) = 1
AND CAST(LTRIM(CAST(AttributeValue AS NVARCHAR(MAX))) AS INT) BETWEEN 0 AND 200 THEN 'D1'
ELSE 'Other'
END
FROM
#MyTable AS SCR
use sql replace function
select replace(columnnName, ' ', '')

Varchar to Decimal conversion in DB2 with empty and null values

I have a varchar field AMOUNT in DB2.
Possible table state values:
ID AMOUNT
-------------------------
1 123.4578
2 NULL
2 123.78
1 -8562.85441
2
1 0.0
-------------------------
Column AMOUNT can be empty as the second last row above depicts.
I want to do a SUM over the AMOUNT group by ID in a query.
The result SUM should be DECIMAL(16,2).
What will be the correct way to do that considering that the value can be both null and empty and also the number of digits after the decimal is not following any fixed format?
It's a duplicate of this question, but the answers given are not complete and not accepted.
Thanks for reading!
I think the code you want is:
select (case when amount <> '' then cast(amount as float) end)
from table t;
You don't have to worry about NULL values. sum() ignores them. If you want to get 0 if all values are NULL/blank, then add else 0 to the case statement.
If you are concerned about other non-numeric values, you can try:
select (case when amount <> '' and
not regexp_like(amount, '[^0-9.]', '')
then cast(amount as float) end
from table t;
CAST(CASE WHEN ISNULL(AMOUNT, '') = '' THEN NULL ELSE REPLACE(AMOUNT, ',', '.') END AS DECIMAL (16,2)) AS AMOUNT
or
CAST (123.4578 AS DECIMAL (16,2))
+ CAST (NULL AS DECIMAL (16,2))
+ CAST (123.78 AS DECIMAL (16,2))
+ CAST (-8562.85441 AS DECIMAL (16,2))
= RESULT DECIMAL(16,2)

Select and filter nvarchar like a int

I have a nvarchar column BigMacs in table McTable in my MS SQL 2005 database with alfanumeric and numeric values. For example:
132
432adfad
sfs54543
5256
And now i would like to do something like this:
select Convert(BigMacs, int) from McTable
where IsNumerc(BigMacs) = 1 AND Convert(BigMacs, int) > 6
But when I do this i get a error:
Msg 245, Level 16, State 1, Line 41
Conversion failed when converting the nvarchar value '.' to data type int.
On line select.
How to fix this?
This is probably because the IsNumeric function returns true for any value that COULD be converted to a number. Try the following example:
create table McTable (BigMac varchar(255))
insert into McTable select '1p927'
insert into McTable select '1927'
insert into McTable select '1,927'
insert into McTable select '1.927'
select BigMac, isnumeric(BigMac)
from McTable
select BigMac, CAST(BigMac AS DECIMAL)
from McTable
where isnumeric(BigMac) = 1
Even though all rows except the '1p927' are numeric, the cast will fail! This is because '1,927' cannot be converted to a Decimal (on my machine)
IsNumeric doesn't work exactly as specified. As found here, you could use
IsNumeric (data + 'e0')
-Edo
Try this:
SELECT *
FROM (
SELECT REPLACE(BigMacs, ',', '.') AS BigMacs
FROM McTable m
WHERE IsNumerc(BigMacs) = 1
) q
WHERE CAST(BigMacs AS DECIMAL) > 6
IsNumeric will return TRUE on decimal fractions like 1234.1232, but they cannot be converted to INT.
Checking:
WITH McTable AS
(
SELECT '123124,123123' AS BigMacs
)
SELECT *
FROM (
SELECT REPLACE(BigMacs, ',', '.') AS BigMacs
FROM McTable
WHERE IsNumeric(BigMacs) = 1
) q
WHERE CAST(BigMacs AS DECIMAL) > 6
-----------
123124.123123
There are many ways to accomplish this. Both of these work the same. It is best not to use replace in this situation as there are too many unknowns to account for to replace. It is best to filter everything that is NOT what your after.
SELECT
CONVERT(INT,BigMacs) AS BigMacs
FROM
McTable
WHERE
ISNUMERIC(BigMacs) = 1
AND PATINDEX('%[^0-9]%', BigMacs) = 0
SELECT
CONVERT(INT,BigMacs) AS BigMacs
FROM
McTable
WHERE
ISNUMERIC(BigMacs) = 1
AND BigMacs NOT LIKE ('%[^0-9]%')
Note: It helps if people spell ISNUMERIC() correctly. It also helps if you use the correct syntax order on CONVERT()