MySQL format issue - mysql

I have a MySQL query:
SELECT DATE_FORMAT(CONVERT_TZ(`StartTime`, '+0:00', `timeZone`),'%b %d, %Y %h:%i %p') as start,
DATE_FORMAT(CONVERT_TZ(`endTime`, '+0:00', `timeZone`),'%b %d, %Y %h:%i %p') as end
which is giving output as
start = Nov 09, 2015 06:40 PM
end = Nov 09, 2015 07:10 PM
But I want the output in following format: Nov 09, 2015 06:40 PM EST instead of Nov 09, 2015 06:40 PM

Since you have the time zone already, you can just insert it into the second DATE_FORMAT() argument like this:
SELECT DATE_FORMAT(CONVERT_TZ(`StartTime`, '+0:00', `timeZone`),CONCAT'%b %d, %Y %h:%i %p ', timeZone)) as start,
DATE_FORMAT(CONVERT_TZ(`endTime`, '+0:00', `timeZone`), CONCAT('%b %d, %Y %h:%i %p ', timeZone)) as end
FROM appointments
WHERE CounselorID = 225 AND AvaliableStatus = '0' AND counselorsStatus = 0 AND Type = 2

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 get MMM dd yyyy hh:mm:ss format in sql server 2008

I want to get datetime as Jan 17 2013 4:34:59 with time in 24 hrs format
SELECT CONVERT(VARCHAR(12), SYSDATETIME(), 107) +' '+ CONVERT(VARCHAR(12), SYSDATETIME(), 108) AS [Mon DD, YYYY]

what is the problem of this mysql query?

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

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