Monday 17 January 2022

How to convert YYYYMMDD INT to DATETIME in SQL Server? Example

 You can convert it directly with the CONVERT function by using the style 112 = ISO = yyyymmdd:

DECLARE @date int;

SET @date = 20220701



SELECT CONVERT(datetime, CONVERT(varchar(8), @date), 112)

SELECT CONVERT(datetime, '20070701', 112)

You can get this error Arithmetic overflow error converting expression to data type datetime.
that error came because we were comparing a datetime with int without converting later to datetime.

like
X.PriceDt < A.PrimaryDate

this will fix this
X.PriceDt < CONVERT(datetime, CONVERT(varchar(8), A.PrimaryDate), 112))


How to solve Arithmetic overflow error converting expression to data type datetime


Arithmetic overflow error converting expression to data type datetime.

SELECT CONVERT(datetime, 23232322, 112)


Arithmetic overflow error converting expression to data type datetime.

SELECT CONVERT(datetime, '23232322', 112)

 
The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.

SELECT CONVERT(datetime, '', 112)
1900-01-01 00:00:00.000


SELECT CONVERT(datetime, NULL, 112)
NULL


SELECT CONVERT(datetime, '-9999', 112)

The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.

"Arithmetic overflow error converting expression to data type datetime." also come when we try to convert a integer thinking its string

SELECT * from TABLE where DATE >= 20140101 < 20150101


here I have forgotten single quotes which results in this error.


Saturday 15 January 2022

How to compare float and double values in Java? Example Tutorial

If you have been doing Java programming then you may know that the use of == operator is not the correct way to compare floating point values in Java. If you use equality operator to compare float and double variables then it can cause an endless loop in Java, but is there a way to prevent that loop from running infinitely? Yes, instead of using == operator, you can use relational operator e.g. less than (<) or greater than (>) to compare float and double values.