Converting date format produce by excel to SQL - mysql

Asking for any ideas to convert this kind of date in SQL from May-15-2020 18:03 to 'yyyyMMddHHmiss' or 'yyyyMMdd'.
I am trying this query
select from_unixtime(unix_timestamp('May-15-2020 16:03', 'MM-dd-yyyy
HH:mi'), 'yyyyMMdd') from dual
but it wont work.

Use the right right function STR_TO_DATE, and use a format that matches your date rather than something scraped from a previous answer/blog.
Reference manuals of date and time functions are very useful for solving these basic problems.

Related

How to Select Data Between Two Dates and Times in SQL Server?

why this query gives me the wrong output although I am trying to print data between '01-May-2022' and '30-May-2022';
why is this query giving me apr data?
It appears that you are storing your dates as text. You should not be doing this, and the best long term fix is to make datee a proper date column. As a short term fix, you may use STR_TO_DATE:
SELECT datee
FROM customer_shopping_details_tbl
WHERE STR_TO_DATE(datee, '%d-%b-%Y') BETWEEN '2022-05-01' AND '2022-05-30';
Thank you all ....
I did a mistake while saving the date, I had saved the date in the string format. That is the reason that the query does not show proper output
What I learned/point to remember
-MySQL date format is yyyy-mm-dd
-Save your date in the form of date NOT IN String
go through the comments for the better idea
I have saved my data in the form of String so i have fired the following query and is working now
Good evening,
Maybe this could help:
SELECT login,datetime FROM log where datetime between
to_date(’01-jan-2004,’mm/dd/yyyy’) and
to_date(’21-jan-2004′,’mm/dd/yyy’)
order by datetime DESC;

Format date sql

Good day , i'm kind of struggling at the moment and this might be really easy for you , but i'm really new in sql .
I'm trying to change the format of a date from 2021-10-05 to Tue-05-Oct . in a htm file using SQL
I'm struggling cause i do not really understand how to use the convert or formatdate with an object (Select date.birthday as object) .
I'm a bit lost :(
You can format the date using the MySQL DATE_FORMAT() function.
For example:
SELECT DATE_FORMAT(`myDate`, '%a-%d-%b') as textDate from `myTable`
which will return a date in the form Tue-05-Oct.
You could also do this directly in crystal reports by creating a formula field as follows.
totext({ReplaceWithDataFieldToFormat},"ddd-dd-MMM")

How to convert a string to date and extract values in Access query

I'm using Access DB 2007 - 2010; I've tried to import many CSV files but the timestamp column keeps failing to import correctly.
So I linked all of the CSV's to an Access DB and I'm trying to query all of the tables.
I'm trying to extract the year and day of the year from the time stamp (which is currently a string)
I'm trying to combine the Format with datepart functions and it keeps failing. (it just says error in the table)
The format function by itself works but I can't combine it with anything.
I'm basically trying to do this:
select datepart("y", Format(gmt, "dd-mmm-yyyy hh:nn:ss")) as DOY from Table1;
but it fails. I've also tried CDate and DateValue in different combinations but it all fails.
Does anyone know how to get this to work?
UPDATE
The format function isn't doing anything. The text remains the same no matter how I try to format it.
Here's a datetime sample: 05-Dec-2008 13:40:01.955
Access can't cope with the milliseconds in your date strings.
Use Left() to exclude them and feed the resulting substring to CDate().
SELECT CDate(Left(gmt, 20)) AS date_from_string
FROM Table1;
Once you have a valid Date/Time value, you can use Year(<Date/Time value>) or DatePart("yyyy", <Date/Time value>) to extract the year. And DatePart("y", <Date/Time value>) will give you the day of the year.
Just solve this issue, here is my code for your reference:
update tablename
set date=cdate(format(left(gmt,4)&"-"&right(gmt,2),"yyyy-mm"))

Calculating age in conditional split transformation

I've a requirement to split a table contents based on the age and I've a column DOB which is in string format and is of type '19910930'.I've used the following command and it works fine when run
DATEDIFF( YY,CONVERT(date,[DOB]),CONVERT(date, GETDATE()))
and the same gives an error when I try it as condition in Conditional Split transformation as
DATEDIFF( YY,CONVERT(date,[DOB]),CONVERT(date, GETDATE()))<=25
I even tried a Derived Column transformation with the same expression and that too ain't working.
Could you please help me as where I'm missing out in this?
--Vijay
try converting in this way:
DATEDIFF("YY",(DT_DATE)( SUBSTRING(#[User::DOB] ,1,4)+"-"+SUBSTRING (#[User::DOB] ,5,2)+"-"+SUBSTRING (#[User::DOB] ,7,2)) ,GETDATE())
DateDiff by year will not work when the day is between the first of the year and the [DOB]. As long as your [DOB] is in form YYYYMMDD. You can use this formula to get the age...
select
CurrentAge = ((convert(varchar(8), getdate(), 112) - cast([DOB] as int)) / 10000)
the same formula in an SSIS expression would be very clunky with a bunch of DatePart calls to format the current date [System::ContainerStartTime] into the YYYYMMDD form and a conversion of [DOB] into DT_I4. I'd advise to do the calulation in the SQL select if possible.
Thanks Jayvee and dotjoe...I couldn't get it done either way But I instead used your suggestion to calculate age as a new column in the SQL itself while generating Source and could get the required result using condition split.
DATEDIFF(YY,CONVERT(DATE,DOB),CONVERT(DATE,GETDATE())) as Age
I hope there should be a way this can be done in Derived Column though..
Vjai

Format date in mysql query that uses cast

I've got this as the select part of my query:
SELECT cast(cast(exp_channel_titles.edit_date as char(14)) as datetime) AS Edit_Date
That takes data from a db in this format 20130501092128 and returns it in this format 2013-05-01 09:21:28
I can only assume it is some kind of magic as i don't fully understand how this works tbh.
But, i need to change the format of the date that it spits out to this format: %d/%m/%Y %k:%i:%s
I can honestly say i have no idea how to do this in that query, i've tried adding it as a param to datetime (is that even a mysql function?!?) but no joy and many other poor attempts that i wont go into.
If anyone can help, i'd be hugely grateful!
MySql automatically converts 20130501092128 to a date and time field, even if it is a VARCHAR or a INT, and you can just use this:
SELECT DATE_FORMAT(exp_channel_titles.edit_date, '%d/%m/%Y %k:%i:%s')
Please see fiddle here.
You can change output format using DATE_FORMAT() function from MySQL. Here is the documentation post about it.
https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-format
You can change the output format into whatever format you want, but if you recieve that data into an application, modifies it and return that data to server (editing a row for example). Remember to reformat it into a valid date for MySQL.
If you dont know how to do it, just have to do this into your query:
SELECT DATE_FORMAT(cast(cast(exp_channel_titles.edit_date as char(14))
as datetime), '%e/%m/%Y %k:%i:%s') AS Edit_Date