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
Related
From my sql query I'm getting output as datetime.datetime(2020, 9, 22, 0, 0)
query = '''SELECT checkin_date FROM `table1`
WHERE checkin_date BETWEEN %s AND %s'''
cursor.execute(query,(startDate, endDate)
results = cursor.fetchall()
#results:
#[(datetime.datetime(2020, 9, 22, 0, 0), datetime.datetime(2020, 9, 24, 0, 0))]
for res in results:
## When I print type I get correct result
print(type(res[0]) ## <type 'datetime.datetime'>
##when I compare with another datetime.date (currentDate variable)
if res[0] < currentDate:
## I get error `TypeError: can't compare datetime.datetime to datetime.date` *which is expected*
## But when I use .date()
if res[0].date() < currentDate:
## I get `TypeError: can't compare datetime.date to unicode`
I tried converting currentDate to datetime.datetime, but still doesn't work. Can't seem to figure out what's the issue here.
To force your query to spit out the date format you want, change it to this:
SELECT DATE_FORMAT(checkin_date, '%Y-%c-%d')
FROM table1
WHERE DATE(checkin_date) BETWEEN %s AND %s
To make it able to use an index on your checkin_date column, change it to this.
SELECT DATE_FORMAT(checkin_date, '%Y-%c-%d')
FROM table1
WHERE checkin_date >= DATE(%s)
AND checkin_date < DATE(%s) + INTERVAL 1 DAY
Try this
splitting a datetime column into year, month and week
SELECT Year(checkin_date), Month(Checkin_date), Day(Checkin_date),
FORMAT(GETDATE(),'HH'), FORMAT(GETDATE(),'mm')
FROM table1
WHERE (CAST(checkin_date AS DATE) BETWEEN '2018-01-01' AND '2020-01-01')
Note: Use 'HH' for 24 hours format and 'hh' for 12.
I want to arrive at an output like 2011-Q4 (Financial Yr-Qtr)
I can do this by:
CASE -- Results: 2011-Q4 (Financial Yr-Qtr)
WHEN MONTH(MyDate) BETWEEN 1 AND 3 THEN concat((YEAR(MyDate) - 1), '-', 'Q4')
WHEN MONTH(MyDate) BETWEEN 4 AND 6 THEN concat((YEAR(MyDate) - 1), '-', 'Q1')
WHEN MONTH(MyDate) BETWEEN 7 AND 9 THEN concat((YEAR(MyDate) - 0), '-', 'Q2')
WHEN MONTH(MyDate) BETWEEN 10 AND 12 THEN concat((YEAR(MyDate) - 0), '-', 'Q3')
END AS FYrQtr
But can the same output be achieved without using CONCAT? (I only have 2008 at work; CONCAT arrived in 2012).
Thanks.
In this particular case you can simply use the + operator plus some cast():
CASE -- Results: 2011-Q4 (Financial Yr-Qtr)
WHEN MONTH(MyDate) BETWEEN 1 AND 3 THEN cast(YEAR(MyDate) - 1 as char(4)) + '-Q4'
WHEN MONTH(MyDate) BETWEEN 4 AND 6 THEN cast(YEAR(MyDate) - 1 as char(4)) + '-Q1'
WHEN MONTH(MyDate) BETWEEN 7 AND 9 THEN cast(YEAR(MyDate) - 0 as char(4)) + '-Q2'
WHEN MONTH(MyDate) BETWEEN 10 AND 12 THEN cast(YEAR(MyDate) - 0 as char(4)) + '-Q3'
END FYrQtr
(but note the use of the cast() function: the concat() does implicit conversion from int to char types, while the + operator requires that the left part and the right part are char types)
I am stuck in one place.
I am using DateDiff in Ms Access it is giving me proper output, like
StartDate is 10-Sep-2016
EndDate is 15-Oct-2016
Total Days which I will get is 35
& months will i get is 1 Month
DateDiff('d',StartDate,EndDate)
**But I want output as 2 months if it is exeeded the 30 days.
if it is 61 days then 3 months & so on.
**IIFFF days diffrence is
29 Days then output should be 1 months
30 Days then output should be 1 months
32 Days then output should be 2 months
60 Days then output should be 2 months
62 Days then output should be 3 months**
Will that be possible in the DateDiff in MsAccess
or is there any other function available so that i can achieve the same output.**
You can do this using conditional logic. Perhaps something like this:
select iif(DateDiff('d', StartDate, EndDate) > 30,
DateDiff('d',StartDate,EndDate) & " days",
"2 months"
)
Your logic that anything exceeding 30 days is "2 months" seems strange. Normally, I think the logic would look like this:
select iif(DateDiff('d', StartDate, EndDate) > 30,
DateDiff('d', StartDate, EndDate) & " days",
DateDiff('m', StartDate, EndDate) & " months"
)
will this logic suffice to modify your SQL function?
Public Function FN_GET_MONTH(iDays As Long, Optional iDaysInMonth As Long = 30)
If (iDays / iDaysInMonth) > iDays \ iDaysInMonth Then
FN_GET_MONTH = (iDays \ iDaysInMonth) + 1
Else
FN_GET_MONTH = (iDays \ iDaysInMonth)
End If
End Function
?FN_GET_MONTH(29) = 1
?FN_GET_MONTH(31) = 2
?FN_GET_MONTH(60) = 2
?FN_GET_MONTH(80) = 3
?FN_GET_MONTH(91) = 4
you can have this public function and use it in your SQL code like
FN_GET_MONTH(DateDiff("d", StartDate, EndDate))
This query seems to give the results you seek:
SELECT
StartDate,
EndDate
numDays,
((numDays - 1) \ 30) + 1 AS numMonths
FROM
(
SELECT
StartDate,
EndDate,
DateDiff("d", StartDate, EndDate) AS numDays
FROM YourTable
)
It gives me
numDays numMonths
------- ---------
...
29 1
30 1
31 2
32 2
...
59 2
60 2
61 3
62 3
...
It seems like your minimum count of months for a positive count of days is 1, thus:
MonthCount = Sgn(DateDiff("d",StartDate,EndDate)) + DateDiff("m",StartDate,EndDate)
Edit
For a 30-day cut that will produce your example output, use this simple formula in your query:
MonthCount: (DateDiff("d",[StartDate],[EndDate])-1)\30+1
I have a number such as this : 840106
I need to do the following :
Change the number to date add - and flip the number : 06-01-84
add 28 to the last 2 digit that the date will be : 06-01-12
84 + 16 = 00 + 12 = 12
number is always changing sometimes it cab be 850617 , but format is always same add - and add 28 last 2 digit.
any ideas how to help me here ?
Here is a sqlite solution:
create table t( c text);
insert into t (c) values(990831);
insert into t (c) values(840106);
insert into t (c) values(800315);
insert into t (c) values(750527);
insert into t (c) values(700923);
insert into t (c) values(620308);
select c, substr(c,5,2) || '-' || substr(c,3,2) || '-' ||
case when (substr(c,1,2) + 28) < 100 then (substr(c,1,2) + 28)
else case when ((substr(c,1,2) + 28) - 100) < 10 then '0' || ((substr(c,1,2) + 28) - 100)
else ((substr(c,1,2) + 28) - 100)
end
end
from t;
For formatting you can use
http://www.w3schools.com/sql/func_date_format.asp
For adding days to the date you should take a look at date_add() function
mysql> SELECT DATE_ADD('1998-01-02', INTERVAL 31 DAY);
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-add
Assuming date is the name of the column containing your date:
DATE_FORMAT(DATE_ADD(STR_TO_DATE(date, %y%m%d), INTERVAL 28 YEAR), %d-%m-%y);
What this does is first formats the string into a date, then adds 28 years, then converts back to string with the new format.
SQLite is a lot tricker with this, you'll need to use substrings.
substr(date,5) || "-" || substr(date,3,4) || "-" || CAST(CAST(substr(date,1,2) as integer) + 28 - 100) as text
I'm not too experienced with SQLite so the casting may be a bit weird.
Here is a t-sql solution that you can use and migrate to mysql.
declare #myDate as char(8) = '840106';
declare #y as char(2), #m as char(2), #d as char(2);
set #y = LEFT (#myDate, 2);
declare #yi as int = Convert (int, #y);
IF #y between 30 and 99 ----------- pick a cut-off year
SET #yi = (28 - (100-#yi));
SET #y = CONVERT(char, #yi)
set #m = SUBSTRING(#myDate, 3, 2);
set #d = SUBSTRING(#myDate, 5, 2);
SET #myDate = #d + '-' + #m + '-' + #y;
PRINT #myDate;
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