Feed on
Posts
Comments

Archive for the ‘SQL DateTime’ Category

A year ago I wrote post about SQL SERVER - Retrieve - Select Only Date Part From DateTime - Best Practice where I have discussed two different methods of getting datepart from datetime.
Method 1:
SELECT DATEADD(D, 0, DATEDIFF(D, 0, GETDATE()))
Method 2:
SELECT CONVERT(VARCHAR(10),GETDATE(),111)
I have summarized my post suggesting that either method works fine and I prefer to [...]

Read Full Post »

Download SQL Server 2008 Interview Questions and Answers Complete List
Interview is very important event for any person. A good interview leads to good career if candidate is willing to learn. I always enjoy interview questions and answers series. This is my very humble attempt to write SQL Server 2008 interview questions and answers. SQL Server [...]

Read Full Post »

SQL SERVER - 2008 - Interview Questions and Answers Complete List Download
3) Questions of SQL SERVER 2008
What are the basic functions for master, msdb, model, tempdb and resource databases?
The master database holds information for all databases located on the SQL Server instance and is theglue that holds the engine together. Because SQL Server cannot start [...]

Read Full Post »

Recently I have recieved email from Vivek Jamwal, which contains many useful SQL Server Date functions.
—-Today
SELECT GETDATE() ‘Today’
—-Yesterday
SELECT DATEADD(d,-1,GETDATE()) ‘Yesterday’
—-First Day of Current Week
SELECT DATEADD(wk,DATEDIFF(wk,0,GETDATE()),0) ‘First Day of Current Week’
—-Last Day of Current Week
SELECT DATEADD(wk,DATEDIFF(wk,0,GETDATE()),6) ‘Last Day of Current Week’
—-First Day of Last Week
SELECT DATEADD(wk,DATEDIFF(wk,7,GETDATE()),0) ‘First Day of Last Week’
—-Last Day of Last Week
SELECT DATEADD(wk,DATEDIFF(wk,7,GETDATE()),6) ‘Last Day of Last Week’
—-First Day of Current Month
SELECT DATEADD(mm,DATEDIFF(mm,0,GETDATE()),0) ‘First Day of Current Month’
—-Last Day of Current Month
SELECT DATEADD(ms,- 3,DATEADD(mm,0,DATEADD(mm,DATEDIFF(mm,0,GETDATE())+1,0))) ‘Last Day of Current Month’
—-First Day of Last Month
SELECT DATEADD(mm,-1,DATEADD(mm,DATEDIFF(mm,0,GETDATE()),0)) ‘First Day of Last Month’
—-Last Day of Last Month
SELECT DATEADD(ms,-3,DATEADD(mm,0,DATEADD(mm,DATEDIFF(mm,0,GETDATE()),0))) ‘Last Day of Last Month’
—-First Day of Current Year
SELECT DATEADD(yy,DATEDIFF(yy,0,GETDATE()),0) ‘First Day of Current Year’
—-Last Day of Current Year
SELECT DATEADD(ms,-3,DATEADD(yy,0,DATEADD(yy,DATEDIFF(yy,0,GETDATE())+1,0))) ‘Last Day of Current Year’
—-First Day of Last Year
SELECT DATEADD(yy,-1,DATEADD(yy,DATEDIFF(yy,0,GETDATE()),0)) ‘First Day of Last Year’
—-Last Day of Last Year
SELECT DATEADD(ms,-3,DATEADD(yy,0,DATEADD(yy,DATEDIFF(yy,0,GETDATE()),0))) ‘Last Day of Last Year’
ResultSet:
Today
———————–
2008-08-29 21:54:58.967
Yesterday
———————–
2008-08-28 21:54:58.967
First Day of Current Week
————————-
2008-08-25 00:00:00.000
Last Day [...]

Read Full Post »

One of the reader Nanda of SQLAuthority.com has posted very detailed script of converting any date time in desired format. I suggest every reader of this blog to save this script in your permanent code bookmark and use it when you need it.
Refer the function and get familiar yourself with different format this function support. [...]

Read Full Post »

How to get current system date time in SQL Server?
First thing which comes to many users is using following script.
SELECT GETDATE() AS CurrentDateTime
Above method is not the only method to retrieve the current system date time for SQL Server. SQL Server 2008 has many different function which provides current system date time in different format [...]

Read Full Post »

If you want to find current datetime in SQL Server I suggest to read the following post :
SQL SERVER - Retrieve Current Date Time in SQL Server CURRENT_TIMESTAMP, GETDATE(), {fn NOW()}
This post is related to new feature available in SQL Server 2008. In SQL Server 2008 there is a function which provides current offset of [...]

Read Full Post »

I have been playing with SQL Server 2008 recently. There are many new features which SQL Server 2008 have. One of the interesting addition to SQL Server 2008 is system table field which records when SQL Server was started. This field has data type as datetime that is why it is precise to 3 milisecond.
Note [...]

Read Full Post »

I have been asked many times when there is DATENAME function available why do I go in exercise of writing UDF For the getting the day of the week. Answer is : I just like it!
SELECT DATENAME(dw, GETDATE())

