I have a date range:
SET #declared_start_datetime = '11/02/2015 07:00:00'
SET #declared_end_datetime = '11/09/2015 18:00:00'
This was working until I realized it was pulling all records from 7:00 am on 2nd of November until 6:00 pm on November 9th.
What I need it to do is pull all records from those dates but exclude Saturday/Sunday and only between the hours of 7:00 and 18:00 for Monday-Friday.
I am using SQL Server going through a linked MYSQL server so I am using OPENQUERY. Here is an example of what I have so far.
DEClare #session varchar(max) = 'SELECT * FROM database.session WHERE CREATIONTIMESTAMP between ''''' + convert(nvarchar(50),#converted_start_datetime, 120) + ''''' and ''''' + convert(nvarchar(50),#converted_end_datetime, 120) + ''''''
SET #session = 'SELECT * INTO ##session FROM OPENQUERY(mxie,''' + #session + ''')'
exec(#session)
How do I exclude Saturday's and Sundays and adjust it to only pull the time where the business is open?
Edit:
DEClare #session varchar(max) = 'SELECT * FROM database.session WHERE ((CREATIONTIMESTAMP between ''''' + convert(nvarchar(50),#converted_start_datetime, 120) + ''''' and ''''' + convert(nvarchar(50),#converted_end_datetime, 120) + ''''')
AND (TIME(CREATIONTIMESTAMP) between ''''' + convert(nvarchar(50),#business_opens_time, 108) + ''''' and ''''' + convert(nvarchar(50),#business_closes_time, 108) + '''''))'
SET #session = 'SELECT * INTO ##session FROM OPENQUERY(mxie,''' + #session + ''')'
exec(#session)
select * from ##session
drop table ##session
This doesn't exclude Saturdays or Sundays yet but it is currently returning 0 results. if I change the AND to OR it will return results but not what I am looking for.
I am looking for Monday - Friday from 7:00 am until 6:00 pm for each of the days.
Related
I am using SQL Server 2008 R2.
I have one field storing time as integer value.
I want to convert integer to time as follows.
IntegerValue TimeValue
-------------------------
1 00:01 AM
11 00:11 AM
123 01:23 AM
541 05:41 AM
1317 13:17 PM
Please reply.
Thanks.
SELECT
CONVERT(VARCHAR, coulmn / 100) + ':' + RIGHT('00' + CONVERT(VARCHAR, column% 100),2)
FROM YourTable
Try like this,
It is almost similar to #Mukund answer
SELECT c AS IntegerValue
,CONVERT(VARCHAR, c / 100) + ':' + RIGHT('00' + CONVERT(VARCHAR, c % 100), 2) AS TimeValue
FROM (
VALUES (1)
,(11)
,(123)
,(541)
,(1317)
) t(c)
I have a date that I want to display in SQL Server like this:
Date=`2015-12-08`
O/p=Dec 15(I need output like this)
How can I do it? I tried like
SELECT
LEFT(DATENAME(MONTH, GETDATE()), 3) + ' ' + DATENAME(YEAR, GETDATE()) AS [Mon YYYY]`
But it returns output like Dec 2015 but I need it to be Dec 15
try
SELECT CONVERT(CHAR(4), GETDATE(), 100) + CONVERT(CHAR(4), GETDATE(), 120)
update :
Your can try if you want only jan 15
SELECT RIGHT(CONVERT(varchar, "yourdate",6),6)
try this
SELECT LEFT(CAST(Date as varchar(20)), 3) + '-' + RIGHT(CAST(YEAR(Date) as CHAR(4)), 2) as MonYr
FROM YourTable
This is what you are looking for
SELECT RIGHT(CONVERT(varchar, GetDate(),6),6)
Ok,
This is a little tricky, I am trying to replace the dates in a SQL Query results with a standard date, based on the month.
For example:
Any dates that are in July get 20140701
August gets 20140801
I could use a case statement:
Case
When Datepart(mm, TxnDate) = 1 and Datepart(yy, TxnDate) = 2014 then TxnDate = 20140101
etc...
but that could get very long as the database goes back 5 years and the result sets cover different periods then.
Any quick suggestions would be greatly appreciated.
Thanks,
declare #mydate datetime
select #mydate = GETDATE()
select cast(datepart(yy,#mydate) as varchar(4)) + RIGHT('0' + RTRIM(MONTH(#mydate)), 2) + '01'
select #mydate = GETDATE() - 10
select cast(datepart(yy,#mydate) as varchar(4)) + RIGHT('0' + RTRIM(MONTH(#mydate)), 2) + '01'
should print 20140701 and 20140601
If you simply want to set every TxnDate to the first of its month, you can do this:
TxnDate = DATEADD(month, DATEDIFF(month, 0, TxnDate), 0)
If your requirement is more complicated than that, you will need to explain.
Try Something like this
SELECT REPLACE (CONVERT(VARCHAR(8), TxnDate, 112),SUBSTRING(CONVERT(VARCHAR(8), TxnDate, 112),7,8),'01') AS [YYYYMMDD]
hope this is what you are looking for
in one of the table the date column values are like below
date
25052008112228
26052008062717
table name is transaction
i tried using the below query but its throwing error
select * from transaction where date between '2012-01-06' and '2012-06-30'
select * from transaction where date between '2012/01/06' and '2012/06/30'
give me a solution.
The problem is that the [date] column doesn't contain a date in a format that will be automatically converted to an appropriate datetime value - it doesn't even contain a supported format value. So you're left shredding the text using string operations:
declare #Transactions table (TDate char(14))
insert into #Transactions (TDate) values
('25052008112228'),
('26052008062717')
select CONVERT(datetime,
SUBSTRING(TDate,5,4) + '-' +
SUBSTRING(TDate,3,2) + '-' +
SUBSTRING(TDate,1,2) + 'T' +
SUBSTRING(TDate,9,2) + ':' +
SUBSTRING(TDate,11,2) + ':' +
SUBSTRING(TDate,13,2))
from
#Transactions
Results:
2008-05-25 11:22:28.000
2008-05-26 06:27:17.000
You could wrap the CONVERT/SUBSTRING operations into a UDF, if you need to perform this kind of conversion often. Of course, ideal would be to change the column definition to store a genuine datetime value - almost all datetime issues arise when people treat them as text.
(Note, I've renamed both the table and the column, since using reserved words is usually a bad idea)
Your query could be something like:
;with converted as (
select *,CONVERT(datetime,
SUBSTRING([Date],5,4) + '-' +
SUBSTRING([Date],3,2) + '-' +
SUBSTRING([Date],1,2) + 'T' +
SUBSTRING([Date],9,2) + ':' +
SUBSTRING([Date],11,2) + ':' +
SUBSTRING([Date],13,2)) as GenuineDate
from [Transaction]
)
select * from converted where GenuineDate between '20120106' and '20120630'
(Note that I've also changed the date literals in the final query to a safe format also)
-- asp time stamp
select * from [transaction] where
cast(SUBSTRING([date],5,4) + '-' + SUBSTRING([date],3,2) + '-' +
SUBSTRING([date],1,2) + ' ' + SUBSTRING([date],9,2) +
':' + SUBSTRING([date],11,2) + ':' +
SUBSTRING([date],13,2) as datetime)
between '2008-05-26' and '2012-01-06'
-- unix epoch time
select * from [transaction] where [date]
between DATEDIFF( SECOND, '01-01-1970 00:00:00', '2012-01-06' )
and DATEDIFF( SECOND, '01-01-1970 00:00:00', '2012-06-30')
Hallo. How can I get the date of last friday of current month with mysql?
Thanks in advance.
edit. Hi Stefan. This is what I've done
set #ldom = dayofweek(last_day(curdate()));
select
case
when #ldom = 7 then last_day(curdate()) - interval 1 day
when #ldom = 6 then last_day(curdate())
when #ldom = 5 then last_day(curdate()) - interval 6 day
when #ldom = 4 then last_day(curdate()) - interval 5 day
when #ldom = 3 then last_day(curdate()) - interval 4 day
when #ldom = 2 then last_day(curdate()) - interval 3 day
else last_day(curdate()) - interval 2 day
end as last_friday
but I'd like to know if there is a smarter way.
EDIT. I made some test bases on samplebias answer to find last monday,tuesday and so on of a specific month.
These are the correct queries.
-- last sunday of month
set #data = '2011-04-01';
select str_to_date(last_day(#data) - ((7 + weekday(last_day(#data)) - 6) % 7),"%Y%m%d") -- 2011-04-24
-- last saturday
set #data = '2011-04-01';
select str_to_date(last_day(#data) - ((7 + weekday(last_day(#data)) - 5) % 7),"%Y%m%d") -- 2011-04-30
-- last friday
set #data = '2011-04-01';
select str_to_date(last_day(#data) - ((7 + weekday(last_day(#data)) - 4) % 7),"%Y%m%d") -- 2011-04-29
-- last thursday
set #data = '2011-04-01';
select str_to_date(last_day(#data) - ((7 + weekday(last_day(#data)) - 3) % 7),"%Y%m%d") -- 2011-04-28
-- last wednesday
set #data = '2011-04-01';
select str_to_date(last_day(#data) - ((7 + weekday(last_day(#data)) - 2) % 7),"%Y%m%d") -- 2011-04-27
-- last tuesday
set #data = '2011-04-01';
select str_to_date(last_day(#data) - ((7 + weekday(last_day(#data)) - 1) % 7),"%Y%m%d") -- 2011-04-26
-- last monday
set #data = '2011-04-01';
select str_to_date(last_day(#data) - ((7 + weekday(last_day(#data))) % 7),"%Y%m%d") -- 2011-04-25
Hope that it helps someone else.
Thanks again to samplebias. ;)
Here is a simplified version using just date math:
SELECT LAST_DAY(NOW()) - ((7 + WEEKDAY(LAST_DAY(NOW())) - 4) % 7);
Depending on how NOW() gets evaluated (once or twice per statement), you might want to still wrap this in a function and store the result of NOW() into a variable, and then use the variable for the LAST_DAY(var) call, to avoid a race condition where the month rolls over between calls to NOW().
-- Today is 05 April 2013
-- Get Last Friday from MySQL
SELECT DATE_FORMAT(LAST_DAY(NOW()) - ((7 + WEEKDAY(LAST_DAY(NOW())) - 4) % 7), '%Y-%m-%d') last_friday;
-- Output
last_friday
-------------
2013-04-26