I am trying to insert data from a staging table into original table for a particular time period in Microsoft SQL Server Management Studio 18 using below query.
INSERT INTO dbo.trial ([turbineid], [createddate], [lastupdatedby],[lastupdateddate])
SELECT
[turbineid], [createddate], [lastupdatedby], [lastupdateddate]
FROM
dbo.trial_January
WHERE
createddate BETWEEN (DATEPART(mm, createddate) = 01
AND DATEPART(dd, createddate) = 01))
AND (DATEPART(mm, createddate) = 01
AND DATEPART(dd, createddate) = 30)
When trying to execute this query, I get this error:
Incorrect syntax near '='.
You are trying to compare a date with some integers.
Your where clause should look more like this
WHERE createddate BETWEEN (DATEFROMPARTS(DATEPART(YY,createddate),1,1)) AND (DATEFROMPARTS(DATEPART(YY,createddate),1,30))
This claus doesn't make sense
where createddate between
(DATEPART(mm , createddate) = 01 AND DATEPART(dd, createddate) = 01))
AND (DATEPART(mm, createddate) = 01 AND DATEPART(dd, createddate) = 30)
Between state needs two date to check if the given date is between two dates.
for exemple :
SELECT 'true' WHERE GETDATE() BETWEEN GETDATE() -1 AND GETDATE() + 1
And the DATEPART(mm, CreatedDate) returns the month of your date.
SELECT 'true' where DATEPART(mm , GETDATE()) = 10 //Checks if the month of the given date is 10.
According your query, you want to get records of january. So you can try this
INSERT INTO dbo.trial ([turbineid], [createddate], [lastupdatedby],[lastupdateddate])
SELECT
[turbineid], [createddate], [lastupdatedby], [lastupdateddate]
FROM
dbo.trial_January
WHERE
DATEPART(MONTH, createddate) = 01
AND DATEPART(YEAR, createddate) = 2020 -- the year you want
your where condition is not correct
where DATEPART(mm , createddate) = 1
gives you results for first month. you dont need to have anything else for the day if your intention is to look for whole january
Related
basically trying to create a query that will display employee anniversary dates for upcoming month or year of current date, also would like to display a column that shows the years of service
SELECT
Employee,
Hire_Date
CASE WHEN DATEADD(YY,DATEDIFF(yy,Hire_Date,GETDATE()),Hire_Date)<GETDATE() THEN DATEDIFF(yy,Hire_Date,GETDATE())
ELSE DATEDIFF(yy,Hire_Date,GETDATE())-1 END AS 'Years of service'
FROM
MyTable
looking to display employees with anniversary dates coming up in the coming month or in the next year
Here is the script validated (see pciture below) to display the employees with birth_date coming in the next month
Replace mytable by your own table
declare #mytable as table(employe varchar(100), birth_date datetime,hire_date datetime)
insert into #mytable values
('name1','01/01/1972','01/01/2000') ,
('name2','12/02/1985','01/02/2003') ,
('name3','04/12/1990','03/04/2005') ,
('name4','05/03/1969','12/12/2005') ,
('name5','04/02/1968','12/02/2010') ,
('name6','04/04/1968','12/11/2009') ,
('name7','12/03/1978','01/01/2019') ,
('name8','01/12/2000','03/02/2018') ,
('name9','12/12/1970','05/02/2019') ,
('name10','04/04/1980','04/04/2018')
select employe,birth_date,hire_date,
CASE WHEN DATEADD(YY,DATEDIFF(yy,Hire_Date,GETDATE()),Hire_Date)<GETDATE() THEN DATEDIFF(yy,Hire_Date,GETDATE())
ELSE DATEDIFF(yy,Hire_Date,GETDATE())-1 END AS 'Years of service'
from #mytable where (
month(getdate()) < 12
and
month(birth_date)=1+month(getdate()) )
or (month(getdate())=12 and month(birth_date)=1)
I don't understand well, but, if you need know who make anniversary in the next month, or next year, you should use DATEDIFF function for to filter the data.
Example:
SELECT Employee, Hire_Date, DATEDIFF(year, Hire_Date, getdate()) as YearsService
FROM MyTable
-- if you need fetch to next month, you should use <= 1
WHERE DATEDIFF(month, CONCAT(YEAR(GETDATE()), '-', MONTH(Hire_Date), '-' , DAY(Hire_Date)), GETDATE()) = 1
I am trying to get data from my Database of those who have upcoming birth days in next few days(declared earlier)
it's working fine for days but this query will not work if i add 24 days to current date cause than it will need change in month.
i wonder how can i do it
declare #date int=10,
#month int=0
select * from STUDENT_INFO where DATEPART(DD,STDNT_DOB) between
DATEPART(DD,GETDATE()) and DATEPART(DD,DATEADD(DD,#date,GETDATE()))
and
DATEPART(MM,STDNT_DOB) = DATEPART(MM,DATEADD(MM,#month,GETDATE()))
This query works fine but it only checks date between 8 & 18
but if i use it like this
declare #date int=30,
#month int=0
select * from STUDENT_INFO where DATEPART(DD,STDNT_DOB) between
DATEPART(DD,GETDATE()) and DATEPART(DD,DATEADD(DD,#date,GETDATE()))
and
DATEPART(MM,STDNT_DOB) = DATEPART(MM,DATEADD(MM,#month,GETDATE()))
it will return nothing since it require addition in month as well
If I Use it like this
declare #date int=40,
#month int=0
select * from STUDENT_INFO where DATEPART(DD,STDNT_DOB) between
DATEPART(DD,GETDATE()) and DATEADD(DD,#date,GETDATE())
and
DATEPART(MM,STDNT_DOB) = DATEPART(MM,DATEADD(MM,#month,GETDATE()))
than it will return results till the last of this month but will not show till 18/12 which was required
Here is a simple way to solve it:
DECLARE #date int = 10
;WITH cte as
(
SELECT cast(getdate() as date) fromdate,
cast(getdate() as date) todate,
1 loop
UNION ALL
SELECT cast(dateadd(year, -loop, getdate()) as date),
cast(dateadd(year, -loop, getdate())+#date as date),
loop+1
FROM cte
WHERE loop < 200 -- go back 200 years
-- (should be enough unless you are a turtle)
)
SELECT dob, name, datediff(year, dob, getdate()) will_turn
FROM cte
JOIN (values(cast('1968-11-11' as date), 'Jack'),
(cast('1984-11-12' as date), 'Jill'),
(cast('1984-11-13' as date), 'Hans'),
(cast('1984-11-21' as date), 'Gretchen'),
(cast('1884-11-22' as date), 'Snowwhite')) x(dob, name)
ON dob BETWEEN fromdate and todate
OPTION (maxrecursion 300)
Returns:
dob name name will_turn
1984-11-12 Jill 29
1984-11-13 Hans 29
1984-11-21 Gretchen 29
1968-11-11 Jack 45
avoiding udf:-
create table #dob (
name varchar(50),
cakeday date
)
go
insert into #dob values
('yesterday',dateadd(yy,-50,getdate()-1)),
('today',dateadd(yy,-51,getdate())),
('ten days',dateadd(yy,-52,getdate()+10)),
('eleven days',dateadd(yy,-53,getdate()+11))
go
select *
from #dob d
where datepart(dayofyear,d.cakeday)
between datepart(dayofyear,getdate()) and datepart(dayofyear,getdate())+10
returns (on 2013-11-09):-
name cakeday
today 1962-11-09
ten days 1961-11-19
Would this work for you?
declare #date int = 10
select * from STUDENT_INFO
where DATEDIFF(DD, GETDATE(),
DATEFROMPARTS(DATEPART(YYYY, GETDATE()), -- this years birthday
DATEPART(MM, STDNT_DOB),
DATEPART(DD, STDNT_DOB))) between 0 and #date
or DATEDIFF(DD, GETDATE(),
DATEFROMPARTS(DATEPART(YYYY, GETDATE()) + 1, -- next years birthday
DATEPART(MM, STDNT_DOB),
DATEPART(DD, STDNT_DOB))) between 0 and #date
The trick here is to get the students birthday this year (and next year) and get the number of days until that date. I did that by creating a calculated field using the students day and month of birth, and included todays year.
The check against next years birthday (i.e., the second part of the OR clause) is required to get those students whose upcoming birthday is early next year.
Edit: the query will fail if anyone on the table was born on feb/29 (on a leap year, of course). Thanks to t-clausen.dk for poiting it out.
Could you please help me select a date which is the beginning of a particular following month, e.g. April?
For example, if it is Jan 08 2013, it should select April 01 2013, but if it is June 08 2013, it should select April 01 2014.
Thanks.
I would create a calendar table, then you can simply do something like this:
select
min([Date])
from
dbo.Calendar
where
MonthNumber = 4 and
DayNumber = 1 and
[Date] > getdate()
Querying a calendar table is usually clearer, simpler and more flexible than using date functions. You might also want to consider what happens if today is April 1: do you want today's date, or next year's?
If you're interested in April 1 because it's the start of a financial year, you can add that information to your calendar table directly:
select
min([Date])
from
dbo.Calendar
where
IsStartOfFinancialYear = 0x1 and
[Date] > getdate()
Use the DATEADD and the DATEPART function, like this short example:
DECLARE #Date Datetime
SET #Date = '2013.01.08 00:00:00'
SELECT DATEADD(year,
CASE WHEN DATEPART(month, #Date) < 4
THEN 0
ELSE 1 END,
DATEADD(day, -DATEPART(day, #Date) + 1,
DATEADD(month, -DATEPART(month, #Date) + 4, #Date)))
On a datawarehouse project with SSIS/SSAS, I have to generate my own time dimension because I've personal data to integrate with.
My problem is with SSAS because I also need to integrate translation. After reading the documentation, I've found a command to set language for the current session by using SET LANGUAGE ENGLISH but I'm not able to change language for different field of the query.
Is there a way to generate MONTH_NAME in French and also get MONTH_NAME_DE in German ?
Here is the script that I've found on Internet
WITH Mangal as
(
SELECT Cast ('1870-01-01' as DateTime) Date --Start Date
UNION ALL
SELECT Date + 1
FROM Mangal
WHERE Date + 1 < = '2015-12-31' --End date
)
SELECT
Row_Number() OVER (ORDER BY Date) as ID
, Date as DATE_TIME
, YEAR (date) as YEAR_NB
, MONTH (date) as MONTH_NB
, DAY (date) as DAY_NUMBER
, DateName (mm, date) as MONTH_NAME
, LEFT ( DateName (mm, date), 3) KMONTH_NAME
, DateName (dw, date) as DAY_NAME
, LEFT (DateName (dw, date), 3) as KDAY_NAME
, (SELECT TOP 1 FIELD
FROM TABLEXY
WHERE Date BETWEEN TABLEXY.DATE_FROM AND LEGISLATUR.DATE_TO
AND LANGAGE = 'FR'
) as PERSONAL_FIELD
, (SELECT TOP 1 FIELD
FROM TABLEXY
WHERE Date BETWEEN TABLEXY.DATE_FROM AND LEGISLATUR.DATE_TO
AND LANGAGE = 'DE'
) as PERSONAL_FIELD_DE
FROM Mangal
OPTION (MAXRECURSION 0)
SQL Server has a table containing names of Months and week days. However, they are stored as comma delimited values:
select
months,
shortmonths,
days
from
master.dbo.syslanguages
where
alias in ('English','French', 'German')
You might use this in your query.
I have a SQL SERVER Table which has only one field "StartDate" the records are as follows
**
2011-07-28 19:30:00.000
2011-07-29 21:50:00.000
2011-07-25 09:20:00.000
**
What i want to do is :
SHOW RECORDS if its CURRENT DATE ( todays date ) and the time difference between current time the StartDate is not less then 5 minutes, i have written the following code but it doesnt show me the time difference ?
SELECT * FROM table WHERE DATEDIFF(day, StartDate, GETDATE()) <= 0
SELECT StartDate
FROM table
WHERE YEAR(StartDate)=YEAR(GETDATE())
AND MONTH(StartDate)=MONTH(GETDATE())
AND DAY(StartDate)=DAY(GETDATE())
AND (DATEDIFF(minute, StartDate, GETDATE()) >= 5
OR
DATEDIFF(minute, StartDate, GETDATE()) <= 5)
How about:
SELECT StartDate
,GETDATE()
,DATEDIFF(day, StartDate, GETDATE())
,DATEDIFF(minute, StartDate, GETDATE())
,*
FROM table
WHERE DATEDIFF(day, StartDate, GETDATE()) <= 0
AND DATEDIFF(minute, StartDate, GETDATE()) >= 5
There are two ways to do it one being DateDiff the other is DATEADD. Judging by the way I read your question DateAdd should do it. Here is an example;
SELECT *
FROM [dbo].[TABLE]
WHERE [LAST_UPDATE] > DATEADD(minute,-5,GetDate())
Using BETWEEN is probably a little more optimal than two AND statements (maybe not). Try not to do a calculation on each row if you don't have to. Doing DATEADD only on the current date will only be calculated once.
SELECT
whatever
FROM
table
WHERE
StartDate
BETWEEN FLOOR( CAST( GETDATE() AS FLOAT ) )
AND DATEADD(minute, -5, GETDATE())
I interpret the question as looking for rows where the date is today (between the start of today) but not within the last 5 minutes (and less than 5 minutes ago). That might not be what you were going for.
Hope that helps