Error with CAST SQL - sql-server-2005

I am trying to combine two expressions and display them as a header. Displaying them as a header isn't the problem, combining the two lines is what's holding me back.
I am using the expression:
CAST(Max((#PurchaseCost + #Prod_CostLBS) * #InputWeight) AS DECIMAL (10,4)) + SPACE(10) +
CAST(#InputWeight - SUM(Sum([IC_ProductLots].[OriginalQuantity_Stk])) OVER () AS DECIMAL (10,4))
I tried using INT NUMERICand FLOAT as well. The only one that didn't return an error was float but that messed up the numbers.
I am trying to get an output like this
Cost: $10,000 Shrink: 120
Whether I use or don't use CAST I get the error:
Error converting data type varchar to numeric.
I can provide my full code if needed.
I am using Microsoft SQL Server 2005.

You try to concatenate numeric and string:
This is a numeric:
CAST(Max((#PurchaseCost + #Prod_CostLBS) * #InputWeight) AS DECIMAL (10,4)) +
This is a string: show official documentation of SPACE function
SPACE(10) +
This is a numeric:
CAST(#InputWeight - SUM(Sum([IC_ProductLots].[OriginalQuantity_Stk])) OVER () AS DECIMAL (10,4))
If you want to use only string please cast using varchar instead of decimal(10,4)
in this way:
CAST(Max((#PurchaseCost + #Prod_CostLBS) * #InputWeight) AS VARCHAR) +
SPACE(10) +
CAST(#InputWeight - SUM(Sum([IC_ProductLots].[OriginalQuantity_Stk])) OVER () AS VARCHAR (10,4))

Related

SQL Server SELECT To Return String Including Single Quotes

I need to get a formatted string with data from a SELECT statement. The problem is that I need it to return with single quotes. Here's an example.
I have a table that contains 2 columns TrainingSwipeID (int) and ExtendedDate (datetime). For an example I have a row in the table that contains
TrainingSwipeID = 123
TrainingEnd = 04/23/2019 09:00:00
I need to create a SELECT statement that will return the formatted string such as
{'TrainingSwipeID':123,'TrainingEnd ':04/23/2019 09:00:00}
I've researched and found that you can double single quote to get around this issue. I've tried the following with no luck and I get the following error "Conversion failed when converting the varchar value '{'TrainingSwipeID':' to data type int."
SELECT '{''TrainingSwipeID'':' + TrainingSwipeID + '''TrainingEnd'':' + TrainingEnd + '}'
AS MyFormattedString
FROM TrainingSwipe
Can anyone help?
The numeric and date/time data types have a higher precedence than the string data types. That's why you need to convert the numeric types into strings and prevent undesired implicit conversions.
SELECT '{''TrainingSwipeID'':' + CAST(TrainingSwipeID AS varchar(15))
+ '''TrainingEnd'':' + CONVERT( varchar(20), TrainingEnd , 101) + ' '
+ CONVERT( varchar(20), TrainingEnd , 8) + '}'
AS MyFormattedString
FROM TrainingSwipe
I finally used the simplest answer from #AlexKudryashev but Luis' answer worked as well.
SELECT CONCAT('{''TrainingSwipeID'':' , TrainingSwipeID, ',''TrainingEnd'':', TrainingEnd, '}')

Splitting field and adding decimal point to create numeric value SQL

I have a field in SQL Server 2014 that I am working with that looks like this:
**RawField**
20060202
20060323
I want to add a split the field and add a decimal point and create a numerical field. This is what I would like to see:
**RawField**
200602.02
200603.23
So I need to split the field, add the decimal point, and convert to a numerical value. I tried some code but was getting an error. Please see my code below:
select top 1000 cast(SUBSTRING(cast(RawField as varchar(6)),1,6) + cast('.' as varchar(1)) + SUBSTRING(cast(RawField as varchar(2)),6,2) as int)
from Table
I get an error of:
Msg 245, Level 16, State 1, Line 11
Conversion failed when converting the varchar value '200602.' to data type int.
Is this a good approach?
you want to convert the string to numeric with 2 decimal places ?
select convert(decimal(10,2), RawField) / 100.0
I guest your RawField contains other alphanumeric after that and you only posted the first 8 characters ?
this should work. Just take the first 8 characters and convert. Simple and direct
select convert(decimal(10,2), left(RawField, 8)) / 100.0
Please try this.
select top 1000 cast(SUBSTRING(cast(RawField as varchar(6)),1,6) + cast('.' as varchar(1)) + SUBSTRING(cast(RawField as varchar(2)),6,2) as numeric(8,2))
from Table
You are trying to cast string with decimal number to int.
Use float/numeric/decimal when casting
select cast(SUBSTRING(RawField ,1,6) + cast('.' as varchar(1)) + SUBSTRING(RawField ,7,2) as numeric(16,2))

Check IP address range

I try to make IP to country query.
Simplest way is make query like this:
select Country
from dbip
where 8 * power(255,3) +
8 * power(255,2) +
8 * power(255,1) + 8
between FromIPInt and ToIPInt
It works for small Google's IP, but for greater IP:
Arithmetic overflow error converting expression to data type int.
Which other way I can compute it?
CAST is the solution, but you have do put it in the right place :
select Country
from dbip
where cast(255 as bigint) * power(255,3)
+ 255 * power(255,2)
+ 255 * power(255,1)
+ 255
between FromIPInt and ToIPInt
The error Arithmetic overflow error converting expression to data type int occurs if you CAST the whole calculation:
select cast(255 * power(255,3) as bigint)
But not if you cast your first number
select cast(255 as bigint) * power(255,3)
More details about that: https://msdn.microsoft.com/en-us/library/ms187745.aspx
Integer constants greater than 2,147,483,647 are converted to the decimal data type, not the bigint data type.
SQL Server does not automatically promote other integer data types (tinyint, smallint, and int) to bigint.

Concatenate after using Maths - SQL Server 2005

Is it possible to concatenate after using a Math equation?
I have a simple calculation running like so
Value / 100 * 0.5 + Value as Total Value
example would be 3000 / 100 * 0.50 = 3015.
But I would like to include a star with it.
I.e.
Value / 100 * 0.5 + Value + ' *' as Total Value
to display as 3015 *
However I'm getting an error saying
Error converting data type varchar to numeric.
Is here a way around this? Would it be possible to then convert the total value to a varchar and concatenate the star?
You cannot just concatenate together numerical and string values - so you need to do:
SELECT CAST((Value / 100 * 0.5 + Value) AS VARCHAR(20)) + ' *' as Total Value
First determine your result from your computation, then cast it to a varchar large enough to hold the result, and concatenate that casted string with the star string literal.

SQL Base32 Conversion

I am writing a SQL Function that will take in a decimal and return the base32 representation of that decimal.
My problem is with the decimal to ascii conversion.
I am allowed to run the following query and return an ascii character
"SELECT CHAR( 65 )" which returns "A"
However in my function when I am trying to build my output string of letters, I am having trouble casting a bigint into a char, then concatenate that char to the end of a another char(which would be my output).
Sample line of code: "SET #OutputChar = #OutputChar + CAST( ( #Output + 55 ) AS CHAR(255) )"
What is the proper way to cast a bigint to char and then concatenate that char to another?
Thanks
How are you declaring #OutputChar?
If you have:
DECLARE #OutputChar CHAR(255)
then every time you concantenate, it will truncate it to 255. CHAR includes the whitespace at the end, maybe you mean to use varchar instead of CHAR?
Try this:
SET #OutputChar = #OutputChar + CONVERT(varchar(255),#Output + 55)