Solved

SQL Server convert day into date format

Posted on 2014-07-31
7
318 Views
Last Modified: 2014-07-31
Hello Experts Exchange
I have a field in SQL Server 2012 that just has the day of the month in it, I need to convert the day into a full date.

For example the field would have 31 and I want it to become 31/07/2014.

Is there a SQL script that can do this?

Thank you for reading.

Regards

SQLSearcher
0
Comment
Question by:SQLSearcher
7 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 40231178
the question to ask is if you have a value of 31, and are in the month of Feb, April etc where there is no such day: what should happen?
0
 
LVL 15

Expert Comment

by:Vikas Garg
ID: 40231181
Hi,

My friend if the field contains 31 then there are total 8 months which has 31 Days so what should be the logic to pick which month to display for 31 .
Also have to set the same for 30, 28 , 29 (Feb in Leap year)
0
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 40231185
a simple started (without looking at the above question) would be this:;
declare @dayofmonth int
set @dayofmonth = 31

select dateadd(day, @dayofmonth - datepart(day, getdate()), cast(getdate() as date))

Open in new window

0
Edgartown IT Case Study

Learn about Edgartown's quest to ensure the safety and security of the entire town's employee and citizen data. Read the case study!

 
LVL 65

Expert Comment

by:Jim Horn
ID: 40231458
>For example the field would have 31 and I want it to become 31/07/2014.
What's the logic for the 7 and 2014? If this is the current month/year, then SQL 2012 introduces EOMONTH, which returns the last day of the month for any given date. EOMONTH
0
 
LVL 48

Expert Comment

by:PortletPaul
ID: 40231531
Turing only 31, without month and year, into a date will be an utter guess
Presumably there are values of 1 through 31, and turning any of those would be an utter guess too.

I would ask if you know of some inherent logic behind this curious field?

Is there anything else in a row to give us a clue on  month and year?
(perhaps you could provide the column headings and a few rows of this data to look at?)
0
 
LVL 69

Expert Comment

by:Scott Pletcher
ID: 40231722
--code to get "day_number" day of the current month

SELECT DATEADD(DAY, day_number - 1, DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0))
FROM (
    SELECT 31 AS day_number UNION ALL
    SELECT 17
) AS test_data
0
 

Author Closing Comment

by:SQLSearcher
ID: 40231927
Thank you for your help.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Rebooting Witness SQL Server 2 23
query analyser in sql server 2016 express edition 2 24
Linked Server - SP with Param to VIew 7 19
SQL Syntax 6 26
In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Viewers will learn how the fundamental information of how to create a table.

679 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question