Format date to include day of week - sql-server

I have a SQL Server 2012 query that converts date to VARCHAR
SELECT
CONVERT(VARCHAR(12), dbo.Download.Date_of_Download, 107) as Date_to_Display,
dbo.Download.Number_of_Computers
FROM dbo.Download
ORDER BY dbo.Download.Date_of_Download DESC
Below are results
Date_to_Display Number_of_Computers
-----------------------------------
Aug 14, 2014 240
Aug 13, 2014 519
Aug 12, 2014 622
Aug 11, 2014 2132
Aug 10, 2014 1255
Aug 09, 2014 3240
How do I include day of week, i.e. Saturday, Aug 09, 2014 ?

try this:
select datename(dw,getdate())
output:
------------------------------
Thursday
(1 row(s) affected)
using your query:
SELECT
Datename(dw, dbo.Download.Date_of_Download)+', '+CONVERT(VARCHAR(12), dbo.Download.Date_of_Download, 107) as Date_to_Display,
dbo.Download.Number_of_Computers
FROM dbo.Download
ORDER BY dbo.Download.Date_of_Download DESC

In SQL Server version 2012 and later, there is the FORMAT TSQL function that can do what you want. The "dddd" portion does day of the week:
SELECT
FORMAT(dbo.Download.Date_of_Download, 'dddd MMM dd, yyyy') as Date_to_Display,
dbo.Download.Number_of_Computers
FROM dbo.Download
ORDER BY dbo.Download.Date_of_Download DESC
MS docs: https://learn.microsoft.com/en-us/sql/t-sql/functions/format-transact-sql

Related

Looking Time windows for 24 Hr interval

I am executing below query in SQL Server 2012
print dateadd(day,datediff(day,1,getdate()),0)
print dateadd(day,datediff(day,0,getdate()),0)
and getting below result
Nov 9 2017 12:00AM
Nov 10 2017 12:00AM
But i am looking result like below
Nov 9 2017 12:00AM
Nov 10 2017 11:59PM
Please help !!
You would seem to want:
print cast(getdate() as date);
print dateadd(minute, -1, dateadd(day, 2, cast(getdate() as date)))
Or, alternatively:
print cast(getdate() as date);
print dateadd(minute, 2*24*60-1, cast(getdate() as date))

SQL DATEADD(weekday, 1, date) does not seem to work?

Here is my query:
SELECT ID AS 'securityid'
, date
, DATEADD(DW, 1, adate) AS 'lagged_date_v2'
, DATEADD(DAY, 1, adate) AS 'lagged_date_v1'
, aclose AS 'previous_close'
FROM mytable
WHERE adate BETWEEN '20170101 09:00' AND '20170630 18:00' AND
ID = 100056;
Here are the output from the code above:
I compared the results to DATEADDby day and by weekday and it returns the same results.
Column adate is just typical date, we can tell Jan 28, 2017 is Saturday. However, on this row, both lagged_date_v1 and lagged_date_v2 return Jan 28. If I use DATEADD by weekday correctly, I should see Jan 30 instead Jan 28, right?
My sql server version is 2008.
If I use DATEADD by weekday correctly, I should see Jan 30 instead Jan
28, right?
Wrong. In SQL Server, weekday doesn't mean days that aren't weekend days. It means which day of the week (1-7) is the day in question based on the current datefirst setting.

How to convert sql string datetime

How do I convert/cast the string April 04, 2016 12:00:00 to DATETIME in sql server 2005?
UPDATE I would like to compare dates like this :
SELECT *
from ZeroAndLackingRequest
WHERE request_date BETWEEN CONVERT(DATETIME,'April 04, 2016 12:00:00',103)
AND CONVERT(DATETIME,'April 04, 2016 11:59:59',103)
but it is not working. Any Ideas why?
SELECT cast('April 04, 2016 12:00:00' AS datetime)
OR
SELECT CONVERT(DATETIME,'April 04, 2016 12:00:00')
For more details
Select * from [ZeroAndLackingRequest] Z where Z.request_date = CONVERT(datetime, 'April 04, 2016 12:00:00', 120)
remember I choose 120 style you can chooose 103 or any other please see below link.
For a full discussion of CAST and CONVERT, including the different date formatting options, see the MSDN Library Link below:
http://msdn.microsoft.com/en-us/library/ms187928.aspx
If you want to compare
Select * from [ZeroAndLackingRequest] Z
where cast(Z.request_date as datetime) >= cast('April 04, 2016 12:00:00' as datetime)
and cast(Z.request_date as datetime) < cast('April 04, 2016 11:59:59' as datetime)

Query last 12 months of data where year and month are separate columns

