Chris Jones
asked on
advanced sql function to select the most recent saturday
Hello
i need a sql statement or a function that can get the latest saturday of the month.
EXMPALE
the past saturday was the 17th
1. i want to run a report that goes to that saturday and readall after that saturday
i need a sql statement or a function that can get the latest saturday of the month.
EXMPALE
the past saturday was the 17th
1. i want to run a report that goes to that saturday and readall after that saturday
>>i need a sql statement or a function that can get the latest saturday of the month.
What if the current date is such that a Saturday within the current month has not occurred yet?
For example, if this were run on 4 Jun 2010, there will not have been a Saturday in June yet.
What if the current date is itself a Saturday?
What if the current date is such that a Saturday within the current month has not occurred yet?
For example, if this were run on 4 Jun 2010, there will not have been a Saturday in June yet.
What if the current date is itself a Saturday?
ASKER
good question matthewspatrick: do you know of a way i can work around something like that.
ASKER
if the day is saturday i will disable th feature of runing the report.
try this:
anydate - datepart(weekday, anydate)
this will give you last saturday before any given date...
anydate - datepart(weekday, anydate)
this will give you last saturday before any given date...
and if you have datetime and you want datepart only use
CAST(FLOOR(CAST( some_datetime AS FLOAT ) ) AS DATETIME)
so if we combine these two
CAST(FLOOR(CAST(
anydate - datepart(weekday, anydate)
AS FLOAT ) ) AS DATETIME) as Previous_Saturday_datepart _only
CAST(FLOOR(CAST( some_datetime AS FLOAT ) ) AS DATETIME)
so if we combine these two
CAST(FLOOR(CAST(
anydate - datepart(weekday, anydate)
AS FLOAT ) ) AS DATETIME) as Previous_Saturday_datepart
ASKER
i have a sql statement that i created but i get a syntax error befor AND
sqlstmt = "SELECT * FROM JobHistorySummary jhs LEFT JOIN JobHistoryDetail jhd ON jhs.JobHistoryID = jhd.JobHistoryID INNER JOIN JobHistoryDetailInfo jhi ON jhd.JobHistoryDetailID = jhi.JobHistoryDetailID WHERE '" & find & "' - datepart(weekday, '" & find & "') AND jhs.MediaSetName= '" & jobtype & "'"
'" & find & "' - ...
-->
" & find & " -
find is number right? if yes, no need to wrap it with '...
-->
" & find & " -
find is number right? if yes, no need to wrap it with '...
ASKER
sorry i was not clear with my varables
corect find is the current date and it gives th error in the sql at
datepart(weekday,'" & find & "')
between the weekday and the ,
corect find is the current date and it gives th error in the sql at
datepart(weekday,'" & find & "')
between the weekday and the ,
ASKER
NEW STATEMENT
sqlstmt = "SELECT * FROM JobHistorySummary jhs LEFT JOIN JobHistoryDetail jhd ON jhs.JobHistoryID = jhd.JobHistoryID INNER JOIN JobHistoryDetailInfo jhi ON jhd.JobHistoryDetailID = jhi.JobHistoryDetailID WHERE jhs.MediaSetName= '" & jobtype & "' AND '" & find & "' - datepart(weekday,'" & find & "') '"
ASKER
ok i see i need to pass it a single number EX date
ASKER
i get the error
Unclosed quotation mark before the character string ''.
STATEMENT
Unclosed quotation mark before the character string ''.
STATEMENT
"SELECT * FROM JobHistorySummary jhs LEFT JOIN JobHistoryDetail jhd ON jhs.JobHistoryID = jhd.JobHistoryID INNER JOIN JobHistoryDetailInfo jhi ON jhd.JobHistoryDetailID = jhi.JobHistoryDetailID WHERE jhs.MediaSetName= 'Weekly' AND '1' - datepart(weekday,'1') '"
ASKER
hello i tried the exmpale above and i get a syntax error near AS
'sqlstmt = "SELECT * FROM JobHistorySummary jhs LEFT JOIN JobHistoryDetail jhd ON jhs.JobHistoryID = jhd.JobHistoryID INNER JOIN JobHistoryDetailInfo jhi ON jhd.JobHistoryDetailID = jhi.JobHistoryDetailID WHERE jhs.MediaSetName= '" & jobtype & "' AND CAST(FLOOR(CAST('" & find & "'- datepart(weekday, '" & find & "')AS FLOAT ) ) AS DATETIME) as Previous_Saturday_datepart_only'"
check here
http://www.sqlusa.com/bestpractices/datetimeconversion/
for example if your passed date is in this format dd/mm/yyyy, you should use
convert('23/10/2016', datetime, 103) -- dd/mm/yyyy
so you should use
sqlstmt = "SELECT * FROM JobHistorySummary jhs LEFT JOIN JobHistoryDetail jhd ON jhs.JobHistoryID = jhd.JobHistoryID INNER JOIN JobHistoryDetailInfo jhi ON jhd.JobHistoryDetailID = jhi.JobHistoryDetailID WHERE jhs.MediaSetName= '" & jobtype & "' AND some_date <= convert('" & find & "', datetime, 103) - datepart(weekday, convert('" & find & "', datetime, 103))'"
http://www.sqlusa.com/bestpractices/datetimeconversion/
for example if your passed date is in this format dd/mm/yyyy, you should use
convert('23/10/2016', datetime, 103) -- dd/mm/yyyy
so you should use
sqlstmt = "SELECT * FROM JobHistorySummary jhs LEFT JOIN JobHistoryDetail jhd ON jhs.JobHistoryID = jhd.JobHistoryID INNER JOIN JobHistoryDetailInfo jhi ON jhd.JobHistoryDetailID = jhi.JobHistoryDetailID WHERE jhs.MediaSetName= '" & jobtype & "' AND some_date <= convert('" & find & "', datetime, 103) - datepart(weekday, convert('" & find & "', datetime, 103))'"
oops, one fix, extra ' at the end part...
sqlstmt = "SELECT * FROM JobHistorySummary jhs LEFT JOIN JobHistoryDetail jhd ON jhs.JobHistoryID = jhd.JobHistoryID INNER JOIN JobHistoryDetailInfo jhi ON jhd.JobHistoryDetailID = jhi.JobHistoryDetailID WHERE jhs.MediaSetName= '" & jobtype & "' AND some_date <= convert('" & find & "', datetime, 103) - datepart(weekday, convert('" & find & "', datetime, 103))"
and if you get any error, please post the value of sqlstmt here...
sqlstmt = "SELECT * FROM JobHistorySummary jhs LEFT JOIN JobHistoryDetail jhd ON jhs.JobHistoryID = jhd.JobHistoryID INNER JOIN JobHistoryDetailInfo jhi ON jhd.JobHistoryDetailID = jhi.JobHistoryDetailID WHERE jhs.MediaSetName= '" & jobtype & "' AND some_date <= convert('" & find & "', datetime, 103) - datepart(weekday, convert('" & find & "', datetime, 103))"
and if you get any error, please post the value of sqlstmt here...
ASKER
Hello
i get a incorrect syntax error near my varable find
find = '04-26-2010'
i get a incorrect syntax error near my varable find
find = '04-26-2010'
ASKER
my sql statement
sqlstmt = "SELECT * FROM JobHistorySummary jhs LEFT JOIN JobHistoryDetail jhd ON jhs.JobHistoryID = jhd.JobHistoryID INNER JOIN JobHistoryDetailInfo jhi ON jhd.JobHistoryDetailID = jhi.JobHistoryDetailID WHERE jhs.MediaSetName= '" & jobtype & "' AND some_date <= convert('" & find & "', datetime, 110) - datepart(weekday, convert('" & find & "', datetime, 110))"
ASKER
ALSO here is the sql statement with the values from the varables
"SELECT * FROM JobHistorySummary jhs LEFT JOIN JobHistoryDetail jhd ON jhs.JobHistoryID = jhd.JobHistoryID INNER JOIN JobHistoryDetailInfo jhi ON jhd.JobHistoryDetailID = jhi.JobHistoryDetailID WHERE jhs.MediaSetName= 'Weekly' AND '04-26-2010' <= convert('04-26-2010', datetime, 110) - datepart(weekday, convert('04-26-2010', datetime, 110))"
ASKER
new revised statement with displayed varale values
"SELECT * FROM JobHistorySummary jhs LEFT JOIN JobHistoryDetail jhd ON jhs.JobHistoryID = jhd.JobHistoryID INNER JOIN JobHistoryDetailInfo jhi ON jhd.JobHistoryDetailID = jhi.JobHistoryDetailID WHERE jhs.MediaSetName= 'Weekly' AND jhs.EndTim <= convert('04-26-2010', datetime, 110) - datepart(weekday, convert('04-26-2010', datetime, 110))'"
ASKER
any ideals?
OK, couple of issues....
What value do you get from:
select @@datefirst
If it is 7 then,
select convert(datetime, '04-26-2010', 101) - datepart(weekday, convert(datetime, '04-26-2010', 101))
will give you the previous saturday, if it is not seven then you do have to do a bit of extra maths.
Next, your SQL statement is not formatted correctly (need to fix those convert statements and there seems to be an extra signle quote at the end):
"SELECT * FROM JobHistorySummary jhs LEFT JOIN JobHistoryDetail jhd ON jhs.JobHistoryID = jhd.JobHistoryID INNER JOIN JobHistoryDetailInfo jhi ON jhd.JobHistoryDetailID = jhi.JobHistoryDetailID WHERE jhs.MediaSetName= 'Weekly' AND jhs.EndTim <= convert(datetime, '04-26-2010', 101) - datepart(weekday, convert(datetime, '04-26-2010', 101))"
Then using "find" replace 04-26-2010 with " & find & " (leave all single quotes etc as is)
If you can convert "find" to a yyyymmdd string then you should not have to worry about the convert (but convert is ver explicitly telling SQL how to read the date string and is a good thing to do).
What value do you get from:
select @@datefirst
If it is 7 then,
select convert(datetime, '04-26-2010', 101) - datepart(weekday, convert(datetime, '04-26-2010', 101))
will give you the previous saturday, if it is not seven then you do have to do a bit of extra maths.
Next, your SQL statement is not formatted correctly (need to fix those convert statements and there seems to be an extra signle quote at the end):
"SELECT * FROM JobHistorySummary jhs LEFT JOIN JobHistoryDetail jhd ON jhs.JobHistoryID = jhd.JobHistoryID INNER JOIN JobHistoryDetailInfo jhi ON jhd.JobHistoryDetailID = jhi.JobHistoryDetailID WHERE jhs.MediaSetName= 'Weekly' AND jhs.EndTim <= convert(datetime, '04-26-2010', 101) - datepart(weekday, convert(datetime, '04-26-2010', 101))"
Then using "find" replace 04-26-2010 with " & find & " (leave all single quotes etc as is)
If you can convert "find" to a yyyymmdd string then you should not have to worry about the convert (but convert is ver explicitly telling SQL how to read the date string and is a good thing to do).
Oh, that yyyymmdd business ? as an example, try:
select convert(datetime,'20100426 ') - datepart(weekday, '20100426')
select convert(datetime,'20100426
ASKER
i tried trhe script above and it slected everything.
Is that good or bad ?
ASKER
bad i only want to get the last saturday and up
EXMAPLE
friday,saturday,sun,monday ,tuesday,w ensday,thu rsday,frid ay
I ONLY NEED
saturday,sun,monday,tuesda y,wensday, thursday,f riday
EXMAPLE
friday,saturday,sun,monday
I ONLY NEED
saturday,sun,monday,tuesda
OK, then what do you get from:
select @@datefirst
select @@datefirst
Also, are we sure we want "<=" in that "where" clause for jhs.EndTim ? Looks like it should be >=
Typing up some dummy data, and setting datefirst to be 7 (ie select @@datefirst returns 7)
Then the following returns data from the 24th onwards :
SELECT *
FROM JobHistorySummary jhs
LEFT JOIN JobHistoryDetail jhd ON jhs.JobHistoryID = jhd.JobHistoryID
INNER JOIN JobHistoryDetailInfo jhi ON jhd.JobHistoryDetailID = jhi.JobHistoryDetailID
WHERE jhs.MediaSetName= 'Weekly'
AND jhs.EndTim >= convert(datetime,'20100426 ') - datepart(weekday, '20100426')
Then the following returns data from the 24th onwards :
SELECT *
FROM JobHistorySummary jhs
LEFT JOIN JobHistoryDetail jhd ON jhs.JobHistoryID = jhd.JobHistoryID
INNER JOIN JobHistoryDetailInfo jhi ON jhd.JobHistoryDetailID = jhi.JobHistoryDetailID
WHERE jhs.MediaSetName= 'Weekly'
AND jhs.EndTim >= convert(datetime,'20100426
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Awesome
http://msdn.microsoft.com/en-us/library/ms174420.aspx
note: what should happen if you run the code on a saturday?