Click here to Skip to main content
15,914,371 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have database. It is having table which call UserProfile. It contain column birthday.
Each and everyday I want to find the birthdays of users
Eg User birthday
Saman 1990/08/23
Rajith 1987/05/08
Piumi 1989/08/23
kumara 1989/04/06


I want to find the users who are born on 23rd of August(08)

SQL
select *
from
UserProfile
where BirthDay like&'%/08/23'

But is is not working.
Posted
Updated 8-Sep-15 9:26am
v2
Comments
PIEBALDconsult 8-Sep-15 15:41pm    
LIKE operates on strings, and you should never store dates as strings. Store them as DATEs and use DATEPART instead.

If I understand your situation correctly I would recommend not using a character column for storing dates. Instead it should be defined as a date column.

When the column is a date column, it's easy to extract the day or the month. For example
SQL
SELECT *
FROM UserProfile
WHERE DATEPART(day, BirthDay) = 23
AND  DATEPART(month, BirthDay) = 8

But the key is to use proper data type for the column.

If you really can't change the data type, then you can use the LIKE example in solution 1 or use CONVERT function to convert the type from varchar to date. If the date format you use is YYYY/MM/DD then something like the following should work
SQL
SELECT *
FROM UserProfile
WHERE DATEPART(day, CONVERT(date, BirthDay, 111)) = 23
AND  DATEPART(month, CONVERT(date, BirthDay, 111)) = 8

But as said, using date column is the proper way to go.
 
Share this answer
 
You can do it, if you like, but you have to be aware of the date format. You need to convert the date to string in a specific format, than you can use LIKE operator.
SQL
select * from
(SELECT convert(varchar, getdate(), 111) as BirthDate) as T
where T.BirthDate like '%/09/08'


See date formats: https://anubhavg.wordpress.com/2009/06/11/how-to-format-datetime-date-in-sql-server-2005/[^]
In SQL-2012 you have even more at hands: https://msdn.microsoft.com/en-us/library/hh213505.aspx[^]
 
Share this answer
 
v2

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