I am doing my project in MVC4 using c# and sql.. I have a table MemberDetails which contain table
CREATE TABLE [dbo].[MemberDetails] (
[Id] INT IDENTITY (1, 1) NOT NULL,
[Mem_FirstNA] VARCHAR (100) NOT NULL,
[Mem_LastNA] VARCHAR (100) NOT NULL,
[Mem_Occ] VARCHAR (100) NOT NULL,
[Mem_DOB] DATETIME NOT NULL,
[Mem_Email] VARCHAR (50) NOT NULL,
PRIMARY KEY CLUSTERED ([Id] ASC)
);
I just want to select the names and date of birth where whose birthday in next 30 days and I use the following query
SELECT
Mem_FirstNA, Mem_LastNA, Mem_DOB
FROM
MemberDetails
WHERE
Mem_DOB >= getdate() - 1 AND Mem_DOB <= getdate() + 30
Is that correct, I got 0 item selected , I use the following table.
1 Pal Software 08-03-1987 AM 12:00:00
3 mn Par Bussiness 19-10-1967 AM 12:00:00
4 man George Business 13-11-1956 AM 12:00:00
5 Smi Kan Housewife 22-10-1980 AM 12:00:00
try this
SELECT
Mem_FirstNA, Mem_LastNA, Mem_DOB
FROM
MemberDetails
WHERE DAYOFYEAR(Mem_DOB)-DAYOFYEAR(getdate())<=30
Updated
SELECT
Mem_FirstNA, Mem_LastNA, Mem_DOB
FROM
MemberDetails
WHERE
DATEDIFF(DAY, GETDATE(), DATEADD(YEAR, DATEDIFF(YEAR, Mem_DOB, GETDATE()), Mem_DOB)) BETWEEN 0 AND 30
SELECT
Mem_FirstNA, Mem_LastNA, Mem_DOB
FROM
MemberDetails
WHERE
Mem_DOB >= Cast(current_timestamp As Date) AND Mem_DOB < DATEADD(d, 30, Cast(current_timestamp As Date));
Should work. Between usage was removed based on comments.
Will work for January birth dates as well
DECLARE #now DATETIME
SET #now = GETDATE()
SELECT Mem_FirstNA, Mem_LastNA, Mem_DOB
FROM MemberDetails
WHERE
CASE WHEN month(Mem_DOB) = 1
THEN DATEADD(YY, YEAR(DATEADD(DAY, 30, DATEADD(m, month(#now) - 1, DAY(#now) - 1))) - 1900, DATEADD(m, month(Mem_DOB) - 1, DAY(Mem_DOB) - 1))
ELSE DATEADD(m, month(Mem_DOB) - 1, DAY(Mem_DOB) - 1)
END
> DATEADD(m, month(#now) - 1, DAY(#now) - 1)
AND
CASE WHEN month(Mem_DOB) = 1
THEN DATEADD(YY, YEAR(DATEADD(DAY, 30, DATEADD(m, month(#now) - 1, DAY(#now) - 1))) - 1900, DATEADD(m, month(Mem_DOB) - 1, DAY(Mem_DOB) - 1))
ELSE DATEADD(m, month(Mem_DOB) - 1, DAY(Mem_DOB) - 1)
END
< DATEADD(DAY, 30, DATEADD(m, month(#now) - 1, DAY(#now) - 1))
Hope it helps
Try this
SELECT
Mem_FirstNA, Mem_LastNA, Mem_DOB
FROM
MemberDetails
WHERE
WHERE DATEPART(mm,Mem_DOB) BETWEEN DATEPART(mm,getDate()) AND DATEPART(mm,getDate())+1;
why not use this where clause:
WHERE MEM_DOB BETWEEN GETDATE()-1 AND GETDATE()+30
Try this...
SELECT
Mem_FirstNA, Mem_LastNA, Mem_DOB
FROM
MemberDetails
WHERE
ltrim(str(year(GETDATE()))) + '-' + ltrim(str(month(Mem_DOB))) + '-' + ltrim(str(day(Mem_DOB))) >= getdate() - 1 AND ltrim(str(year(GETDATE()))) + '-' + ltrim(str(month(Mem_DOB))) + '-' + ltrim(str(day(Mem_DOB))) <= getdate() + 30
EDIT: The comments for this answer have rightly pointed out that it will not work if the current year is a leap year. So this update. The list of dates can be more efficiently generated by using Get a list of dates between two dates using a function
Select Mem_FirstNA, Mem_LastNA, Mem_DOB from MemberDetails m, (
Select datepart(dd,getdate()) as d, datepart(mm,getdate()) as m
union
Select datepart(dd,getdate() + 1) as d, datepart(mm,getdate() + 1) as m
union
Select datepart(dd,getdate() + 2) as d, datepart(mm,getdate() + 2) as m
union
Select datepart(dd,getdate() + 3) as d, datepart(mm,getdate() + 3) as m
union
Select datepart(dd,getdate() + 4) as d, datepart(mm,getdate() + 4) as m
union
Select datepart(dd,getdate() + 5) as d, datepart(mm,getdate() + 5) as m
union
Select datepart(dd,getdate() + 6) as d, datepart(mm,getdate() + 6) as m
union
Select datepart(dd,getdate() + 7) as d, datepart(mm,getdate() + 7) as m
union
Select datepart(dd,getdate() + 8) as d, datepart(mm,getdate() + 8) as m
union
Select datepart(dd,getdate() + 9) as d, datepart(mm,getdate() + 9) as m
union
Select datepart(dd,getdate() + 10) as d, datepart(mm,getdate() + 10) as m
union
Select datepart(dd,getdate() + 11) as d, datepart(mm,getdate() + 11) as m
union
Select datepart(dd,getdate() + 12) as d, datepart(mm,getdate() + 12) as m
union
Select datepart(dd,getdate() + 13) as d, datepart(mm,getdate() + 13) as m
union
Select datepart(dd,getdate() + 14) as d, datepart(mm,getdate() + 14) as m
union
Select datepart(dd,getdate() + 15) as d, datepart(mm,getdate() + 15) as m
)X
where
datepart(dd, m.Mem_DOB) = x.d and datepart(mm, m.Mem_DOB) = x.m
If you are downvoting, please comment why.
Related
I'm trying to get the total amount from the next month's record.
SELECT TOP (1000)
[DEMAND_POINT_ID],
[DPM_XREF_ID],
[UTIL_TYPE],
[ACCOUNT_ID],
[REV_YR_MNTH],
[TOTAL_AMOUNT],
(SELECT CAST(LEFT((SELECT CONVERT(varchar, DATEADD(month, 1, LEFT(REV_YR_MNTH, 4) + '-' + RIGHT(REV_YR_MNTH, 2) + '-' + '01'), 112)), 6) AS int)) AS nextmonth,
(SELECT de2.[TOTAL_AMOUNT]
FROM [DEQEnergyUsage].[dbo].[DST_ENERGY_USAGE_AGGR] de2
WHERE de2.ACCOUNT_ID = de.ACCOUNT_ID
AND de2.REV_YR_MNTH = (SELECT CAST(LEFT((SELECT CONVERT(varchar, DATEADD(month, 1, LEFT(REV_YR_MNTH, 4) + '-' + RIGHT(REV_YR_MNTH, 2) + '-' + '01'), 112)), 6) AS int))) AS nextamount
FROM
[DEQEnergyUsage].[dbo].[DST_ENERGY_USAGE_AGGR] de
WHERE
ACCOUNT_ID =1
ORDER BY
ACCOUNT_ID, de.REV_YR_MNTH
The value nextmonth creates the next month's rev_yr_mnth correctly however when I try to use this created value in the where clause it brings back nothing in the nextamount field.
It's like it does not recognize the created value as a true value. What do I need to do to have the where clause recognize the value?
Using CTE you can add condition on nextmonth.
With CTE As (
SELECT TOP (1000)
[DEMAND_POINT_ID],
[DPM_XREF_ID],
[UTIL_TYPE],
[ACCOUNT_ID],
[REV_YR_MNTH],
[TOTAL_AMOUNT],
(SELECT CAST(LEFT((SELECT CONVERT(varchar, DATEADD(month, 1, LEFT(REV_YR_MNTH, 4) + '-' + RIGHT(REV_YR_MNTH, 2) + '-' + '01'), 112)), 6) AS int)) AS nextmonth,
(SELECT de2.[TOTAL_AMOUNT]
FROM [DEQEnergyUsage].[dbo].[DST_ENERGY_USAGE_AGGR] de2
WHERE de2.ACCOUNT_ID = de.ACCOUNT_ID
AND de2.REV_YR_MNTH = (SELECT CAST(LEFT((SELECT CONVERT(varchar, DATEADD(month, 1, LEFT(REV_YR_MNTH, 4) + '-' + RIGHT(REV_YR_MNTH, 2) + '-' + '01'), 112)), 6) AS int))) AS nextamount
FROM
[DEQEnergyUsage].[dbo].[DST_ENERGY_USAGE_AGGR] de
WHERE
ACCOUNT_ID =1
ORDER BY
ACCOUNT_ID, de.REV_YR_MNTH
)
select * from CTE
-- Where nextmonth condition
I have the following query logic
Show comments which is not deleted
Show comments which is deleted after created, within a timespan of 30 days before today(Today - Createddate <= 30)
Below is the logic which I wrote, but I don't think it is working correct.
SELECT string_agg(
CAST(CONCAT_WS(',',
c.Id,
COALESCE(c.ParentCommentId, 0),
c.TotalRatingsCount,
c.Pinned,
c.IsDeleted,
FORMAT(c.CreatedDate, 'yyyy-MM-dd HH:mm:ss')) AS VARCHAR(MAX))
, '|')
FROM Comments c
WHERE c.DiscussionId = d.Id
and
((c.IsDeleted = 0 and DATEDIFF(day, c.CreatedDate , GETDATE()) >= 30))
or
(((c.IsDeleted = 1 or c.IsDeleted =0) and DATEDIFF(day, c.CreatedDate , GETDATE()) <= 30))
SELECT string_agg(
CAST(CONCAT_WS(',',
c.Id,
COALESCE(c.ParentCommentId, 0),
c.TotalRatingsCount,
c.Pinned,
c.IsDeleted,
FORMAT(c.CreatedDate, 'yyyy-MM-dd HH:mm:ss')) AS VARCHAR(MAX))
, '|')
FROM Comments c
WHERE c.DiscussionId = d.Id
and
((c.IsDeleted = 0 )
or
((c.IsDeleted = 1 ) and DATEDIFF(day, c.CreatedDate , GETDATE()) <= 30))
I'm trying to create a query that will display all clients born this month, but I'm getting:
Conversion failed when converting date and/or time from character string."
Here is my code.
SELECT
t.Client_id, t.Name
FROM
(SELECT
Client_id,
C_name + ' ' + C_surname AS Name,
CONVERT(INT, SUBSTRING(Client_id, 3, 2)) AS M,
CONVERT(INT, SUBSTRING(Client_id, 5, 2)) AS D,
CONVERT(DATE, CONVERT(VARCHAR, CONVERT(CHAR(4), DATEPART(YEAR, GETDATE())) + '-' + SUBSTRING(Client_id, 3, 2) + '-' + SUBSTRING(Client_id, 5, 2))) AS DateOfBirth
FROM
tblClientInfo) T
WHERE
T.M <= 12
AND T.DateOfBirth = CONVERT(DATE, GETDATE());
I tried to put CONVERT(varchar, getdate()) but still getting error
This one works well for extracting birthdays for today only:
SELECT
t.Client_id, t.Name
FROM
(SELECT
Client_id,
C_name + ' ' + C_surname AS Name,
CONVERT(INT, SUBSTRING(Client_id, 3, 2)) AS M,
CONVERT(INT, SUBSTRING(Client_id, 5, 2)) AS D,
CONVERT(DATE, CONVERT(VARCHAR, CONVERT(CHAR(4), DATEPART(YEAR, GETDATE())) + '-' + SUBSTRING(Client_id, 3, 2) + '-' + SUBSTRING(Client_id, 5, 2))) AS DateOfBirth
FROM
tblClientInfo) T
WHERE
T.D <= 31
AND T.M <= 12
AND T.DateOfBirth = CONVERT(DATE, GETDATE());
Try this:
select *
from tblClientInfo
where CAST(SUBSTRING(Client_id, 3, 2) AS TINYINT) = month(getdate());
Hmmm . . . I'm thinking:
select c.*
from tblClientInfo c
where month(dateofbirth) = month(getdate());
I need to find the last day of a month in the american (mm-dd-yyyy) format for a column which has yymm(nvarchar format).
example:- for 1601---> 01-31-2016
Thank you for help!
Using convert to get the date of the 1st of the month, then dateadd to get the next month, and one more dateadd to get one day before:
DECLARE #D char(4) = '1601'
SELECT DATEADD(DAY, -1, DATEADD(MONTH, 1, CONVERT(date, #D + '01', 12)))
Result:
2016-01-31
With a string of 1601, we just need to append a 01 because the century will be assumed. This new string of 160101 can be converted into a date.
Example
select convert(varchar(10),dateadd(day,-1,dateadd(month,1,YourCol+'01')),101)
From YourTable
Returns
01/31/2016
Since you said year 20XX...
declare #oddDate nvarchar(4) = '1601'
select
DATEADD(d, -1, DATEADD(m, DATEDIFF(m, 0, '20' + cast(YY as varchar(2)) + cast(MM as varchar(2)) + '01') + 1, 0)) as LastDayof20Year
,'20' + cast(YY as varchar(2)) + cast(MM as varchar(2)) + '01' as MadeUpDate
from
(select
left(#oddDate,2) as YY
,right(#oddDate,2) as MM) x
Or simply...
select
DATEADD(d, -1, DATEADD(m, DATEDIFF(m, 0, '20' + cast(left(#oddDate,2) as varchar(2)) + cast(right(#oddDate,2) as varchar(2)) + '01') + 1, 0)) as LastDayof20Year
Something like the following should do the trick...
IF OBJECT_ID('tempdb..#TestData', 'U') IS NOT NULL
DROP TABLE #TestData;
CREATE TABLE #TestData (
CharDate CHAR(4) NOT NULL
);
INSERT #TestData (CharDate) VALUES
('9801'), ('9902'), ('0012'), ('0202'), ('1005'), ('1503');
--============================================================
SELECT
FormattedEOM = CONVERT(CHAR(10), em.EndOfMonth, 101)
FROM
#TestData td
CROSS APPLY ( VALUES (CASE WHEN CAST(LEFT(td.CharDate, 2) AS INT) > 30 THEN '19' ELSE '20' END) ) c (Century)
CROSS APPLY ( VALUES (DATEFROMPARTS(CONCAT(c.Century, LEFT(td.CharDate, 2)), RIGHT(td.CharDate, 2), 1)) ) fm (FirstOfMonth)
CROSS APPLY ( VALUES (EOMONTH(fm.FirstOfMonth)) ) em (EndOfMonth);
HTH,
Jason
Edit: The following should work with 2008....
SELECT
FormattedEOM = CONVERT(CHAR(10), em.EndOfMonth, 101)
FROM
#TestData td
CROSS APPLY ( VALUES (CASE WHEN CAST(LEFT(td.CharDate, 2) AS INT) > 30 THEN '19' ELSE '20' END) ) c (Century)
CROSS APPLY ( VALUES (CAST(c.Century + td.CharDate + '01' AS DATE)) ) fm (FirstOfMonth)
CROSS APPLY ( VALUES (DATEADD(mm, 1, fm.FirstOfMonth)) ) nm (NextMonth)
CROSS APPLY ( VALUES (DATEADD(dd, -1, nm.NextMonth)) ) em (EndOfMonth);
I have the following sql statement to find all Mondays dates of the year.
SELECT DateAdd(week,
o1.v + o0.v,
DateAdd(day,
2 - DatePart(dw, Convert(VARCHAR(4), 2012) + '-01-01'),
Convert(VARCHAR(4), 2012) + '-01-01'
)
)
FROM (SELECT 0 AS v UNION
SELECT 8 UNION
SELECT 16 UNION
SELECT 24 UNION
SELECT 32 UNION
SELECT 40 UNION
SELECT 48) AS o1
CROSS JOIN (SELECT 0 AS v UNION
SELECT 1 UNION
SELECT 2 UNION
SELECT 3 UNION
SELECT 4 UNION
SELECT 5 UNION
SELECT 6 UNION
SELECT 7) AS o0
WHERE 2012 = DatePart(year,
DateAdd(week,
o1.v + o0.v,
DateAdd(day,
2 - DatePart(dw, Convert(VARCHAR(4), 2012) + '-01-01'),
Convert(VARCHAR(4), 2012) + '-01-01')
)
)
How can i modify it in order to find all monday dates after the todays date?
I would create a calendar table rather than write a complex query. Then you can write a clear, simple query like this:
select
c.BaseDate
from
dbo.Calendar c
where
c.DayOfWeek = 'Monday' and
c.YearNumber = year(getdate()) and
c.BaseDate > getdate()
As a general rule, a calendar table is the easiest solution for working with dates because it is a lot simpler to query and maintain than functions, and you can add columns whenever you need to support a new date attribute.
there are many functions to help a long with CTE , it will be simple ,below some suggestions , hope it help .
declare #DateFrom Date
declare #DateTo Date
set #DateFrom ='2016-01-01'
set #DateTo = '2016-12-31'
SELECT AllDates as MonDates from
(Select DATEADD(d, number, #dateFrom) as AllDates from master..spt_values
where type = 'p' and number between 0 and datediff(dd, #dateFrom, #dateTo)) AS D1
WHERE DATENAME(dw, D1.AllDates)In('Monday')
you can do on the where but I think its quite complicated.
A CTE is a good workaround:
with DAYS as (
SELECT DateAdd(week, o1.v + o0.v, DateAdd(day, 2 - DatePart(dw
, Convert(VARCHAR(4), 2012) + '-01-01'), Convert(VARCHAR(4)
, 2012) + '-01-01')) as MY_DAY
FROM (SELECT 0 AS v UNION SELECT 8 UNION SELECT 16 UNION SELECT 24
UNION SELECT 32 UNION SELECT 40 UNION SELECT 48) AS o1
CROSS JOIN (SELECT 0 AS v UNION SELECT 1 UNION SELECT 2 UNION SELECT 3
UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7) AS o0
WHERE 2012 = DatePart(year, DateAdd(week, o1.v + o0.v, DateAdd(day
, 2 - DatePart(dw, Convert(VARCHAR(4), 2012) + '-01-01')
, Convert(VARCHAR(4), 2012) + '-01-01')))
)
select MY_DAY from DAYS
where MY_DAY >getdate()
I used succesfully this query (i adapted one of the answers here)
SELECT MondaysThisMonth = cast (DATEADD(DAY,n,MondayBeforeFOM) as date)
FROM (
SELECT FirstOfMonth, MondayBeforeFOM = DATEADD(DAY,DATEDIFF(DAY,0,FirstOfMonth)/7*7,0)
FROM (SELECT FirstOfMonth = DATEADD(MONTH,DATEDIFF(MONTH,0,GETDATE()),0)) d
) e
CROSS JOIN (SELECT 7 UNION ALL SELECT 14 UNION ALL SELECT 21 UNION ALL SELECT 28 UNION ALL SELECT 35) f (n)
WHERE DATEADD(DAY,n,MondayBeforeFOM) < DATEADD(MONTH,1,FirstOfMonth)