I have a float field, I need to convert it as varchar and I need to add a leading zero when it contains only one nummber(it can contains 1 or 2 numbers). This is my query:
SELECT FORMAT(((LTRIM(RTRIM(STR(floatField,2))))), '00')
but I get an error:data type for argument 1 is not valid. I know I can do this in a different way, but I want to understand what's wrong in this
format takes a float (among other things) and converts it to a varchar. Don't try to do its job for it:
SELECT FORMAT(myfloat, '00')
FROM mytable
DBFiddle Demo
Related
How can I add 5596 + 00003 to get 559600003 in SQL Server?
I tried with the following query:
select 5596 + '00003'
But it is giving 5599 and I want this to show 559600003.
You can also use CONCAT():
SELECT CONCAT(5596, '00003')
CONCAT() does not require any explicit conversion.
SQL Server attempts to return the result in the datatype of the first part of the calculation which in your case is a number. It can happily convert the second part of the calculation into a number, and therefore does so.
To obtain the result you want you must convert the first part of the calculation to a string e.g.
select convert(varchar, 5596) + '00003'
Note: convert(varchar,x) uses a default length of 30 which is probably enough for most numbers.
CONCAT(), as in one of the other answers is probably a better solution.
You can use CAST
SELECT CAST(5596 AS varchar) + '00003'; -- return 559600003
Because :
CAST is more easier to read than CONVERT
CAST is ANSI-SQL compliant : it means that the CAST function can be used by many databases
But in case you have a date type CONVERT is more flexible and contains more options than CAST
For more details take a look : https://learn.microsoft.com/en-us/sql/t-sql/functions/cast-and-convert-transact-sql?redirectedfrom=MSDN&view=sql-server-ver15
I need to convert varchar2 type value 100.0 into 100 (without decimal points) in Oracle SQL. Can you please help me?I used regexp_substr...but it fails in one situation given below.
SELECT REGEXP_SUBSTR(0.1,'(\d*)') FROM DUAL;---it results in Null but i want zero(0).
Note:- in Orcale sql developer
You want to display number in format You wish it to be displayed, so You should use formating functions. No need for rounding here in my opinion - query below should give You what You want.
SELECT
TO_CHAR(TO_NUMBER('100.1'),'FM9999') as res
FROM
dual
;
More on number formats can be found here.
Try to this....
SELECT TRUNC(TO_NUMBER(100.5)) FROM DUAL;
I have a column in varchar format and I would like to convert it to float. The values are like:
188234,62
188235
188235,43
188235,88
So, When I try to convert it I get an error:
Error converting data type varchar to float.
I thought that maybe some fields are not numeric and I checked with isnumeric() function, according to the query I concluded that all the values are numeric. Then I tried different queries to convert the field types:
Query1:
SELECT CarData.dbo.FinalData_OilLevel_new_send.KM
CASE WHEN ISNUMERIC(CarData.dbo.FinalData_OilLevel_new_send.KM) = 1
THEN CAST(CarData.dbo.FinalData_OilLevel_new_send.KM AS FLOAT)
ELSE NULL
END
FROM CarData.dbo.FinalData_OilLevel_new_send
Query2:
SELECT CASE ISNUMERIC(KM) WHEN 1 THEN CONVERT(float, KM)) ELSE null END from CarData.dbo.FinalData_OilLevel_new_send
However I always get the same error. Are there some other ways to convert varchar to float?
You have to use a dot as decimal separator:
CAST(REPLACE(CarData.dbo.FinalData_OilLevel_new_send.KM,',','.') AS FLOAT)
At first you need to Replace
The , present
try like this!
select cast(replace(val,',','') as float) from table
SEE DEMO
I am using Sybase and I am doing a select which returns me a column called "iftype", but its type is int and I need to convert into varchar. When I try to do the select without the convert function I get this error:
Error code 257, SQL state 37000: Implicit conversion from datatype 'VARCHAR' to 'INT' is not allowed. Use the CONVERT function to run this query.
I dont know how to implement the function CONVERT. Can anyone help me, please ?
Use the convert function.
SELECT CONVERT(varchar(10), field_name) FROM table_name
Use the STR function:
SELECT STR(field_name) FROM table_name
Arguments
float_expression
Is an expression of approximate numeric (float) data type with a decimal point.
length
Is the total length. This includes decimal point, sign, digits, and spaces. The default is 10.
decimal
Is the number of places to the right of the decimal point. decimal must be less than or equal to 16. If decimal is more than 16 then the result is truncated to sixteen places to the right of the decimal point.
source: https://msdn.microsoft.com/en-us/library/ms189527.aspx
You can use CAST function:
SELECT CAST(your_column_name AS varchar(10)) FROM your_table_name
Actually you don't need to use STR Or Convert. Just select 'xxx'+LTRIM(ColumnName) does the job.
Possibly, LTRIM uses Convert or STR under the hood.
LTRIM also removes need for providing length. It seems to be working for integer or float without worry of truncation.
SELECT LTRIM(ColumnName) FROM TableName
also, LTRIM is better than STR as
SELECT STR(1234567890.123)
gives 1234567890
whereas
SELECT LTRIM(1234567890.123)
gives 1234567890.123
SELECT Cast(Cast([field_name] AS BIGINT) AS NVARCHAR(255))
FROM table_name
CONVERT(DATA_TYPE , Your_Column) is the syntax for CONVERT method in SQL. From this convert function we can convert the data of the Column which is on the right side of the comma (,) to the data type in the left side of the comma (,) Please see below example.
SELECT CONVERT (VARCHAR(10), ColumnName) FROM TableName
I have a question that bothers me. How can i convert a varchar to number when inside the varchar value consists of alphabets.
For my varchar price column values:
14dollars10cents
15dollars20cents
By converting it to varchar to number price column, the values should be:
1410
1520
I know that if the varchar does not consists any alphabets, it can auto convert by"
SELECT CONVERT(INT, PRICE) FROM Table
Is there any way to get rid of the alphabets in the middle as I would like to do mathematical function on it.
Updated attempt of putting fixed point number in:
SELECT CAST (Replace(REPLACE(PRICE, 'dollars', '.'),'cents','') AS Number(4,2)))
FROM TEST;
Thanks
You could just use REGEXP_REPLACE to remove all non digit characters:
SELECT REGEXP_REPLACE(price, '[^[:digit:]]')
FROM table;
To then convert this to a number:
SELECT TO_NUMBER(REGEXP_REPLACE(price, '[^[:digit:]]'))
FROM table;
If you want to add the point in then you can do that with REGEXP_REPLACE too:
SELECT TO_NUMBER(REGEXP_REPLACE(val, '^([0-9]*)([^[:digit:]]*)([0-9]*)(.*)$', '\1.\3'))
FROM table;
Voila...
SELECT CAST(REPLACE(YourVarcharCol, 'dollars', '') AS INT) FROM Table
The issue with this is it will break if the varchar still contains alpha-numeric characters.
How about using translate to strip out the unwanted characters.
SELECT TO_NUMBER(TRANSLATE('14dollars10cents','1234567890dolarscents','1234567890')) FROM DUAL
No I don't think there is direct way.
you can do string parsing to get your integer value.