Showing posts with label SQL. Show all posts
Showing posts with label SQL. Show all posts

Get date of birth in T SQL


There are many ways to find the date of birth using T SQL; complicated and simple. Here is a simple example to find the date of birth without using much resources


SELECT FLOOR(DATEDIFF(DAY, @BirthDate, GETDATE()) / 365.25)


This will give you the age as of now. Its very accurate and also will take care of leap year, since we are considering a year as 365.25 days.


Kindly pot your suggestions or ideas as comments, so that others can make use of your ideas...

Pad Left - T SQL Query

I some cases, we will come across a situation where we think how easy will haven been our life, if they had included the PADLEFT() feature in SQL. Don't worry, we can achieve that functionality easily by a small query.

First Approach:


SELECT REPLICATE('0', 10-LEN(CAST(1234 AS VARCHAR))) + CAST(1234 AS VARCHAR)

The result will be: 0000001234

Second Approach: 

SELECT RIGHT('0000000000' + CAST(1234 AS VARCHAR), 10)

This will  also give the same result as above: 0000001234

Enjoy...

[Please feel free to post comments on this article. Or you can ask for more. I will be happy helping you]