Solved

take year from date and convert to a predefined month and date.

Posted on 2013-10-25
4
425 Views
Last Modified: 2013-10-28
is there a better syntax to do the below: a date is given.. it can be any date.. we have to ignore the date and month of that input, but just the year.. then we need the result set of predined April 12th to 16th of that year.

DECLARE @YEAR CHAR(4)
DECLARE @DateBegin CHAR(10)
DECLARE @DateEnd CHAR(10)
select @YEAR = CAST(YEAR('2011-11-19') as CHAR(4)) /*this could be any date */
select @DateBegin = @YEAR+'-04-12'
select @DateEnd = @YEAR+'-04-16'
select  'BETWEEN '''+ @DateBegin+''' AND ''' + @DateEnd +''''

then i have to use the above inside a query in the WHERE condition.
0
Comment
Question by:25112
  • 2
4 Comments
 
LVL 15

Expert Comment

by:Ess Kay
ID: 39601140
not sure what you are trying to do there, but if you are going to hardcode it, then hardcode the whole thing
0
 
LVL 65

Accepted Solution

by:
Jim Horn earned 500 total points
ID: 39601143
You've pretty much nailed it.  Give this a whirl..
Declare @dt date = '2012-05-31'

SELECT CAST(CAST(YEAR(@dt) as char(4)) + '-04-12' as date) as start_date
SELECT CAST(CAST(YEAR(@dt) as char(4)) + '-04-16' as date) as end_date

Open in new window

As an aside, keep in mind that using two dates in BETWEEN means that you're including both of them, and that those two days have a time component of midnight, so it wouldn't capture a value of say 2013-04-16 12:00:00.   SQL expert PortletPaul wrote an article on this.
0
 
LVL 65

Expert Comment

by:Jim Horn
ID: 39601152
... so, if you want to capture all 2013-04-16 values regardless of time, then instead of

WHERE SomeValue BETWEEN '2013-04-12' AND '2013-04-16'
--  it'll be
WHERE SomeValue >= '2013-04-12' AND SomeValue < '2013-04-17'
0
 
LVL 5

Author Comment

by:25112
ID: 39605640
good solution- thx.
0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Suggested Solutions

Hi all, It is important and often overlooked to understand “Database properties”. Often we see questions about "log files" or "where is the database" and one of the easiest ways to get general information about your database is to use “Database p…
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 …
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

776 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