what is the problem of this mysql query? - mysql

SELECT
*
FROM
table_temp
WHERE
install_date < NOW()
AND install_date > DATE_FORMAT(2011 - 06 - 16, "%Y-%m-%d")

The problem resides on theis line:
install_date > DATE_FORMAT(2011 - 06 - 16, "%Y-%m-%d")
The 1st variable should be a string of a date
For example:
SELECT DATE_FORMAT('2007-10-04 22:23:00', '%H:%i:%s');
Or in your case:
install_date > DATE_FORMAT('2011 - 06 - 16', "%Y-%m-%d")
See MySQL DOC

The value 2011 - 06 - 16 needs to be wrapped up in quotes

Related

Error in executing dynamic SQL Query with table variable in SQL Server 2008

These are the following parameters & table variable used for executing dynamic sql query:
Declaration
DECLARE #sQuery VARCHAR(MAX)
DECLARE #FYear NVARCHAR(10)='2016-2017'
DECLARE #TYear NVARCHAR(10)='2016-2018'
DECLARE #CLAIMSUM TABLE
(
ClaimType NVARCHAR(MAX),
JAN DECIMAL(19,6),
FEB DECIMAL(19,6),
MAR DECIMAL(19,6),
APR DECIMAL(19,6),
MAY DECIMAL(19,6),
JUN DECIMAL(19,6),
JUL DECIMAL(19,6),
AUG DECIMAL(19,6),
SEP DECIMAL(19,6),
OCT DECIMAL(19,6),
NOV DECIMAL(19,6),
DEC DECIMAL(19,6),
TOTAL DECIMAL(19,6)
)
Dynamic SQL Query
SET #sQuery=N'SELECT C1.ClaimType,
SUM(C1.JAN) AS ''JAN '+#FYear+'-'+#TYear+''',SUM(C1.FEB) AS ''FEB '+#FYear+'-'+#TYear+''',SUM(C1.MAR) AS ''MAR '+#FYear+'-'+#TYear+''',
SUM(C1.APR) AS ''APR '+#FYear+'-'+#TYear+''',SUM(C1.MAY) AS ''MAY '+#FYear+'-'+#TYear+''',SUM(C1.JUN) AS ''JUN '+#FYear+'-'+#TYear+''',
SUM(C1.JUL) AS ''JUL '+#FYear+'-'+#TYear+''',SUM(C1.AUG) AS ''AUG '+#FYear+'-'+#TYear+''',SUM(C1.SEP) AS ''SEP '+#FYear+'-'+#TYear+''',
SUM(C1.OCT) AS ''OCT '+#FYear+'-'+#TYear+''',SUM(C1.NOV) AS ''NOV '+#FYear+'-'+#TYear+''',SUM(C1.DEC) AS ''DEC '+#FYear+'-'+#TYear+''',
SUM(C1.TOTAL) AS ''TOTAL''
FROM #CLAIMSUM C1 GROUP BY C1.ClaimType';
EXECUTE #sQuery
Error
When executing this query I am getting following error:
The name 'SELECT C1.ClaimType,
SUM(C1.JAN) AS 'JAN 2016-2017',SUM(C1.FEB) AS 'FEB 2016-2017',SUM(C1.MAR) AS 'MAR 2016-2017',
SUM(C1.APR) AS 'APR 2016-2017',SUM(C1.MAY) AS 'MAY 2016-2017',SUM(C1.JUN) AS 'JUN 2016-2017',
SUM(C1.JUL) AS 'JUL 2016-2017',SUM(C1.AUG) AS 'AUG 2016-2017',SUM(C1.SEP) AS 'SEP 2016-2017',
SUM(C1.OCT) AS 'OCT 2016-2017',SUM(C1.NOV) AS 'NOV 2016-2017',SUM(C1.DEC) AS 'DEC 2016-2017',
SUM(C1.TOTAL) AS 'TOTAL'
FROM #CLAIMSUM C1 GROUP BY C1.ClaimType' is not a valid identifier.
I had replaced EXECUTE #sQuery with EXECUTE (#sQuery) and now it is working fine.

To get values from 2D array

I have used this query to retrieve the dates for one particular user's approved leaves -
LeaveRequest.where(user_id: 6).where(status: 1).pluck(:from_date, :to_date)
and I'm getting this array as result -
[[Mon, 12 Sep 2016, Fri, 16 Sep 2016], [Tue, 06 Sep 2016, Tue, 06 Sep 2016], [Thu, 01 Sep 2016, Fri, 02 Sep 2016], [Tue, 30 Aug 2016, Wed, 31 Aug 2016]]
what I want is to fetch all the dates as well as the dates between 12 Sep 2016 and 16 Sep, 2016 (13th 14th and 15th).
I am assuming you mean something like this
require 'date'
#This is to simulate your current Array
current_array = 5.times.map {|n [Date.new(2016,n+1,1).<<(1),Date.new(2016,n+1,1)]}
#map the 2 dates to a Range
new_array = current_array.map{|start_date,end_date| (start_date..end_date)}
new_array.first.class
#=> Range
Calling to_a on the Range will blow it out into all the dates between start_date and end_date
With a rails you could do something like
class LeaveRequest
def self.user_requested_ranges(user_id, status_id)
scoped.
where(user_id: user_id, status: status_id).
pluck(:from_date, :to_date).
map do |from_date, to_date|
#optionally to output the full Array in each Range you could use
#(from_date..to_date).to_a
(from_date..to_date)
end
end
end
Then call as
LeaveRequest.user_requested_ranges(6,1)

