Solved

SQL Server convert day into date format

Posted on 2014-07-31
7
301 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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
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

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.

Join & Write a Comment

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
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.
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.

747 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

12 Experts available now in Live!

Get 1:1 Help Now