SQL Query to get all clients birthdays for this month - sql

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());

Related

How do you do a where clause with a select statement as a value

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

Need hour min and sec in a query

I am calculating the time difference between 2 times, I want to print the hour min and sec. Can anyone please tell me how to do it.
My query
SELECT
CONVERT(VARCHAR(8), DATEADD(ms, DATEDIFF(ms, CONVERT(VARCHAR(8), GETDATE(), 114), CONVERT(VARCHAR(8), VCTime, 114)), 0), 114) AS TImeDifference
FROM
Test
Output:
TimeDifference
---------------
10:51:37
20:51:37
21:51:37
22:21:37
08:51:37
00:51:37
Expected Output
TimeDifference
---------------
10h:51m:37s
20h:51m:37s
21h:51m:37s
22h:21m:37s
08h:51m:37s
00h:51m:37s
One way is to use sub query and concatenation operator + for 2008 with DATEPART function as below:
SELECT (
CAST(DATEPART(HOUR,(TImeDifference)) AS VARCHAR) + 'h:' +
CAST(DATEPART(MINUTE,(TImeDifference)) AS VARCHAR) + 'm:' +
CAST(DATEPART(SECOND,(TImeDifference)) AS VARCHAR) + 's')
FROM(
SELECT
CONVERT(varchar(8), DATEADD(ms, DATEDIFF(ms, convert(varchar(8),getdate(),114),
convert(varchar(8),VCTime,114)), 0), 114) as TImeDifference
FROM test
) t
Yes I realized concat is introduced in 2012 so we can use + instead
you can follow below way
DECLARE #x int,
#dt1 smalldatetime = '2018-08-17 03:24:16',
#dt2 smalldatetime = getdate()
SET #x = datediff (s, #dt1, #dt2)
SELECT convert(varchar, #x / (60 * 60 * 24)) + ':'
+ convert(varchar, dateadd(s, #x, convert(datetime2, '0001-01-01')), 108)
this will return 1:05:57:00
Try this:
select cast(date_diff / 3600 as varchar(4)) + 'h:' +
cast((date_diff % 3600) / 60 as varchar(4)) + 'm:' +
cast(date_diff % 60 as varchar(4)) + 's'
from (
select datediff(second, getdate(), VCTime) date_diff from my_table
) a
First, you should not be converting to strings to get the difference. I think this should be fine:
SELECT CONVERT(VARCHAR(8),
DATEDIFF(ms, CAST(GETDATE() as TIME), CAST(VCTime as TIME)),
114
) as TImeDifference
FROM Test;
Then you want to add "h", "m", and "s". You can use the STUFF() function. But let me do this using APPLY so the code doesn't look quite so messy:
SELECT ( STUFF(STUFF(TimeDifference_str, 6, 0, 'm'), 3, 0, 'h') + 's' ) as TimeDifference_hms
FROM test t CROSS APPLY
(VALUES (CONVERT(VARCHAR(8),
DATEDIFF(ms, CAST(GETDATE() as TIME), CAST(VCTime as TIME)),
114
)
)
) v(TimeDifference_str)

Get Last date of month SQL

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);

How to get 30 days from now from SQL table

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.

SQL Query - Have to determine if it is a specific day of the month (ex: 2 Tuesday of month)

I have an app that allows users schedule an action to occur in the future. For example, that can select a date and schedule it to run on that day every month (ex: the 15th of each month). However, I now need to allow them to select a week day and week of the month. For example, they need to run an action the first friday of the month. Therefore I am allowing the to select the weekday (monday, tuesday, wednesday....) and week of the month (1st, 2nd, 3rd, 4th or 5th).
Here is the query I currently use:
Declare #nowString varchar(19)
Declare #nowDateString varchar(19)
Declare #now datetime
Declare #lastMinute datetime
Declare #nowDate datetime
Set #nowString = '#currentDateTime#:00'
Set #nowDateString = LEFT(#nowString, 10)
set #now = #nowString
set #nowDate = DATEADD(dd, 0, DATEDIFF(dd, 0, #now))
set #lastMinute = DATEADD(mi, -1, #now)
select *
from message_prepared
where schedule = '1'
and active = '1'
and noaa = '0'
and (
(
schedule_type = 'recurring'
and startdate <= #nowDate
and isnull(enddate, DATEADD(yy, 1, #nowDate)) >= #nowDate
and (
#nowDateString + ' ' + isnull(recurring_start_time_hour, '00') + ':' + isnull(recurring_start_time_min, '00') + ':00' = #now
or #nowDateString + ' ' + isnull(recurring_start_time_hour, '00') + ':' + isnull(recurring_start_time_min, '00') + ':00' = #lastMinute
)
-- Test for different type of recurring
and (
( ltrim(rtrim(recurring)) = 'M' and DATEPART(dd, startdate) = DATEPART(dd, #now) )
or ( ltrim(rtrim(recurring)) = 'W' and DATEPART(dw, startdate) = DATEPART(dw, #now) )
or ltrim(rtrim(recurring)) = 'D'
)
)
or (
schedule_type = 'once'
and startdate = #nowDate
and (
#nowDateString + ' ' + onetime_start_time_hour + ':' + onetime_start_time_min + ':00' = #now
or #nowDateString + ' ' + onetime_start_time_hour + ':' + onetime_start_time_min + ':00' = #lastMinute
)
)
)
and repeat_multiple_times = 0
UNION ALL
SELECT *
FROM MESSAGE_PREPARED
WHERE schedule = '1'
AND active = 1
AND noaa = 0
AND recurring = 'D'
AND repeat_multiple_times = 1
AND startDate IS NOT NULL
AND recurring_start_time_hour IS NOT NULL
AND recurring_start_time_hour < 24
AND recurring_start_time_min IS NOT NULL
AND recurring_start_time_min < 60
AND startdate <= #nowDate
AND ISNULL(enddate, DATEADD(yy, 1, #nowDate)) >= #nowDate
AND
(
CASE WHEN repeat_unit = 'M'
THEN
DATEDIFF(n,
CONVERT(DATETIME,
CAST(DATEPART(yyyy, startDate) AS VARCHAR(4)) + '-' +
CAST(DATEPART(mm, startDate) AS VARCHAR(2)) + '-' +
CAST(DATEPART(dd, startDate) AS VARCHAR(2)) + ' ' +
CAST(recurring_start_time_hour AS VARCHAR(2)) + ':' +
CAST(recurring_start_time_min AS VARCHAR(2)) + ':00', 20),
GETDATE()) % repeat_interval
ELSE
DATEDIFF(n,
CONVERT(DATETIME,
CAST(DATEPART(yyyy, startDate) AS VARCHAR(4)) + '-' +
CAST(DATEPART(mm, startDate) AS VARCHAR(2)) + '-' +
CAST(DATEPART(dd, startDate) AS VARCHAR(2)) + ' ' +
CAST(recurring_start_time_hour AS VARCHAR(2)) + ':' +
CAST(recurring_start_time_min AS VARCHAR(2)) + ':00', 20),
GETDATE()) % (repeat_interval * 60)
END = 0
OR
CASE WHEN repeat_unit = 'M'
THEN
(DATEDIFF(n,
CONVERT(DATETIME,
CAST(DATEPART(yyyy, startDate) AS VARCHAR(4)) + '-' +
CAST(DATEPART(mm, startDate) AS VARCHAR(2)) + '-' +
CAST(DATEPART(dd, startDate) AS VARCHAR(2)) + ' ' +
CAST(recurring_start_time_hour AS VARCHAR(2)) + ':' +
CAST(recurring_start_time_min AS VARCHAR(2)) + ':00', 20),
GETDATE()) - 1) % repeat_interval
ELSE
(DATEDIFF(n,
CONVERT(DATETIME,
CAST(DATEPART(yyyy, startDate) AS VARCHAR(4)) + '-' +
CAST(DATEPART(mm, startDate) AS VARCHAR(2)) + '-' +
CAST(DATEPART(dd, startDate) AS VARCHAR(2)) + ' ' +
CAST(recurring_start_time_hour AS VARCHAR(2)) + ':' +
CAST(recurring_start_time_min AS VARCHAR(2)) + ':00', 20),
GETDATE()) - 1) % (repeat_interval * 60)
END = 0
)
This will only occur when reocurring is set to "M" and I would like to determine if today is the specific day of the week, week of the month and hour/min.
This is pretty simple logic. Today is the nth DOW of the month when the following is true:
Today is that day of the week
The day of the month is between 7*(n-1) + 1 and 7 * n
So, the first Monday of the month is always between the 1st and 7th, and so on. Here is an example case statement to test this:
declare #DayOfWeek varchar(255) = 'Thursday';
declare #Which int = 3;
select (case when datename(dw, Today) = #DayOfWeek and
(DAY(Today) - 1) / 7 = #Which - 1
then 1
else 0
end)
from (select CAST(getdate() as date) as Today) t
I've structured the query this way so you can test it with different values. Just replace the expression that defines Today, with something like getdate() - 3 or '2013-01-01'.