TSQL First of last month - date only - sql-server-2008

This is using SQL2008 R2.
I am trying to get the first of the last month in these two formats:
'06/01/2013'
'2013-06-01'
I found this code, but it includes the time:
SELECT DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE())-1, 0)
My stored procedures want only the date.
I tried this to trim off the time, but it didn't work.
SELECT RIGHT(DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE())-1, 0),8)

What you want appears to be different character style outputs which are described here..
SELECT CONVERT(CHAR(10), DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE())-1, 0), 111)
SELECT CONVERT(CHAR(10), DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE())-1, 0), 120)

Your question essentially boils down to how to convert a datetime to a date. Like this:
CONVERT(date, someDate)
So, combine that with the code you already have to get the date you want:
SELECT CONVERT(date, DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE())-1, 0))

try this, it has no string conversions ...
declare #dt DateTime = getDate()
Select DateAdd(mm,-1, DateAdd(day, 1-day(#dt),
datediff(day, 0, #dt)))
or ...
declare #dt DateTime = getDate()
Select DateAdd(mm,-1, DateAdd(day, 0,
datediff(day, day(#dt)-1, #dt)))
or, even simpler,....
declare #dt DateTime = getDate()
select dateadd(mm, datediff(mm, 1, #dt)-1, 0)
or, making it allow for arbitrary months back...
declare #dt DateTime = getDate()
declare #monthsBack tinyInt = 3
select dateadd(mm, datediff(mm, 1, #dt) - #monthsBack, 0)

Try this:
convert(varchar, DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE())-1, 0), 101)
Here is another fun way... I am typing from my nexus, please forgive this
Select cast(cast(getdate() as int) as datetime),getdate()

Related

MS SQL Server Query Date functions to MySQL

I have this MS SQL Server query:
SELECT DATEPART(MONTH, si.score_date),
MAX(si.score_value)
FROM score_info AS si
WHERE si.score_date >= DATEADD(MONTH, -3, DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0))
GROUP BY DATEPART(MONTH, si.score_date);
Now I'm having a hard time converting this query to MySQL especially this part:
DATEADD(MONTH, -3, DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0))
So far here's what I've converted from MS SQL Server query to MySQL query:
DATEPART(MONTH, si.score_date)
to
EXTRACT(MONTH FROM si.score_date)
Please help me as I'm on my learning stage. Thank you so much.
This T-SQL expression:
DATEADD(MONTH, -3, DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0))
Gives you the first day of the month, 3 months ago (as of today March 30th, that's December 1st).
In MySQL you can get the same result like so:
DATE_FORMAT(CURRENT_DATE, '%Y-%m-01') - INTERVAL 3 MONTH
Also, EXTRACT(MONTH FROM si.score_date) can be shorten as MONTH(si.score_date) (the MONTH() function exists in both MySQL and TSQL, by the way). But note that presumably, you want to keep the year part as well, in case your data spreads over severl year.
I would write your query as:
SELECT
DATE_FORMAT(si.score_date, '%Y-%m-01') year_month,
MAX(si.score_value) max_score
FROM score_info AS si
WHERE si.score_date >= DATE_FORMAT(CURRENT_DATE, '%Y-%m-01') - INTERVAL 3 MONTH
GROUP BY DATE_FORMAT(si.score_date, '%Y-%m-01'));

Getting the month to show date name rather than number when pulling each month in the year

I am pulling data that lists each month of the year but the month column states the first day of each year, how can I make this show the full month name?
Here is some of what I have. I read similar questions and tried to add in what it stated everywhere but I couldn't get it work, help?
SELECT a.MonthDate
,Sum(a.ScrapPcs) / (Sum(a.GrossPcs)*1.00) As ScrapPct
,0.05 As ScrapTarget
,sum (a.GrossPcs) As 'Total Parts Cast'
,sum (a.ScrapPcs) as 'Total Parts Scrap'
,CASE
WHEN Sum(a.ScrapPcs) / (Sum(a.GrossPcs)*1.00) <= 0.05 THEN 1
WHEN Sum(a.ScrapPcs) / (Sum(a.GrossPcs)*1.00) >= 0.9*.05 THEN -1
ELSE 0
END
AS Status
FROM
(
SELECT DATEADD(month, DATEDIFF(month, 0, b.CreationProdDate), 0) As MonthDate
,Count(b.BOOKING_ID) As ScrapPcs
, 0 As GrossPcs
FROM dbo.CPC_BOOKING b
WHERE b.CreationProdDate Between DATEADD(yy, DATEDIFF(yy, 0, GetDate()), 0) AND DateAdd("d", -1, GetDate())
and CPC_BookingTyp_ID in (2,8)
and ManufacturingPlant in (90002604,90017699, 90017705)
GROUP BY DATEADD(month, DATEDIFF(month, 0, b.CreationProdDate), 0)
UNION ALL
SELECT DATEADD(month, DATEDIFF(month, 0, n.ProductionDate), 0) As MonthDate
,0 As ScrapPcs
,Sum(n.Quantity) As GrossPcs
FROM [NORIS].[dbo].[MDL_Data_Ultimate] n
WHERE n.ProductionDate Between DATEADD(yy, DATEDIFF(yy, 0, GetDate()), 0) AND DateAdd("d", -1, GetDate())
and n.ManufacturingPlantGroup = 1
GROUP BY DATEADD(month, DATEDIFF(month, 0, n.ProductionDate), 0)
) a
group by a.MonthDate
Thanks in advance, anything helps
For MySQL:
https://www.w3resource.com/mysql/date-and-time-functions/mysql-monthname-function.php
Use the MONTHNAME function as per the example on that webpage.
SELECT MONTHNAME(a.MonthDate)
For MS SQL Server:
If you want the month name use this in the derived table:
SELECT datename(month,DATEADD(month, DATEDIFF(month, 0, b.CreationProdDate), 0)) As MonthDate
That is, you want to apply the datename(month, date)function to the first column of both unioned queries.

How to find data from a previous month in sql starting from day 1 in the SELECT in sql?

SELECT
team, team_name, COUNT(*) AS c
FROM
[helpdesk].[dbo].[inpc] , helpdesk.dbo.teams
WHERE
team = teams.id
AND st IN (4, 5)
AND CONVERT(VARCHAR(10), dd_1, 102) > DATEADD(month, -1, GETDATE())
GROUP BY
team, team_name
Your question is really a painful SQL Server date olympics question. We can phrase the requirement of the previous month as a date on or after the first of the previous month and strictly before the first of the current month.
SELECT
team,
team_name,
COUNT(*) as c
FROM [helpdesk].[dbo].[inpc], helpdesk.dbo.teams
WHERE
team = teams.id AND
st IN (4,5) AND
dd_1 >= DATEADD(month, DATEDIFF(month, 0, DATEADD(month, -2, GETDATE())), 0) AND
dd_1 < DATEADD(month, DATEDIFF(month, 0, DATEADD(month, -1, GETDATE())), 0)
GROUP BY
team,
team_name;
Also, as others have pointed out, you should use explicit join syntax. I'm not sure your current join even makes sense, as it appears to be a cross join.

SQL to show min,max during timerange where name =(x)

Im trying to establish a basic report function.
The tables contain basic columns like this
ID, timestamp, value, key, Query, JSON
The only unique identifier is in the key colum
Theres half a million rows
I'd like it to report back this basically
SELECT min(value) as begin, max(value) as end FROM `key`WHERE NAME='Key1'
TIMERANGE='LastMonth'
Could someone please assist with the syntax of this or the concepts im missing
Thankyou and much appreciated
Try this
SELECT min(value) as begin, max(value) as end FROM `key`
WHERE NAME='Key1' and
timestamp>=date_add(current_date(),interval -1 month) and timestamp<=current_date()
SELECT min(value) as begin, max(value) as end FROM `key`
WHERE ID='Key1'
AND timestamp >= DATEADD(month, DATEDIFF(month, -1, getdate()) - 2, 0)
AND timestamp <= DATEADD(ss, -1, DATEADD(month, DATEDIFF(month, 0, getdate()), 0))
OR
SELECT ID, min(value) as begin, max(value) as end FROM `key`
WHERE timestamp >= DATEADD(month, DATEDIFF(month, -1, getdate()) - 2, 0)
AND timestamp <= DATEADD(ss, -1, DATEADD(month, DATEDIFF(month, 0, getdate()), 0))
GROUP BY ID

Get MAX minutes from query in SQL Server

I have a query that gets all the records for today. With the query below I get the difference from the time a ticket was issued till the current time. This gets the total waiting time.
I want to get the MAXWaitTime from my query result.
SELECT
DATEDIFF(minute, Issued, GETDATE()) AS MaxWaitTime
FROM
tblData
WHERE
(DATEADD(day, DATEDIFF(day, 0, Issued), 0) = DATEADD(day, DATEDIFF(day, 0, GETDATE()), 0))
This works fine I get the minutes back but for several records. I only want the highest.
My result:
32
50
25
I want the result to be the highest only:
50
You need to use the max function to get the highest value. Check this link
SELECT Max(DATEDIFF(minute,Issued,GETDATE()))AS MaxWaitTime
from tblData
WHERE
(DATEADD(day, DATEDIFF(day, 0, Issued), 0) = DATEADD(day, DATEDIFF(day, 0, GETDATE()), 0))
Try Following Query :
SELECT max(DATEDIFF(minute,Issued,GETDATE())) AS MaxWaitTime
from tblData
WHERE (DATEADD(day, DATEDIFF(day, 0, Issued), 0) = DATEADD(day, DATEDIFF(day, 0, GETDATE()), 0))