I'm trying to figure out a way to query the last 12 completed months of data, so it would go up to last month and ignore this month.
I've done this before when the table had a date column, but this new table I'm working with separates the date into two month and year columns.
How would I go about this in SQL Server 2012?
Thanks in advance.
SAMPLE DATA:
CalendarYear CalendarMonth TotalSales
2014 3 35.00
2014 4 220.00
2015 2 243.00
2015 5 17.93
2015 6 216.36
2015 10 370.93
2015 12 350.00
2016 1 116.75
2016 2 13.78
DESIRED OUTPUT (assuming current time is in February 2016):
CalendarYear CalendarMonth TotalSales
2015 2 243.00
2015 5 17.93
2015 6 216.36
2015 10 370.93
2015 12 350.00
2016 1 116.75
I've assumed the Day part is always the 1st of the month.
WHERE
CONVERT(DATETIME, CalendarYear + '-' + CalendarMonth + '- 01') >= DATEADD(mm, -12, GETDATE())
The above assumes the columns are strings. Below I've done a version where the columns are numbers.
WHERE
CONVERT(DATETIME, CAST(CalendarYear AS NVARCHAR(4)) + '-' + CAST(CalendarMonth AS NVARCHAR(2)) + '-01') >= DATEADD(mm, -12, GETDATE())
You can just apply same code/logic to year and month you want data.
where to_date(year || month, 'YYYYMM')
between add_months(trunc(sysdate, 'MM'), -12) and trunc(sysdate)

Get last 6 months monthname.month number and Years in simple select statement

How to get last 6 months month name, month number and Years in simple select statement in sqlserver .
The no of months is 6, and is fixed
12 Dec 2015
11 Nov 2015
10 Oct 2015
9 Sep 2015
8 Aug 2015
7 Jul 2015
6 Jun 2015
This should handle year end boundaries
say, if the current month is Feb 2016, the result should give 2015 months.
2 Feb 2016
1 Jan 2016
12 Dec 2015
11 Nov 2015
10 Oct 2015
9 Sep 2015
8 Aug 2015
You can do it with the following:
SELECT MONTH(DATEADD(mm, -m, GETDATE())) AS m,
LEFT(DATENAME(mm, DATEADD(mm, -m, GETDATE())), 3) AS n,
YEAR(DATEADD(mm, -m, GETDATE())) AS y
FROM (VALUES (0),(1),(2),(3),(4),(5),(6)) t(m)
Output:
m n y
12 Dec 2015
11 Nov 2015
10 Oct 2015
9 Sep 2015
8 Aug 2015
7 Jul 2015
6 Jun 2015
Try this
;with cte as
(
select 0 as num
union all
select num+1 from cte where num<6
)
select month(dates),datename(month,dates),year(dates)
from
(
select dateadd(mm,-num,datadd(dd,1,eomonth(getdate(),-1))) as dates
from cte
) A
SQL FIDDLE DEMO
select datepart(m,GETDATE()) MonthNumber,left(datename(month,GETDATE()),3) as Month,year(GETDATE()) as Year union all
select datepart(m,DATEADD(month,-1,GETDATE())) MonthNumber,left(datename(month,DATEADD(month,-1,GETDATE())),3) as Month,year(DATEADD(month,-1,GETDATE())) as Year union all
select datepart(m,DATEADD(month,-2,GETDATE())) MonthNumber,left(datename(month,DATEADD(month,-2,GETDATE())),3) as Month,year(DATEADD(month,-2,GETDATE())) as Year union all
select datepart(m,DATEADD(month,-3,GETDATE())) MonthNumber,left(datename(month,DATEADD(month,-3,GETDATE())),3) as Month,year(DATEADD(month,-3,GETDATE())) as Year union all
select datepart(m,DATEADD(month,-4,GETDATE())) MonthNumber,left(datename(month,DATEADD(month,-4,GETDATE())),3) as Month,year(DATEADD(month,-4,GETDATE())) as Year union all
select datepart(m,DATEADD(month,-5,GETDATE())) MonthNumber,left(datename(month,DATEADD(month,-5,GETDATE())),3) as Month,year(DATEADD(month,-5,GETDATE())) as Year union all
select datepart(m,DATEADD(month,-6,GETDATE())) MonthNumber,left(datename(month,DATEADD(month,-6,GETDATE())),3) as Month,year(DATEADD(month,-6,GETDATE())) as Year
The above query will work for most of the RDBMS.
For SQL Server specific use the below query.
SELECT MONTH(DATEADD(month, -month, GETDATE())) AS MonthNumber ,
LEFT(DATENAME(MONTH, DATEADD(month, -month, GETDATE())), 3) AS MonthName,
YEAR(DATEADD(month, -month, GETDATE())) AS Year
FROM ( VALUES (0), (1), (2), (3), (4), (5),(6) ) t ( month )
Try DATEADD:
Select * FROM Table where YourDate>=DATEADD(m, -6, GETDATE())

Resources