-1

What I want is the EmployeeName from the emp_mst table with some condition which is given below:-

  1. All EmployeeName for last 7 months from the current date and also less 15 days.

from the below query I am getting the result for the last month, but I want this for the last 6 months

select DATEADD(month, -1, GETDATE()- 15)

Version image

I am using sql server 2008

UPDATED PROCEDURE

SELECT * FROM (SELECT CASE 
                 WHEN (SELECT Isnull(Sum(total_day), 0) 
                       FROM   xxacl_erp_ab_pl_count_view 
                       WHERE  emp_card_no = em.emp_card_no) > 7 THEN 
                 'DOC Exteded By 1 month. Reason:- Taken leave=' 
                 + CONVERT(VARCHAR, (SELECT Sum(total_day) FROM 
                 xxacl_erp_ab_pl_count_view 
                 WHERE emp_card_no = em.emp_card_no)) 
                 + 
' which is > 7. Actual DOC=' 
          + CONVERT(VARCHAR, Dateadd(mm, em.probation_period, em.date_of_joining), 103) 
          + '' 
          ELSE 'N/A' 
        END Remark, 
        em.* 
 FROM   emp_mst em 
        LEFT JOIN company_mst comp 
               ON em.comp_mkey = comp.mkey 
                  AND comp.fa_year = 2008 
                  AND company_name NOT LIKE '%HELIK%' 
 WHERE  em.status IN ( 'A' ) --and em.emp_type='E' 
        AND em.emp_card_no != 9999 
        AND em.resig_date IS NULL 
        AND CONVERT(DATETIME, em.date_of_joining, 103) >= 
            CONVERT(DATETIME, 
            Dateadd(m, -6, Getdate()), 103) 
        AND em.emp_card_no NOT IN (SELECT emp_card_no 
                                   FROM   p_emp_confirmation_hdr 
                                   WHERE  delete_flag = 'N' 
                                          AND hr_flag = 'Y')) pp 
WHERE  remark = 'N/A' 
Nad
  • 4,605
  • 11
  • 71
  • 160
  • could you include some data and expected output ? – t-clausen.dk Jun 24 '15 at 10:32
  • @t-clausen.dk: I want the list of `employees` who joined the company `six months` back. And also one more condition is, if they Join on 15th or before 15 they will be coming in current month confirmation otherwise next month confirmation. Do let me know if you need anything else – Nad Jun 24 '15 at 10:39
  • so basically beween 6 months ago and 15 days ago ? – t-clausen.dk Jun 24 '15 at 10:40
  • @t-clausen.dk: aah yes exactly. – Nad Jun 24 '15 at 10:41
  • @t-clausen.dk: See my edited question – Nad Jun 24 '15 at 10:53
  • try this [compatibility mode link](http://stackoverflow.com/questions/1501596/how-to-check-sql-server-database-compatibility-after-sp-dbcmptlevel-is-deprecate) – t-clausen.dk Jun 24 '15 at 10:56
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/81395/discussion-between-nad-and-t-clausen-dk). – Nad Jun 24 '15 at 11:13

2 Answers2

3

Casting to date to avoid calculating with timestamps

WHERE
  yourdate >= dateadd(m, -6, datediff(d, 15, getdate())) and 
  yourdate <  dateadd(d, -15, datediff(d, 0, getdate()))

Changed answer to adjust for you using sqlserver 2005 or older

Added 15 days extra to the interval

t-clausen.dk
  • 43,517
  • 12
  • 59
  • 92
  • getting error as `Msg 243, Level 16, State 1, Line 1 Type date is not a defined system type. Msg 243, Level 16, State 1, Line 1 Type date is not a defined system type.` – Nad Jun 24 '15 at 10:47
  • The `date` type was added in SQL Server 2008 (which is what you are using according to your original question). Are you sure you're not using SQL Server 2005? – Ed B Jun 24 '15 at 10:51
  • @EdB: Yes , i crossed check that – Nad Jun 24 '15 at 10:51
  • I like this date conversion for 2005 better than the version I used. It's cleaner. – Chris Schubert Jun 24 '15 at 10:53
  • @ChrisSchubert: yes this one is much better, btw, i am getting the same result from both the query. I want that in the procedure to happen, See my updated question for procedure – Nad Jun 24 '15 at 10:55
  • @nad yes functionally they are the same and will return the same result, 100% of the time, but this one would be more maintainable in the long run. – Chris Schubert Jun 24 '15 at 10:59
  • @nad; your screenshot shows management studio is from SQL Server 2008, but if the `date` type doesn't exist, I suspect your server is actually SQL Server 2005. You can check this by right-clicking the server in the object explorer and checking the Version; 9 is 2005, 10 is 2008, 10.5 is 2008R2, etc. – Ed B Jun 24 '15 at 11:01
  • @t-clausen: A bit change, i want all the employee name between `Nov 16 - Dec 15` means 6 months and 15 days gap. but i am not getting that employee – Nad Jun 24 '15 at 11:05
  • when I ran your first statement `dateadd(m, -6, datediff(d, 15, getdate()))`. I am not getting result as `Nov 16` – Nad Jun 24 '15 at 11:19
  • @nad Nov 16 is more than 6 months and 15 days ago – t-clausen.dk Jun 24 '15 at 11:27
  • @t-clausen.dk: So how much is that, I want that result. May be it is 7 months – Nad Jun 24 '15 at 11:29
  • Nov 16 is now 7 months and 8 days ago. But that is not the question you asked originally – t-clausen.dk Jun 24 '15 at 11:36
  • @t-clausen.dk: I know, i will change it, I tried with this `dateadd(m, -7, datediff(d, 8, getdate()))`. I got `Nov 16` but how to get `Dec 15` with second one. Please suggest that – Nad Jun 24 '15 at 11:37
  • @t-clausen.dk: Also one more thing. here it is hardcorded. I want this dynamically to be done. on 15 of every month, every employee will be inititated that whose 6 months has been completed. Please suggest – Nad Jun 24 '15 at 12:07
