Scripting out Stored Procedures and Functions using Query

One of my friends asked me if there is an way to script out the definitions of stored Procedures and Functions using Query. There can be several methods. This is one of the methods that uses BCP utility

All you have to do is replace DBname by the actual Database Name

EXEC master..xp_cmdshell 
'bcp "Select routine_definition from 
order by routine_name" queryout "C:\scripts.sql" -c'

After it runs successfully, the file C:\scripts.sql will have the scripts

Note that due to security issues, xp_cmdshell is disable by default. You need to use sp_configure to enable it. You can find more information in this


Question of the month February 2018 – Find query that gives different result

In SQL Server server, consider the following queries

query 1 : select 12a4;
query 2 : select 12e4;
query 3 : select 12f4;
query 4 : select 12g4;

Only one query will produce different result than others. Find the odd one out

Happy holidays from T-SQL

Curious to know the result of the following query? All you need to do is, in SQL Server, goto SSMS, set the result mode to Text (Press CTRL+T) and then execute the following code

set nocount on select space(17-len(replicate(
char(135),no)))+ replicate(char(135),no*2-1)
from (select top 10 row_number() over (
order by (select 1)) as no from 
(select 0 as no union all select 0 union 
all select 0) as t1 
cross join (select 0 as no union 
all select 0 union all 
select 0) as t2) as t union all 
select space(14)+replicate(char(124),5) union 
all select space(10)+
as varchar(100))

MySQL – Functions to get current date and time

You may often need to do some data filters based on the current date and time. In MySQL you can use the system functions available to get current date and time.

Refer the following examples.

1 Get current date



SELECT CURDATE() AS today_date

The result is


2 Get current time



SELECT CURTIME() AS today_time

The result is


3 Get Current Date and time

SELECT NOW() AS today_date_time

The result is


MySQL – How to create random number with fixed length?

One of my friends asked me “How do I create a 10 digit random number?”

MySQL supports RAND() system function which can produce a random number between 0 and 1 with 16 digits as the decimal part.

An example for a random value can be obtained using

SELECT RAND() as random_number;

The result is


Note that every time you run this code, you will get a different number.

Now to get 10 digit whole number, multiple it by 10000000000 and omit the decimal part by converting the result into UNSIGNED number

SELECT CAST(rand()*10000000000 as UNSIGNED) as random_number

The result is


This can be modified easily to get other number with fixed digits. For example if you want a random 4 digit number, you can use

SELECT CAST(rand()*10000 as UNSIGNED) as random_number

The result is


You can parameterize this to work for any number of fixed digits you want

MySQL – Simple ways to know version number with @@version and version()

If you want to know the version number of MySQL server, there are two simple methods available.

You can use system variable @@VERSION as shown below

SELECT @@VERSION as mysql_version;

Other method is to use system function VERSION()

SELECT VERSION() as mysql_version

Both of them returns the following result


Depends on the version of the server, you may get the different version number

Note : @@VERSION is already available in SQL Server database engine as a global variable to get the version information