Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Check Month year if exists

Posted on 2015-02-11
5
113 Views
Last Modified: 2015-02-17
I have a column called Recevieddate (datetime). I have a string '2/1/2015'. I want to check if month and year exists in Recevieddate when I pass @mydate. Date does not matter.


Sample date

2015-02-01 00:00:00.000
2015-02-06 00:00:00.000
2015-03-05 00:00:00.000
2015-04-15 00:00:00.000



declare @mydate varchar(12)
set@mydate = '2/1/2015'
declare @tf    char(1)


if exists(select *from table1 where receiveddate = @mydate ???)
  begin
    set @tf = 'True'
  end
  else
  begin
    set @tf = 'false
  end


Sample
2/1/2015 should return true because month year exists in Recevieddate
2/15/2015 should return true
0
Comment
Question by:VBdotnet2005
5 Comments
 
LVL 65

Accepted Solution

by:
Jim Horn earned 125 total points
ID: 40604054
What's the purpose of setting @mydate to a varchar, when it contains a date value?
Also, you have @tf char(1), but 'True' and 'False' are more than one character.
Declare @mydate date = '2015-02-01', @tf varchar(10)

IF EXISTS (SELECT receiveddate FROM Table1 WHERE MONTH(receiveddate) = MONTH(@mydate) AND YEAR(receiveddate) = YEAR(@mydate))
   SET @tf = 'True'
ELSE
   SET @tf = 'False'

Open in new window

0
 
LVL 34

Assisted Solution

by:Mike Eghtebas
Mike Eghtebas earned 125 total points
ID: 40604065
if exists (SELECT [receiveddate] FROM Table1 WHERE COALESCE([receiveddate], '19000101') = COALESCE(@mydate, '19000101'))

First part deals with null dates (is not received, has no date yet:

Is null:            returns '19000101'
Is not null:     returns '2/6/2015'   ' for example

The second part deals with the date you supply:

Is null:            returns '19000101'   'highly unlikely
Is not null:     returns '2/10/2015'   ' for example

Now, if they match, record still shows (date exists).
0
 
LVL 35

Assisted Solution

by:David Todd
David Todd earned 125 total points
ID: 40604093
Declare @mydate date = '2015-02-01', @tf varchar(10)

IF EXISTS (SELECT 1FROM Table1 WHERE dateadd( month, datediff( month, receiveddate, 0 ), 0 ) = @mydate
   SET @tf = 'True'
ELSE
   SET @tf = 'False'

Open in new window

0
 
LVL 11

Assisted Solution

by:John_Vidmar
John_Vidmar earned 125 total points
ID: 40604527
If you have a high-volume table then you do not want to wrap a table-field in a function, you'd lose the ability to use an index (if one existed on that table-field). Instead, the following solution truncates the date-variable to the first-of-the-month (greater than or equal to), and first-of-the-next-month (less than):
IF EXISTS
(	SELECT	*
	FROM	table1
	WHERE	receiveddate >= REPLACE(STR(MONTH(@mydate)) + '/01/' + STR(YEAR(@mydate)), ' ','')
	AND	receiveddate <  DATEADD(mm,1,REPLACE(STR(MONTH(@mydate)) + '/01/' + STR(YEAR(@mydate)), ' ',''))
)
	BEGIN
		SET @tf = 'True'
	END
ELSE
	BEGIN
		SET @tf = 'False'
	END

Open in new window

0
 
LVL 48

Expert Comment

by:Vitor Montalvão
ID: 40613702
vbdotnet2005, do you still need help with this question?
0

Featured Post

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

856 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