Serge's Technology View

Talk about Technologies, Software Architecture and Management

Archive for the ‘Fun stuff with SQL Server’ Category

File management from inside SQL code

Writing a log, dumping data, looking for file to be used in Bulk insert…

There are many situations when you would want to have access to files from inside your SQL code on Microsoft SQL Server.

Did you know that you actually can do this? No? Check below for code snippets to perform various operations on files. It is presented in form of the functions but you are actually not limited to that

Prerequisites and assumptions

  1. Your script should have sufficient rights to perform required access to files (not necessarily local).
  2. Scripting.FileSystemObject should be present at your SQL Server location and accessible.

Check if file exists
(more…)

SQL date operations: First day, Last day of the month…

Did you ever faced a situation when you need quickly calculate some boundaries of the Date value like First Day of the month, Last Day of the month, etc… in T-SQL?

Colegue of mine asked me this question… WOW! What do you know…
It is actually very easy if we think about it for a second…

So lets fun begin…

--First Day of the Current Month:
select DATEADD(mm, DATEDIFF(mm, 0, GETDATE()), 0)

-- Last Day of the Current Month:
select DATEADD(ms, -3, DATEADD(mm, DATEDIFF(m, 0, GETDATE()) + 1, 0))

-- Last Day of Prior Month:
select DATEADD(ms, -3, DATEADD(mm, DATEDIFF(mm, 0, GETDATE()), 0))

-- First Day of the Current Year:
select DATEADD(yy, DATEDIFF(yy, 0, GETDATE()), 0)

-- Last Day of the Current Year:
select DATEADD(ms, -3, DATEADD(yy, DATEDIFF(yy, 0, GETDATE()) + 1, 0))

-- Last Day of Prior Year:
select DATEADD(ms, -3, DATEADD(yy, DATEDIFF(yy, 0, GETDATE()), 0))

And some more…

-- First Day of the Quarter:
select DATEADD(qq, DATEDIFF(qq, 0, GETDATE()), 0)

-- Monday of the Current Week:
select DATEADD(wk, DATEDIFF(wk, 0, GETDATE()), 0)

-- Midnight for the Current Day:
select DATEADD(dd, DATEDIFF(dd, 0, GETDATE()), 0)

-- First Monday of the Month:
select DATEADD(wk, DATEDIFF(wk, 0, DATEADD(dd, 6 - DATEPART(day, GETDATE()), GETDATE())), 0)

-- Last 2 full months
select DATEADD(ms, -3, DATEADD(mm, DATEDIFF(mm, 0, GETDATE()) - 2, 0))

Fun stuff with SQL Server

Over past several years Microsoft SQL Server became primary database platform for me when it comes to Windows Development. Why am I so specific? When it comes to Web development and it is not ASP.Net related, then MySQL is THE engine for me. Just to be fair, I should also mention Interbase, DB2, PostgressSQL and Oracle, but this is different story.

So… What have I done with SQL Server? Well… Almost everything - installations, upgrades, migrations, localization support, automated synchronizations between local and remote servers in linked and disconnected mode, performance optimization, data tuning, …, you name it…

What I found is that there is a lot of information and many topics are covered on MSDN, Support Forums and in On-line help, but not all are well described and/or fully covered in one place.

As a result I have decided to present/discuss some topics I come across in this blog. I do not pretend to be a “know-it-all”, so if you have some additional info on any topic, please feel free to comment.

Valid XHTML 1.0 Transitional  Valid CSS!