How to Display 4week per month with condition in sql

I have this logic but I cant figure out what to do to come up with the results. I want to achieve the results below, the idea is (Ex. month of October 2014), normally it has 5 weeks. If there is a 5th week and days is greater than 3 then consider whole week in the next month else add it to 4th week.
Input:
date range
from: 10-01-2014
to: 11-30-2014
Sample Output:
Date Customer week# Dates covered
10-01-2014 Cust01 1 Oct 01 - Oct 11, 2014
10-08-2014 Cust02 1 Oct 01 - Oct 11, 2014
10-17-2014 Cust02 2 Oct 12 - Oct 18, 2014
10-25-2014 Cust03 3 Oct 19 - Oct 25, 2014
10-31-2014 Cust01 4 Oct 26 - Oct 31, 2014
11-01-2014 Cust01 1 Nov 01 - Nov 08, 2014
11-28-2014 Cust02 4 Nov 23 - Nov 30, 2014
11-30-2014 Cust05 4 Nov 23 - Nov 30, 2014
Thanks
Use datepart(ww,[date]) to get week number in a year, then compare the week number by the begin date of a month.
Below is the detail approach to calculate for one date:
declare #dt date, #month_begin date, #month_end date, #week_of_month int
set #dt='20141031'
set #month_begin = convert(date,(convert(char(6),#dt,112)+'01'))
set #month_end = dateadd(day, -1, dateadd(month,1,#month_begin))
set #week_of_month = datepart(ww,#dt) - datepart(ww,convert(date,(convert(char(6),#dt,112)+'01'))) + 1
select
case when #week_of_month > 4 and datepart(dw,#month_end)>3
then 1
else #week_of_month
end as week,
case when #week_of_month > 4 and datepart(dw,#month_end)>3
then datepart(month,dateadd(month,1,#dt))
else datepart(month,#dt)
end as month

Date Time Format in SQL Server

Can any one guide me to get below date Format?
18th Mar 2014
I do see other date formats are supported. But nd, th after date is needed for me.
Maybe not the simplest way, but this should do it;
SELECT CAST(DATEPART(d, dt) AS NVARCHAR(2)) +
CASE DATEPART(d, dt) WHEN 1 THEN 'st' WHEN 2 THEN 'nd'
WHEN 3 THEN 'rd' WHEN 21 THEN 'st'
WHEN 22 THEN 'nd' WHEN 23 THEN 'rd'
WHEN 31 THEN 'st' ELSE 'th' END +
CAST(SUBSTRING(CONVERT(NVARCHAR(256), dt, 106), 3, 256) AS NVARCHAR(256))
AS [myDate]
FROM test;
An SQLfiddle to test with.
Try this
SELECT REPLACE(CONVERT(VARCHAR(11), GETDATE(), 106), Left(CONVERT(VARCHAR(11), GETDATE(), 106),2), Left(CONVERT(VARCHAR(11), GETDATE(), 106),2) + 'th') AS [DD Mon YYYY]
Fiddle Demo

how to work on mysql date fomat?

What is the problem with the mysql syntax
SELECT FORMAT(dCreatedDate,'YYYY-MM-DD') as date1 FROM tbl_book_self
I want to select the date from mysql database in this format,How to use this syntax
Did you mean to use DATE_FORMAT rather than just FORMAT?
Also the format needs to be specified using % notation so the corrected version of your example would be
DATE_FORMAT(dCreatedDate, '%Y-%m-%d')
You can find a list of the specifiers you can use in the format string in the MySQL documentation on Date and Time Functions.
mysql> SELECT DATE_FORMAT('2009-10-04 22:23:00', '%W %M %Y');
-> 'Sunday October 2009'
mysql> SELECT DATE_FORMAT('2007-10-04 22:23:00', '%H:%i:%s');
-> '22:23:00'
mysql> SELECT DATE_FORMAT('1900-10-04 22:23:00',
-> '%D %y %a %d %m %b %j');
-> '4th 00 Thu 04 10 Oct 277'
mysql> SELECT DATE_FORMAT('1997-10-04 22:23:00',
-> '%H %k %I %r %T %S %w');
-> '22 22 10 10:23:00 PM 22:23:00 00 6'
mysql> SELECT DATE_FORMAT('1999-01-01', '%X %V');
-> '1998 52'
mysql> SELECT DATE_FORMAT('2006-06-00', '%d');
-> '00'
http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_date-format
http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_str-to-date
SELECT DATE_FORMAT(dCreatedDate,'%Y-%m-%d') as date1 FROM tbl_book_self