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.

Friday, 3 June 2016

Java Program to Reverse an Integer Number without using String - Example

How ot revrse a integer number in Java

When I was asked "Can you write a program to reverse a number in Java" long long years back, I did a trick. I converted the number to String by just concatentating with empty String e.g. "" + 123 and then converted that Stirng to StringBuilder can caled the reverse() method.

That solved the problem but Interviewer was not satisfied. He said Ok, but then asked again, can you reverse a number without using anything String or StringBuilder and by building your logic with basic programming elements. I was confused, what do you mean by basic programming elements? He said, I can use loop, if block, operator, and other basic stuff which makes a programming language.

Saturday, 28 May 2016

How to Check if Given Number is Prime Number in Java

Before writing a progarm to check if a number is prime or not, let's first revise what is a prime number? In Maths, a number is said to be prime if it is not divisible by any number other than itself. You can now convert this definition to code and create your Java program to check if given number is prime or not.

Friday, 27 May 2016

How to Calculate Factorial in Java using Recursion and Loop

Java Program to calculate factorial in Java using Recursion
You can calculate factorial of a given number in Java program either by using recursion or loop. The factorial of a number is equal to number*fact(number-1), which means its a recursive algorithm and that's why it is often used to teach recursion to programmers in computer programming classes.

But, recursion is not always preffered in production code and that's why you also need to learn how to convert a recursive algorithm to an iterative algorithm i.e. algorithm which make use of various loop construct in Java programming language.

Here in this Java beginners tutorial, I'll teach you both ways to calcualte factorial in Java i.e. with and without recursion. Since recursive algorithm is very simple, we'll fisrt look at that and later we'll check out how to calculate factorial without recursion.

Thursday, 26 May 2016

How to print Pyramid Pattern of Numbers in Java

Write a Java Program to print the following Pyramid pattern of numbers:

1
12
123
1234
12345
1234
123
12
1

You can easily print above pattern using two loops and with the help of print() and println() method of PrintStream. You can access them using System.out object as System.out.print() and System.out.println().

But, in this program, I'll show you can print this patter by using just one loop instead of two.

Friday, 20 May 2016

10 Java Books Every Senior Developer Should Read

Apart form programming, one of my passion is to read books. I have hundreds of e-books on my smartphone and PC and tens of book on my self, especially related to Java. Since I have benefited a lot from the book, I am going to share my collection of Java related books, which I think every senior developer should read. By saying senior Java developer, I mean anyone doing programming in Java for more than 3 to 4 years. You don't need to buy these books, you can download PDF from Internet for some of them, or you can borrow it from your friends or library, but your must read these books. Sometime, I end up purchasing the book but never get chance to read it, that's what I hate. If you have book which looks like new even after one year of purchase, then you have not read it. This is why I also like to borrow books from friends and library. This put me some pressure to read at least couple of chapters before returning it :-)