Reference : Pinal Dave (http://www.SQLAuthority.com)

Read Full Post »

Datetime functions and stored procedures always interests me. Nanda Kumar has suggested modification to previous written article about SQL SERVER - SQL SERVER - UDF - Get the Day of the Week Function - Part 2. He has improved on UDF.
CREATE FUNCTION dbo.udf_DayOfWeek(@dtDate DATETIME)
RETURNS VARCHAR(10)
            AS
    BEGIN
    DECLARE @rtDayofWeek VARCHAR(10)
    DECLARE @weekDay INT
        —-Here I have subtracted 7 For keeping Sunday as the First day like wise for Monday we need to subtract 2 and so on
        SET @weekDay=((DATEPART(dw,@dtDate)+@@DATEFIRST-7)%7)
    SELECT @rtDayofWeek = CASE @weekDay
                    WHEN 1 THEN ’Sunday’
                    WHEN 2 THEN ’Monday’
                    WHEN 3 THEN ’Tuesday’
                    WHEN 4 THEN ’Wednesday’
                    WHEN 5 THEN ’Thursday’
                    WHEN 6 THEN ’Friday’
                    WHEN 0 THEN ’Saturday’
        END
    RETURN (@rtDayofWeek)
    END
GO
SELECT dbo.udf_dayofweek(GETDATE())

Reference : Pinal Dave (http://www.SQLAuthority.com), Nanda Kumar

Read Full Post »

I have written article about SQL SERVER - UDF - Get the Day of the Week Function. I have received good modified script from reader Mihir Popat has suggested another code where Sunday does not have to be necessary the first day of the week.
CREATE FUNCTION dbo.udf_DayOfWeek(@dtDate DATETIME)
RETURNS VARCHAR(10)
            AS
    BEGIN
    DECLARE @rtDayofWeek VARCHAR(10)
    DECLARE @weekDay INT
        – Here I have subtracted 7 For keeping Sunday as the First day
        – like wise for Monday we need to subtract 2 and so on
        SET @weekDay = ((DATEPART(dw,GETDATE())+@@DATEFIRST-7)%7)
    SELECT @rtDayofWeek = CASE @weekDay
                    WHEN 1 THEN ’Sunday’
                    WHEN 2 THEN ’Monday’
                    WHEN 3 THEN ’Tuesday’
                    WHEN 4 THEN ’Wednesday’
                    WHEN 5 THEN ’Thursday’
                    WHEN 6 THEN ’Friday’
                    WHEN 7 THEN ’Saturday’
        END
    RETURN (@rtDayofWeek)
    END
GO
SELECT dbo.udf_DayOfWeek(GETDATE())

Reference : Pinal Dave (http://www.SQLAuthority.com), Mihir Popat

Read Full Post »

I am very pleased to write my 500th post. After 500 posts, I still have same feeling when I wrote first post on this blog. I would like to thank my family for their continuous support in writing this blog. Most of all I want to thank all of YOU for being wonderful readers of [...]

Read Full Post »

One of our project manager asked me why SQL Server does not have only DATE or TIME datatypes? I thought his question is very valid, he is not DBA however he understands the RDBMS concepts very well. I find his question very interesting. I told him that there are ways to do that in SQL [...]

Read Full Post »

SQL SERVER - Cursor to Kill All Process in Database
SQL SERVER - Find Stored Procedure Related to Table in Database - Search in All Stored procedure
SQL SERVER - Shrinking Truncate Log File - Log Full
SQL SERVER - Simple Example of Cursor
SQL SERVER - UDF - Function to Convert Text String to Title Case - Proper [...]

Read Full Post »

While doing surprise code review of Jr. DBA I found interesting syntax DATEFORMAT. This keywords is very less used as CONVERT and CAST can do much more than this command. It is still interesting to learn about learn about this new syntax.
Sets the order of the dateparts (month/day/year) for entering datetime or smalldatetime data. This [...]

Read Full Post »

Following User Defined Function (UDF) returns the numbers of days in month. It is very simple yet very powerful and full proof UDF.
CREATE FUNCTION [dbo].[udf_GetNumDaysInMonth] ( @myDateTime DATETIME )
RETURNS INT
      AS
  BEGIN
  DECLARE @rtDate INT
    SET @rtDate = CASE WHEN MONTH(@myDateTime)
          IN (1, 3, 5, 7, 8, 10, 12) THEN 31
          WHEN MONTH(@myDateTime) IN (4, 6, 9, 11) THEN 30
    ELSE CASE WHEN (YEAR(@myDateTime) % 4 = 0 
            AND
              YEAR(@myDateTime) % 100 != 0) 
          OR
        (YEAR(@myDateTime) % 400 = 0)
            THEN 29
      ELSE 28 END
    END
  RETURN @rtDate
  END
GO

Run following script in Query Editor:
SELECT dbo.udf_GetNumDaysInMonth(GETDATE()) NumDaysInMonth
GO

ResultSet:
NumDaysInMonth
———————–
31
Reference : Pinal Dave (http://www.SQLAuthority.com)

Read Full Post »

While writing few articles about SQL Server DataTime I accidentally wrote User Defined Function (UDF), which I would have not wrote usually. Once I wrote this function, I did not find it very interesting and decided to discard it. However, I suddenly noticed use of Non-Deterministic function in the UDF.
I always thought that use of [...]

Read Full Post »

Very Simple Script which find Monday of the Current Week
SELECT DATEADD(wk, DATEDIFF(wk,0,GETDATE()), 0) MondayOfCurrentWeek

Reference :Pinal Dave (http://www.SQLAuthority.com)

Read Full Post »

Few questions are always popular. They keep on coming up through email, comments or from co-workers. Finding Last Day of Any Month is similar question. I have received it many times and I enjoy answering it as well.
I have answered this question twice before here:
SQL SERVER - Script/Function to Find Last Day of Month
SQL SERVER [...]

Read Full Post »

While reading ColdFusion blog of Ben Nadel Getting the Previous Day In ColdFusion, Excluding Saturday And Sunday, I realize that I use similar function on my SQL Server Database. This function excludes the Weekends (Saturday and Sunday), and it gets previous as well as next work day.
Script to create function to get previous and next [...]

Read Full Post »

Older Posts »