1
SELECT [emp_name] 
FROM [TABLE] 
WHERE [DateColumn] BETWEEN DATEADD(MONTH, -6, CAST(GETDATE() AS DATE)) 
    AND DATEADD(DAY, -15, CAST(GETDATE() AS DATE))

This will show you employees who were added between six months ago and 15 days ago, for example, running that today would give you employees from the range 2014-12-24 and 2015-06-09.

EDIT: For SQL Server 2005 and earlier:

SELECT [emp_name] 
FROM [TABLE] 
WHERE [DateColumn] BETWEEN DATEADD(MONTH, -6, cast(convert(char(11), getdate(), 113) as datetime)) 
    AND DATEADD(DAY, -15, cast(convert(char(11), getdate(), 113) as datetime))
Chris Schubert
  • 1,288
  • 8
  • 17
  • 1
    One syntax is missing `)` – Nad Jun 24 '15 at 10:25
  • that is working what if I want for `BETWEEN '2014-11-24' AND '2014-12-24'` for that one month – Nad Jun 24 '15 at 10:27
  • probably something similar to what you just wrote, but calculated: `BETWEEN DATEADD(MONTH, -7, GETDATE()) AND DATEADD(MONTH, -6, GETDATE())` – Chris Schubert Jun 24 '15 at 10:28
  • error `Incorrect syntax near the keyword 'BETWEEN'.` – Nad Jun 24 '15 at 10:29
  • This works for me if used after a valid `SELECT`. Can you paste what you're executing? – Chris Schubert Jun 24 '15 at 10:30
  • I want `emp_name` between `BETWEEN '2014-11-24' AND '2014-12-24' `. I ran the below query `Select BETWEEN DATEADD(MONTH, -7, GETDATE()) AND DATEADD(MONTH, -6, GETDATE())` – Nad Jun 24 '15 at 10:31
  • You should add a `*` in between `SELECT` and `BETWEEN`, or else defined individual columns. Also you must define your table. - `SELECT [emp_name] FROM [dbo].[emp_table] WHERE [dateColumn] BETWEEN DATEADD(MONTH, -7, GETDATE()) AND DATEADD(MONTH, -6, GETDATE())` – Chris Schubert Jun 24 '15 at 10:33
  • yes, that's working. Could you also read my comment which I replied to `t-clausen.dk` for more clarification whether my question is correct or not – Nad Jun 24 '15 at 10:41
  • @chirs: gettitn error as `Msg 243, Level 16, State 1, Line 1 Type DATE is not a defined system type. Msg 243, Level 16, State 1, Line 1 Type DATE is not a defined system type.` – Nad Jun 24 '15 at 10:47
  • I thought you said you were using Sql Server 2008? You must be on 2005 or earlier. – Chris Schubert Jun 24 '15 at 10:49
  • it is `Sql server 2008` only – Nad Jun 24 '15 at 10:50
  • 1
    because cast(getdate() as date) works from sqlserver 2008 or newer. You must be using an older compatibility level – t-clausen.dk Jun 24 '15 at 10:50
  • Please try my edit which will work with earlier versions. `DATE` was introduced in SQL Server 2008 - if you get that error, it must be an earlier version. – Chris Schubert Jun 24 '15 at 10:51
  • @ChrisSchubert: A bit change, i want all the employee name between `Nov 16 - Dec 15` means 6 months and 15 days gap. but i am not getting that employee – Nad Jun 24 '15 at 11:08
  • @ChrisSchubert: are you their ? please help – Nad Jun 24 '15 at 11:26