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'));
Related
Does anyone know why something like this:
SELECT DATEADD(HOUR,4,'2021-11-01 05:10:00.0000000')
returns the following error:
Msg 241, Level 16, State 1, Line 1
Conversion failed when converting date and/or time from character string.
But when using only up to milliseconds like
SELECT DATEADD(HOUR, 4, '2021-11-01 05:10:00.000')
it works and returns:
2021-11-01 13:10:00.000
And when using it in a table where the column includes the nanosecond like
SELECT DATEADD(HOUR, 4, column-name)
it also works.
Because you've supplied a literal string, and so DATEADD converts the value to it's default data type, datetime. A datetime is accurate to 1/300th of a second and a value like 2021-11-01 05:10:00.0000000 therefore is too large; it has an accuracy greater than 1/300th of a second.
If you explicitly converted the value to a date and time data type that goes up to 1/1000000 of a second, it would work:
SELECT DATEADD(HOUR,4,CONVERT(datetime2(7),'2021-11-01 05:10:00.0000000'))
From the dateadd documentation:
A string literal value must resolve to a datetime.
The datetime type can only accept milliseconds. Attempts to parse a string that has more accuracy than milliseconds will fail.
(Note that the datetime is rounded .000, .003, or .007 seconds. For example, 1 ms is rounded down to 0.)
The same error occurs if you try to declare a datetime and initialize it to a literal string that has more than 3 decimal places of second accuracy:
DECLARE #datetime datetime = '12:10:05.0000'
Msg 241, Level 16, State 1, Line 3
Conversion failed when converting date and/or time from character string.
You can add to a datetime2, but you must declare the datetime2 variable first instead of using a string literal:
declare #date datetime2 = '2021-11-01 05:10:00.0000000'
SELECT DATEADD(HOUR, 4, #date)
2021-11-01 09:10:00.0000000
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]
I am trying to convert varchar(14) to Decimal(14,2).
My code is something like
Declare #varchar_variable varchar(14), #decimal_variable decimal(14,2)
Set #varchar_variable = '189'
Select #decimal_variable = Convert(Decimal(14,2),#varchar_variable)
I am only getting Arithmetic overflow error message when the value of #varchar_variable is large (like '10000000000189')
Why am I getting this error message for larger values ? Are values like 10000000000189 out of range for decimal datatype ? If yes, what is the range of decimal datatype in SQL.
You need to define Decimal(16,2) to convert varchar(14) to decimal. You can alternatively use Cast instead of Convert.
Declare #varchar_variable varchar(14), #decimal_variable decimal(16,2)
Set #varchar_variable = '10000000000189'
Select #decimal_variable = CAST(#varchar_variable AS DECIMAL(16,2))
-- Select #decimal_variable = Convert(Decimal(16,2),#varchar_variable)
SELECT #decimal_variable
As mentioned in comments, Decimal(14,2) contains 14 digits in total.
Try increment your variable size;
Select Convert(Decimal(16,2),'10000000000189')
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)
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'