Home > Link, Query, SQL > BISQL # 55 – Playing With Date type for getting Results as We Required

BISQL # 55 – Playing With Date type for getting Results as We Required

Hi friends

After a week I am blogging on SQL Server Blog.

I have already provided blogs on time as follows

BISQL # 3 :Time and Date Query code and Analysis Part –I

Hope I will be regular from onwards !!

In this article we will just see few conversation of date time .

Problem : I have given a Integer number I Just need to convert this in to current Date

Solution: There are n number of solution for this problem but I will explained shortest and longest one

1.Longest Solution

In this  I don’t know any of the Date time function where I just know Sting and integer Smile

Here is simple code

Declare  @MyDate datetime = GETDATE()
Declare @IntPart int = 15
Declare @Monthpart nvarchar(3) = (Datename(month,@MyDate))
Declare @Yearpart nvarchar(4) = (Datename(YEAR,@MyDate))
Declare @WString nvarchar(15)
= @Monthpart+ ' '+ Convert (nvarchar(2),@IntPart) +' ' + @Yearpart

PRINT @MyDate
PRINT @IntPart
PRINT @Monthpart
PRINT @Yearpart
PRINT @WString

Output of above code is

image

2.Shortest Solution

Its So simple just need to use DATEADD

Declare  @MyDate datetime = GETDATE()
Declare @IntPart int = 15
(select DATEADD(DD,@IntPart,@MyDate))

Output is as simple as above output

image

So here I just need to tell you the solution is always achievable even if you know datetime Function or don’t know it !!!

We will surely have series of post on time as data type !!

Hope this helps !!

If you really like reading my blog and understood at lest few thing then please don’t forget to subscribe my blog

If you wan daily link and analysis or interesting link go to following website which will give @ your inbox please subscribe our following link resource blog

Where todays links are

Dactylonomy of Web Resource >> Link Resource # 25: Sept 12–Sept 24

Advertisements
Categories: Link, Query, SQL
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: