martedì 19 febbraio 2013

T-SQL, Executing DOS Command

The seguente T-SQL Script shows how execute DOS Command:


declare  @temptb as table (id int identity, valore varchar(255))
insert into @temptb exec xp_cmdshell 'ipconfig'
select * from @temptb

My Two Cents ...

Getting SQL Server IP Address


If you need to know the IP of your SQL Server, the seguent scripts is what you need:


declare @cmdresults as table(ip varchar(255))
insert into @cmdresults exec xp_cmdshell'ipconfig | find "IPv4"'
select ltrim(rtrim(substring(ip,charindex(':',ip)+1,len(ip)))) from @cmdresults where ip is not null  

or


declare @cmdresults as table(ip varchar(255))
insert into @cmdresults exec xp_cmdshell'ipconfig | find "IP Address"'
select ltrim(rtrim(substring(ip,charindex(':',ip)+1,len(ip)))) from @cmdresults where ip is not null  

My Two Cents ... 

T-SQL, Row Value Concatenation Unsing "FOR XML PATH"

Sometime, could be usefull to concatenate row values.
For example you have a select that returns this result:


A
B
C
D


but you need:


A, B, C, D

the seguente script makes this: a Row Value Concatenation.


SELECT STUFF((
      SELECT
            ', ' + ltrim(rtrim(Your_Field))
      FROM
            Your_Table
      FOR XML PATH('')
),1,2,'')



My Two Cents ...

Time From Datetime



The following script shows how you can get time from datetime:

select CONVERT(char(8), getdate(), 108) as Time



My Two Cents...