Monday, March 26, 2012

Is there a function like SUBSTRING but for integers?

I'm wondering if there is a function in SQL that works like SUBSTRING function but for integers. Like for example if I have a number like 20010112 and I want to cut it to the first for digits so that it reads 2001?

You use LEFT function.

SELECT LEFT(20010112, 4)

You should get 2001

This function works on string too.

|||this left function is for string. if you pass in a integer, the integer value is implicit convert to string
so 20010112 is converted to '20010112'.

You can convert the integer to string before using substring

substring(convert(varchar(10), @.intvar), 5, 2)

No comments:

Post a Comment