Solved

SQL Server convert day into date format

Posted on 2014-07-31
7
305 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 142

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 14

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 142

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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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:ScottPletcher
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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
I have a large data set and a SSIS package. How can I load this file in multi threading?
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

919 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now