I'm trying to get yy-MM, I tried this but it doesn't work
DECLARE #Date VARCHAR(10) = CONVERT(date, DATEADD(month, -1, CONVERT(date, GETDATE(), 'yy-MM')));
SELECT #Date AS [Date]
How to make it return 2020-03 rather than 2020-03-30?
How to make it return as 2020-03 rather then 2020-03-30?
Are you looking for this?
SELECT convert(varchar(7), getdate(), 126)
Output
2020-04
Demo Here
Related
I have customer_since stored as YYYYWW i.e., 201852.
The below script is what I have been using to work out the difference, however when I have this situation of 201901 - 201852, it gives 48 instead of 1.
is there mod function or something that can be incorporated here to resolve the issue?
CAST(CONCAT (DATEPART(YEAR, DATEADD(MONTH, + 9, GETDATE()))
, RIGHT('0' + RTRIM(DATEPART(WEEK, DATEADD(MONTH, - 3, GETDATE()))+1),2)
) AS INT) - a.customer_since AS Customer_since
You can use the following template to get the first (or the last) date of a given year and week. Then having valid date times, simply use DATEDIFF as it has built in functionality to get difference in weeks:
DECLARE #WeekNo int= 52
DECLARE #Year int=2018
SELECT DATEADD(wk,#WeekNo-1,DATEADD(yy,#Year-1900,0)) AS WeekStart,
DATEADD(wk,#WeekNo,DATEADD(yy,#Year-1900,0))-1 AS WeekEnd
GO
DECLARE #WeekNo int= 1
DECLARE #Year int=2019
SELECT DATEADD(wk,#WeekNo-1,DATEADD(yy,#Year-1900,0)) AS WeekStart,
DATEADD(wk,#WeekNo,DATEADD(yy,#Year-1900,0))-1 AS WeekEnd
GO
SELECT DATEDIFF(WEEK, '2018-12-24 00:00:00.000', '2019-01-01 00:00:00.000')
In one statement:
DECLARE #Input01 VARCHAR(6) = '201852'
,#Input02 VARCHAR(6) = '201901'
SELECT DATEDIFF(WEEK, DATEADD(WEEK, RIGHT(#Input01, 2)-1,DATEADD(YEAR,LEFT(#Input01, 4)-1900,0)), DATEADD(WEEK,RIGHT(#Input02, 2)-1,DATEADD(YEAR,LEFT(#Input02, 4)-1900,0)));
You can create a function which will accept the date string('201852') and will return a date like `Create Function Todate(#dt varchar(100))
returns Date
Begin
Declare #wk int=(Select RIGHT(#dt,2))
return (Select Dateadd(WK, #wk, cast(LEFT(#dt,4) as date)))
End`
Now all you have to do is pass the date string in the function and find a DATEDIFF with start and End date
I have old DBF database that contains date in old 6 digit formats like 291292 or 150793 or even 010302.
I import tables into SQL Server and now I need to convert it to datetime format.
Of course must be converted to similar strings 29.12.1992, 15.07.1993, 01.03.2002 first.
You may try this.
GO
declare #date date
set #date = '901124'
select CONVERT(varchar(10), #date, 102) as [Date] --- for yyyy.MM.dd format
TRY THIS
declare #dd varchar(10)
set #dd = '201292'
select CONVERT(VARCHAR(10), #dd, 2), CONVERT(VARCHAR(10), GETDATE(), 2)
, (SUBSTRING(#dd, 5,2) + SUBSTRING(#dd, 3,2) + SUBSTRING(#dd, 1,2))
, CONVERT(varchar(10), cast((SUBSTRING(#dd, 5,2) + SUBSTRING(#dd, 3,2) + SUBSTRING(#dd, 1,2)) as DATE) , 102)
Ok it pretty simple:
GO
declare #date varchar(6)
set #date = '241190'
select CONVERT(date, concat(SUBSTRING(#date, 5,2),SUBSTRING(#date, 3,2),SUBSTRING(#date, 1,2)), 101) as [Date]
result is 1990-11-24
declare #date date = '901124'
select CONVERT(date, #date) as [Date]
I have tried many different queries that already given here, but it also shows previous year data, for example, if use this query
Dateadd(Month, Datediff(Month, 0, DATEADD(m, -6, current_timestamp)), 0)
or
DATEADD(m, -6, current_timestamp)
These will result in the last six months. In my case, I want to print the last six month data. But the problem is if there is a data with the same date and different in the year will also result with these query eg (the above query returns the result of '2018-03-27 10:04:52.537' and if there is a data with '2017-03-27 10:04:52.537' will also come with the result). Can anyone help me?
here is my query
DECLARE #date6MonthAgo DATETIME = DATEADD(m, -6, current_timestamp)
DECLARE #totalCount INT = (SELECT COUNT(*)
FROM [PSA].[ProductionOrder]
WHERE CreatedDate >= #date6MonthAgo)
DECLARE #openCount INT = (SELECT COUNT(*)
FROM [PSA].[ProductionOrder]
WHERE DocumentStatusCode=7
AND CreatedDate >= #date6MonthAgo )
SELECT #date6MonthAgo, #totalCount, #openCount
Try something like this, after putting in the correct IDcolumn, just to verify for yourself the dates that are being returned
DECLARE #date6MonthAgo DATETIME = DATEADD(m, -6, current_timestamp);
SELECT #date6MonthAgo;
SELECT CreatedDate, keycolumnID
FROM [PSA].[ProductionOrder]
WHERE DocumentStatusCode=7
AND CreatedDate >= #date6MonthAgo
ORDER BY CreatedDate;
You can use this for find the last 6 month data from current date,
DECLARE #fromdate as datetime, #todate as datetime
set #fromdate = (select DATEADD(month, -6, getdate()))
set #todate = getdate()
SELECT CreatedDate, keycolumnID
FROM [PSA].[ProductionOrder]
WHERE DocumentStatusCode=7
AND cast(CreatedDate as date) between cast(#fromdate as date) and cast(#todate as date)
ORDER BY CreatedDate;
If you want find last 6 month date from a specific date, so you can set that date in #fromdate and #todate parameters like this,
set #fromdate = (select DATEADD(month, -6, #Yourdate))
set #todate = #Yourdate
Ok, I'm running query builder out of visual studio 2008. I'm trying to filter the results of the query by time; that is, I want to pull everything from the start of yesterday to noon yesterday.
I've been using GETDATE()-1 for yesterday, which pulls up a timestamp mm/dd/yyyy hh:mm:ss
however, it pulls the current time. to get it to run from the start of the day I appended the timestamp to remove the time itself, so it started at the beginning of the day:
convert(varchar(10), getdate()-1, 120)
so I'm using between to find the range, I have:
BETWEEN convert(varchar(10), getdate()-1, 120) AND // this is where I need it to cut off at noon.
I'm understanding that datetime is a data type here, so I tried subtracting the hours/minutes/seconds using date part, but datepart() only returns ints and doesn't affect the time.
thoughts? how do I get this to cut off at noon
Try this:
--Variables
declare #now datetime = getdate(),
#yesterday datetime
--Yesterday starting datetime
select #yesterday = convert(datetime, convert(date, dateadd(day,-1,#now)))
--Your query to filter between y'day start and y'day noon
--Note that between means inclusive boundary values. (or use >= and <=)
select * from yourTable
where dateCol between #yesteray and dateadd(hour,12,#yesterday)
DECLARE
#Min DATETIME
, #Max DATETIME
SELECT
#Min = DATEADD(DAY, -1, CAST(FLOOR(CAST(GETDATE() AS FLOAT)) AS DATETIME))
, #Max = DATEADD(HOUR, 12, CAST(FLOOR(CAST(GETDATE() AS FLOAT)) AS DATETIME))
SELECT *
FROM <Table> x
WHERE x.[Date] BETWEEN #Min AND #Max
SELECT * FROM T WHERE YourDate BETWEEN CAST(GETDATE()-1 As DATE) AND DATEADD(Hour, -12, CAST(CAST(GETDATE() As DATE) As DATETIME) )
Beware because BETWEEN will include lower and upper boundaries, so you can simply replace BETWEEN with x >= y and y < z if you don't want yesterday at 12:00 to be taken in account
between DateAdd(day, -1, cast(getdate() as date)) and DateAdd(hour, -12, cast(getdate() as date))
Edit: As mentioned in the comments, you can't use hours with a date, you have to cast it back to a datetime, thus:
between DateAdd(day, -1, cast(getdate() as date)) and DateAdd(hour, -12, cast(cast(getdate() as date) as datetime))
If you want to get the results from last 30 mins you need to use this. You can change MINUTE to HOUR too.
--Get now, hour and second included
DECLARE #NOW DATETIME = GETDATE()
--Get 30 mins from now
DECLARE #TranDate DATETIME
SET #TranDate = CONVERT(DATETIME, CONVERT(DATETIME, DATEADD(MINUTE,-30,#NOW)))
After That, add below code to your where statement,
AND TK.TransactionDate >= #TranDate
I have a stored procedure :-
CREATE procedure St_Proc_GetTimeEntryID
#userID int,
#timeEntryID int output
as begin
set nocount on;
SET #timeEntryID=0
DECLARE #TEMP INT
SET #TEMP=0
SELECT #TEMP=ProductionTimeEntryID
FROM production
WHERE ProductionTimeEntryID =
(SELECT MAX(ProductionTimeEntryID)
FROM production
where UserID=#userID
and (CalendarDate = (select GETDATE()))
and IsTaskCompleted=1 )
BEGIN
SET #timeEntryID=#TEMP
END
END
Here CalendarDate is column which containing Date As 06/26/201212:00PM format .
I want to compare the date part only with system date part (06/26/2012 = 06/26/2012) in my subquery which is
(SELECT MAX(ProductionTimeEntryID)
FROM production
where UserID=#userID
and (CalendarDate = (select GETDATE()))
and IsTaskCompleted=1 )
Please guide me what modification i ll do to get the result.
In SQL2K8;
... WHERE CAST(CalendarDate AS DATE) <= CAST(GETDATE() AS DATE)
(This will negate any index use on CalendarDate, alternatively bracket CalendarDate between CalendarDate >= CAST(CalendarDate AS DATE) AND < DATEADD(...)
The most efficient method (meaning fully able to utilize an index on CalendarDate, if one exists) is going to be, on SQL Server 2000/2005:
DECLARE #today SMALLDATETIME;
SET #today = DATEDIFF(DAY, 0, CURRENT_TIMESTAMP);
...
WHERE CalendarDate >= #today
AND CalendarDate < DATEADD(DAY, 1, #today);
If using SQL Server 2008+:
DECLARE #today DATE = CURRENT_TIMESTAMP;
...
WHERE CalendarDate >= #today
AND CalendarDate < DATEADD(DAY, 1, #today);
You can also use a direct cast in SQL Server 2008+, but I'm not 100% sure this is guaranteed to use an index on CalendarDate in all scenarios:
WHERE CONVERT(DATE, CalendarDate) = CONVERT(DATE, CURRENT_TIMESTAMP);
Because this casting does not work with other date/time data types, for consistency I much prefer the open-ended range technique, and definitely do not condone most of the scenarios where you perform implicit or explicit conversions on the column (since this usually means an index won't be used). I've ranted about this and several other date/time atrocities plenty at the following blog posts:
What do BETWEEN and the devil have in common?
Bad habits to kick : mis-handling date / range queries
Something like this?
(SELECT MAX(ProductionTimeEntryID)
FROM production
where UserID=412
and (convert(datetime, convert(varchar(100), CalendarDate, 106)) <= convert(datetime, convert(varchar(100), GETDATE(), 106)))
and IsTaskCompleted=1 )
Use convert function
In your case:
SELECT CONVERT(DATE,GETDATE(),101)
More specifically:
(SELECT MAX(ProductionTimeEntryID)
FROM production
where UserID=#userID
and (CONVERT(DATE,CalendarDate) = CONVERT(DATE,GETDATE()))
and IsTaskCompleted=1 )