I am facing below error while running the query - sql

I am facing below error while running the query
SELECT (0.01 * CAST(instruct_to_bal_avl_aft AS INT) )
FROM [FullYear].[dbo].[aa_momo_log]
Msg 245, Level 16, State 1, Line 12 Conversion failed when converting the varchar value '3.0' to data type int.

Since you are multiplying by a float value you can cast as DECIMAL instead of INT:
SELECT (0.01 * CAST(instruct_to_bal_avl_aft AS DECIMAL) )
FROM [FullYear].[dbo].[aa_momo_log]

Related

How to convert NVarchar to Decimal?

I have a column as nvarchar(max) whose value is
'1.7925e+006'
How can this be converted to decimal? I have tried as below
declare #pd as nvarchar(max) = '1.7925e+006'
Select convert(decimal(18, 2), #pd)
but I get an erorr:
Msg 8114, Level 16, State 5, Line 4
Error converting data type nvarchar to numeric.
Please suggest.
As the string represents a floating point number, you'll need to convert the value to a float first, and then a decimal:
SELECT CONVERT(decimal(18,2),CONVERT(float,'1.7925e+006'));

Convert a sysdate() into int datatype

I have a sysdate() value - 2016.10.18. I need a SQL to convert it to int in MS SQL SERVER.
INPUT - 2016.10.18
OUTPUT reqd. - 20161018
I tried this -
SELECT CONVERT(int, '2016.10.18')
but it throws the following error:
Msg 245, Level 16, State 1, Line 1
Conversion failed when converting the varchar value '2016.10.18' to data type int.
Also tried this -
select CONVERT(int,CONVERT(decimal(19,2),'2016.10.18'))
Getting the following error:
Msg 8114, Level 16, State 5, Line 1
Error converting data type varchar to numeric.
Could someone help me out!?
Select Convert(int,Convert(varchar,cast('2016.10.18' as date),112))
Returns
20161018
Either of the following will work. If you have a use the replace() method, you will need to strip the time portion off via LEFT(...,10)
Select Convert(int,Convert(varchar,cast('2016.10.18 14:24:49' as date),112))
Select cast(Replace(Left('2016.10.18 14:24:49',10),'.','') as int)
Updated answer.
Select Cast(Convert(varchar(8), Cast(Left('2016.10.18 14:24:49',10) as Date), 112) as int)

Convert julian date to date in sql server

I want to convert the julian date in a date format. 
Below, please find an example to create my error. All my dates have 5 digits I need to convert.
create table #test (dateR int)
insert into #test (dateR)
values (39596),(39596),(39595),(39595),(39593),(39592),(39592),(39589),(38104),(38104),(37957)
SELECT * from #test
select *
, dateadd (year, dateR/1000 - 1900, dateR %1000 - 1) as Rd
from #test
Getting the error:
Msg 517, Level 16, State 1, Line 2 Adding a value to a 'datetime'
column caused an overflow.
Also tried:
SELECT DATEADD(dd, CONVERT(int, RIGHT(dateR,3)) - 1, CONVERT(datetime,SUBSTRING(dateR,1,2)+'0101', 212))
This returns:
Msg 8116 level 16 state 1 line 1 nullArgument data type int is invalid for argument of substring function
You can just cast the INT value to a DATETIME.
For example:
SELECT CAST(39596 AS DATETIME); -- Returns 2008-05-30 00:00:00.000

Int to Date in SQL SERVER 2000

How to convert an int column ,Birthdate( sample value 20090301) to a date column in the format(01/03/2009)
Sql Server 2000
I was trying to convert some thing like below
select * from tabl
where
cast(dob as datetime)>='01/03/2009'
which gives an error
Msg 8115, Level 16, State 2, Line 1
Arithmetic overflow error converting expression to data type datetime.
SQL Server doesn't allow you to cast int -> datetime directly, but if you're sure you can cast it to nvarchar first and then cast it to date.
For your question here's an example
declare #test int = 20090301
select CONVERT(datetime, CAST(#test as nvarchar), 112)
112 is the format you mentioned, here's the list of all possible formats for Convert function.
try this !
select * from tabl
where
cast(convert(char(8),dob, 112) as date)>='2009-03-01'

SQL CAST or Convert to SUM

I have a table called T5VAULTSL3 in which my clients are under the column heading VL3CLIENTNUM and their cash amounts are under VL3CONSIDERATION;
VL3CONSIDERATION is showing object explorer as (char(20), null)
What I am trying to achieve is to group all 'Unique' client numbers where the combined cash (consideration) is > 10000
I have tried;
SELECT vl3clientnum, CAST(VL3CONSIDERATION AS NUMERIC (20,2))
FROM T5VAULTSL3
WHERE Vl3CONSIDERATION > 10000
which returns
Msg 245, Level 16, State 1, Line 1
Conversion failed when converting the varchar value ' 0.00' to data type int.
So I tried
SELECT vl3clientnum, VL3CONSIDERATION
FROM T5VAULTSL3
WHERE CAST (Vl3CONSIDERATION AS NUMERIC (20,2)) > 10000
which returns
Msg 8114, Level 16, State 5, Line 1
Error converting data type varchar to numeric.
And
SELECT vl3clientnum, CAST (VL3CONSIDERATION AS NUMERIC (20,2)) AS CONSIDERATION
FROM T5VAULTSL3
WHERE CAST (Vl3CONSIDERATION AS NUMERIC (20,2)) > 10000
which returns
Msg 8114, Level 16, State 5, Line 1
Error converting data type varchar to numeric.
Would I have to use the 'Declare' functionality and if so how? A little stumped by this one...
Other attempts have included.
SELECT vl3clientnum, CAST (VL3CONSIDERATION AS NUMERIC (20,2))
FROM T5VAULTSL3
WHERE VL3CONSIDERATION > 10000
GROUP BY VL3CLIENTNUM, vl3consideration
Msg 245, Level 16, State 1, Line 1
Conversion failed when converting the varchar value ' 0.00' to data type int.
SELECT vl3clientnum, VL3CONSIDERATION
FROM T5VAULTSL3
WHERE CAST (VL3CONSIDERATION as NUMERIC(20,2)) > 10000
GROUP BY VL3CLIENTNUM, vl3consideration
Msg 8114, Level 16, State 5, Line 1
Error converting data type varchar to numeric.
Absoultely perplexed!!!!
I guess the reason is that ' 0.00' has a space in it. You better trim spaces:
CAST(LTRIM(RTRIM(VL3CONSIDERATION)) AS NUMERIC (20,2))