Click here to Skip to main content
15,890,825 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
my code is...


SQL
Select em.Name, DATEADD(YY, DATEPART(YYYY, getdate()) - DATEPART(YYYY,Marriagedate), Marriagedate) AS Current_data, Marriagedate= DATEDIFF(YEAR,Marriagedate,GETDATE()) from EmployeeMaster em inner join EmpMarriageDate ed on em.Id=ed.EmployeeId
where
 CONVERT(datetime, CAST(MONTH(Marriagedate) AS VARCHAR)  +
						'/' + CAST(DAY(Marriagedate)AS VARCHAR) +
						'/' + CASE WHEN MONTH(Marriagedate) = 1 AND MONTH(GETDATE()) = 12 THEN CAST(YEAR(GETDATE()) + 1 AS VARCHAR) ELSE CAST(YEAR(GETDATE()) AS VARCHAR) END )
					BETWEEN GETDATE() AND DATEADD(dd, 7, GETDATE())


my output is look like 23-12-2015 00:00:00:000
But
i want output only 23-12-2015

please help me solve this question.
Thanks
Posted
Updated 23-Dec-15 0:21am
v2

You do the old and common mistake of all...
What you see is only a visual representation of the value stored in the DB...
So to change it you first have to decide where do you want to display it...
to taste it run these lines:
C#
Debug.WriteLine(DateTime.Now.ToString("F"));
Debug.WriteLine(DateTime.Now.ToString("ddd, MMM yyyy"));
Debug.WriteLine(DateTime.Now.ToString("dd/MM/yyyy"));
 
Share this answer
 
Based on the other question you posted ...
I commented there that you should only format a date when you are presenting it to the user in the GUI layer - this is what Kornfeld Eliyahu Peter is presenting in Solution 1.

However your other question seemed to imply that you were trying to find rows where the next marriage anniversary was within the next 7 days.
The way you are formatting the data to do the WHERE clause is messy and unnecessary.

Consider using something like this instead:
SQL
Select 
	em.Name, 
	DATEADD(YY, DATEPART(YYYY, getdate()) - DATEPART(YYYY,Marriagedate), Marriagedate) AS Current_data, 
	DATEADD(YY, DATEPART(YYYY, getdate()) - DATEPART(YYYY,Marriagedate) + 1, Marriagedate) AS Next_Anniv, 
	HowLongMarried = DATEDIFF(YEAR,Marriagedate,GETDATE()) 
from 
	EmployeeMaster em 
	inner join EmpMarriageDate ed on em.Id=ed.EmployeeId
where 
	DATEADD(YY, DATEPART(YYYY, getdate()) - DATEPART(YYYY,Marriagedate), Marriagedate)
		BETWEEN GETDATE() AND DATEADD(dd, 7, GETDATE())	-- next anniversary is this year
OR
	DATEADD(YY, DATEPART(YYYY, getdate()) - DATEPART(YYYY,Marriagedate) + 1, Marriagedate)
		BETWEEN GETDATE() AND DATEADD(dd, 7, GETDATE())	-- next anniversary is next year

You can make it even clearer by using a CTE ...
SQL
with BaseData AS
(
	Select 
	em.Name, 
	DATEADD(YY, DATEPART(YYYY, getdate()) - DATEPART(YYYY,Marriagedate), Marriagedate) AS Current_data, 
	DATEADD(YY, DATEPART(YYYY, getdate()) - DATEPART(YYYY,Marriagedate) + 1, Marriagedate) AS Next_Anniv, 
	HowLongMarried = DATEDIFF(YEAR,Marriagedate,GETDATE()) 
from 
	EmployeeMaster em 
	inner join EmpMarriageDate ed on em.Id=ed.EmployeeId
)
SELECT [Name], Current_data, HowLongMarried
FROM BaseData
where 
	Current_data BETWEEN GETDATE() AND DATEADD(dd, 7, GETDATE())	-- next anniversary is this year
	OR
	Next_Anniv BETWEEN GETDATE() AND DATEADD(dd, 7, GETDATE())	-- next anniversary is next year


Note that if Marriagedate column has been defined as a DateTime rather than Date you can cast the results to Date
SQL
CAST(Current_data as date)
 
Share this answer
 
v2
Comments
Harish-Sangwan 24-Dec-15 4:55am    
My Output still same.
output is : Current_data
2015-12-28 00:00:00.000
But i want (dd/mm/yyyy) this type format.

And Date will be show look like:
2015-12-28
CHill60 24-Dec-15 5:22am    
You should only format the date in the GUI layer - when your user can see it. Are you saying that you are displaying the output in SSMS to your user?
CHill60 24-Dec-15 5:28am    
I've updated my solution
Harish-Sangwan 28-Dec-15 7:47am    
Thank you very much.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900