SQL Date Col - Use DATE or Varchar? - sql-server

I have very simple code, but the issue is that when I convert a date column to Varchar(10), then I am not fetching distinct dates and sort is not working as desired; when I use DATE format, then I am fetching distinct dates and date sort is working, but I get datetime - 2017-02-01 12:00:00 AM (I don't want the time part)
Here is the code:
SELECT DISTINCT
--CONVERT(varchar(10), DATEADD(month, DATEDIFF(month, 0, (MyDate)), 0), 101) AS MonthValue,
CONVERT(DATE, DATEADD(month, DATEDIFF(month, 0, (MyDate)), 0)) AS MonthValue
FROM dbo.Mytable
WHERE MyDate >= GETDATE()-366
ORDER BY MonthValue DESC

Related

How to find the last day of a Quarter, Half Year and Year of current Year in SQL Server?

I would like to get the last day of a Quarter, Half Year and Year of current Year in SQL Server. Let's say the Current Date is 12-May-2020, the Quarter end date will be 30-June-2020, Half Year date will be 30-June-2020 and Year end date will be 31-December-2020.
I am able to get the last date of Quarter and Year using DATEADD and DATEDIFF built-in functions but unable to get the last date of Half Year.
Any help would be appreciated.
Since you tag datediff, this is a solution using dateadd / datediff
; with dates as
(
select [date] = convert(date, '2020-01-01')
union all
select [date] = dateadd(month, 1, [date])
from dates
where [date] < '2020-12-01'
)
select [date],
dateadd(quarter, datediff(quarter, 0, [date]) + 1, -1) as [Last Day of Quarter],
dateadd(quarter, datediff(quarter, 0, [date]) / 2 * 2 + 2, -1) as [Last Day of Half Year],
dateadd(year, datediff(year, 0, [date]) + 1, -1) as [Last Day of Year]
from dates
db<>fiddle

SQL Server - Date String

Can you please help me create an sql statement that will get the previous date and concatenate that date in this format:
'2016-05-05 00:00:00'
I already know the sql function to get the previous date but I don't know yet on how to get the time in the expected format. My query is something like this
Select *
from table
where
transaction_date between '2016-05-05 00:00:00' and '2016-05-05 23:59:59'
So I need 00:00:00 and 23:59:59 to be concatenated in the date.
You don't need the times at all. Just don't use BETWEEN and use >= and < for the next day, like this:
SELECT *
FROM table
WHERE transaction_date >= '2016-05-05'
AND transaction_date < '2016-05-06'
Or if you only have the single date value:
DECLARE #date DATETIME = '2016-05-05'
SELECT *
FROM table
WHERE transaction_date >= #date
AND transaction_date < DATEADD(DAY, 1, #date)
The added benefit of doing it this way is that you also don't miss out on times that occur in the last second of the day as your original query does. For example 2016-05-05 23:59:59.001
Edit: From the comments below
To get yesterdays date, get today's date and subtract a day from it:
DECLARE #date DATETIME = DATEADD(DAY, -1, CAST(GETDATE() AS DATE))
Now use the logic as mentioned above.
This function:
DATEADD(dd, DATEDIFF(dd, 0, GetDate()), 0)
...will turn 2016-05-05 18:18:231 into 2016-05-05 00:00:000. From there it is easy to get full date boundaries by adding and subtracting days:
DECLARE #Date DATETIME
,#YesterdayStartDate DATETIME
,#TodayStartDate DATETIME
SET #Date = GetDate()
SET #TodayStartDate = DATEADD(dd, DATEDIFF(dd, 0, #date), 0)
SET #YesterdayStartDate = DATEADD(dd, -1, #TodayStartDate)
SELECT #Date, #YesterdayStartDate, #TodayStartDate
SELECT *
FROM Table
WHERE
transaction_date >= #YesterdayStartDate AND
transaction_date < #TodayStartDate
This will give you the result with transaction_date on yesterday.
SELECT
*
FROM
TABLE
WHERE
transaction_date BETWEEN dateadd(DAY, datediff(DAY, 1, GETDATE()), 0) AND dateadd(DAY, 1, dateadd(DAY, datediff(DAY, 1, GETDATE()), 0))
Compare date with out time part . ie below code.
Select * from table
where CONVERT(VARCHAR(8), transaction_date , 112) = CONVERT(VARCHAR(8), getdate(), 112)

SQL create a list of dates based on current date

I have a column of dates that range from 2015-06 to 2013-04. The date range will grow as I put in more data. How do I write a query where the date is always a X amount of months before the current date?
date
2015-06
2015-05
2015-04
2015-03
2015-02
for example I want it to be:
Select *
From dbo.name
Where date in (X months ago from current date)
if todays date is 2015-12 and I want 3 months ago, I want the query to be:
Select *
From dbo.name
Where date in ('2015-11','2015-10','2015-09')
Thanks
Select *
From dbo.name
Where CAST([Date] + '-01' AS DATE) >= DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()) -3, 0)
Since your date strings are in yyyy-mm format you can do string comparison using < > =. You just need to convert the current date to varchar(7) using style 126 (yyyy-mm-ddThh:mi:ss.mmm)
For Example
DECLARE #CurrentDate DATETIME = GETDATE()
DECLARE #XMonthsAgo INT = 3
DECLARE #Dates TABLE ([date] varchar(7))
INSERT INTO #Dates VALUES
('2015-09'),('2015-08'),('2015-07'),('2015-06'),('2015-05'),('2015-04'),('2015-03'),
('2015-02'),('2015-01'),('2014-12'),('2014-11'),('2014-10'),('2014-09'),('2014-08'),
('2014-07'),('2014-06'),('2014-05'),('2014-04'),('2014-03'),('2014-02'),('2014-01')
SELECT *
FROM #Dates d
WHERE [date] >= CONVERT(VARCHAR(7), DATEADD(month, -#XMonthsAgo, #CurrentDate), 126)
AND [date] < CONVERT(VARCHAR(7), #CurrentDate, 126)
Result
date
----
2015-07
2015-06
2015-05

Displaying dates values for current date in SQL Server

I am trying to filter out the values based on the date which should be within today's date. I am using the below query, however the query is not working as expected.
This is what I am using in SQL Server.
select values
from table
where date between DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0)
and GETDATE()
I am expecting the date to be filtered for values between '2015-03-18 00:00:00' AND '2015-03-18 23:59:59' if I am executing the query on 18th March 2015.
Please guide.
It's much better to use a >= and < than BETWEEN:
SELECT *
FROM Table
WHERE
date >= CAST(CAST(GETDATE() AS DATE) AS DATETIME)
AND date < DATEADD(DAY, 1, CAST(CAST(GETDATE() AS DATE) AS DATETIME))
CAST(CAST(GETDATE() AS DATE) AS DATETIME) will get you the current date without the time part that is 2015-03-18 00:00:00
DATEADD(DAY, 1, CAST(CAST(GETDATE() AS DATE) AS DATETIME)) will get you the next day, again without the time part.
So in turn, your WHERE condition would be:
WHERE
date >= '2015-03-18 00:00:00'
AND date < '2015-03-19 00:00:00'
Alternatively, you can use this:
SELECT *
FROM Table
WHERE
date >= DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()), 0) -- beginning of this day
AND date < DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()) + 1, 0) -- beginning of next day
For more date calculations, refer to this article.
As You are Expecting value between '2015-03-18 00:00:00' AND
'2015-03-18 23:59:59. so, you can simply compare only date part.
you can Use Cast()/Convert():
SELECT *
FROM Table
WHERE
cast(date as date) =cast(getdate() as date)
It included 2015-03-18 00:00:00' AND '2015-03-18 23:59:59'

How to get a time and Date Separately?

Using SQL Server 2005
Table1
Time
12/05/2009 08:30:49
12/05/2009 17:00:00
13/05/2009 21:00:00
...,
Above Table Both values are same column, Here I want to separate column like Date and Time
Expected Output
Date Time
12/05/2009 08:30:49
12/05/2009 17:00:00
13/05/2009 21:00:00
You can retrieve separate date and time parts like:
SELECT
CONVERT(VARCHAR(10),DateField,101) as DatePart,
CONVERT(VARCHAR(10),DateField,108) as TimePart
FROM YourTable
For more information, see the CONVERT documentation.
The code snippet above returns the DataPart and TimePart as a varchar. If you're interested in retrieving them as a datetime field, try:
SELECT
DATEADD(D, 0, DATEDIFF(D, 0, DateField)) as DatePart,
DATEADD(day, -DATEDIFF(D, 0, DateField), DateField) as TimePart
FROM YourTable
The following query returns the date and time respectively for the current system date/time:
SELECT
CONVERT(VARCHAR(8) , GETDATE() , 108) AS [time],
CONVERT(VARCHAR(10) , GETDATE() , 101) AS [date]
select
CONVERT(VARCHAR(8), GETDATE(), 108) as [time],
CONVERT(VARCHAR(8), GETDATE(), 111) as [date]
See this reference for all formats
Just a slight modification to pmarflee's answer, because you appear to be wanting the date as dd/mm/yy rather than mm/dd/yy:
SELECT CONVERT(VARCHAR(8) , GETDATE() , 108) AS [time], CONVERT(VARCHAR(10) , GETDATE() , 103) AS [date]

Resources