Convert utc string to date SQL - mysql

I've a column sample_date in form of string as 200912301111230000000000 (UTC Time).How can I convert it from string to datetime in form of yyyymmdd using SQL select statement?

As you only want yyyymmdd, which is the first 8 chacters of your string, it is enough to simply use LEFT
I added the ST_TO_DATE so that you can see hw a conversionto a Date column could work
SELECT LEFT('200912301111230000000000',8),STR_TO_DATE(LEFT('200912301111230000000000',8),'%Y%m%d')
LEFT('200912301111230000000000',8) | STR_TO_DATE(LEFT('200912301111230000000000',8),'%Y%m%d')
:--------------------------------- | :-------------------------------------------------------
20091230 | 2009-12-30
db<>fiddle here
So it would loke like this
SELECT LEFT(Your_Column,8) FROM Your_Table

Related

TO_DATE to YYYYMMDD from DD-MON-YY

I have some data in the following date format.
'27-SEP-97' i.e DD-MON-YY
Now I want to convert this to YYYYMMDD. I am using the following script to convert this.
TO_CHAR(TO_DATE(CHD_DATE_FIRST_ACT,'DD-MON-YY'),'YYYYMMDD')
but this is giving me the following output.
20970927
I want this data to be in YYYYMMDD format, such that the output looks like this- 19970927
If '27-SEP-97' is a string (which is what your words suggest), then such a combination of TO_this and TO_that might do the job:
SQL> with test as (select '27-SEP-97' datum from dual)
2 select to_char(to_date(datum, 'dd-mon-rr', 'nls_date_language = english'), 'yyyymmdd') result
3 from test;
RESULT
--------------------------------------------------------------------------------
19970927
SQL>
--USE RR instead of YY in your query.
select TO_CHAR(TO_DATE('27-SEP-97','DD-MON-RR'),'YYYYMMDD') from dual;
You can make use of mysql replace and str_to_date functions
replace takes 3 arguments as show below
replace(string,find_string,replace_with)
you should simply replace - with a blank string
if your str is '27-SEP-97' to get the output as 19970927 execute the following in mysql
select replace(str_to_date('27-SEP-97','%d-%b-%Y'),'-','') as date;
output
+----------+
| date |
+----------+
| 19970927 |
+----------+
%b is used to mention abbreviated month name
click the below links to know more about mysql date and time functions
Mysql Data and Time functions

Format the date and time in MySQL default format

I have a MySQL table with forex tick data
pair tick_date_time price_low price_high
------- --------------------- --------- ------------
USD/JPY 20091201 00:00:00.628 86.280000 86.286000
USD/JPY 20091201 00:00:00.722 86.280000 86.289000
USD/JPY 20091201 00:00:00.741 86.281000 86.289000
USD/JPY 20091201 00:00:01.130 86.283000 86.289000
USD/JPY 20091201 00:00:01.131 86.283000 86.289000
The date/time in this table is not formatted in MySQL default format. I use the query to format the data
SELECT DATE_FORMAT(t.tick_date_time, "%Y%m%d %H:%i:%s") FROM table AS t
This returns NULL. Any idea where I'm wrong? I also like to extract milliseconds if possible.
You are looking for the function str_to_date(), not date_format():
SELECT str_to_date(t.tick_date_time, '%Y%m%d %H:%i:%s')
date_format() takes a date and produces a string. str_to_date() takes a string and converts it to a date.

SELECT BETWEEN dates not working for year - MySQL

I'm using this query to select data between dates:
SELECT id, date FROM table WHERE date BETWEEN '09/10/2015' and '09/23/2015';
I'm getting these results:
1 09/11/2015
2 09/13/2015
3 09/16/2015
4 09/21/2015
5 09/21/2015
6 09/21/2015
7 09/22/2015
8 09/23/2015
But when I change the date to one year before, instead of showing all these data again, It's returning a result ignoring the year:
SELECT id, date FROM table WHERE date BETWEEN '09/15/2014' and '09/23/2015';
Result:
3 09/16/2015
4 09/21/2015
5 09/21/2015
6 09/21/2015
7 09/22/2015
8 09/23/2015
What am I missing?
Edit:
Date column datatype: varchar.
It looks like you are committing a sin . . . storing date values as strings. This is bad. SQL has built-in data types for dates and a wealth of functions.
So, you need to convert the data, using str_to_date():
SELECT id, date
FROM table
WHERE str_to_date(date, '%m/%d/%Y') BETWEEN '2015-09-10' and '2015-09-23';
MySQL doesn't know that you intend for some string column to be a date, so all the comparisons are done as strings. You should also get used to using the ISO standard YYYY-MM-DD format for representing dates.
Try this ....
SELECT id, date FROM table WHERE date BETWEEN STR_TO_DATE('09/10/2015','%d/%m/%Y') and STR_TO_DATE('09/23/2015','%d/%m/%Y');

Distinct TIME in SQL

My table has this field:
`processeddate` int(14) DEFAULT NULL,
a datetime converted by strtotime .
The content is like 1401847266
How do I select distinct this field which is an integer not a datetime?
I want to select per day.
Like the sum of member field ,registered, on 20140603 .
On database:
Member | Processed_Date
A | 1401847266 //real date 20140604
B | 1401846898 //real date 20140604
C | 1401844093 //real date 20140604
D | 1401788219 //real date 20140603
E | 1401788219 //real date 20140603
RESULT that I want to be displayed :
Date | Member
20140603 | 2
20140604 | 3
PS.
The processeddate is in datetime format converted into time integer, not date format.
You can use FROM_UNIXTIME() to convert the integer value to a datetime value and then use functions like YEAR() and MONTH() and DAY() on it. However when using a function on a column MySQL can't use an index anymore. Best is, to convert the other value like this:
...
WHERE your_column BETWEEN UNIX_TIMESTAMP('2014-06-04 00:00:00') AND UNIX_TIMESTAMP('2014-06-04 23:59:59');
EDIT:
What you want to do can be achieved with this query:
select
date(from_unixtime(Processed_Date)) as date, count(*) as member
from your_table
group by date
see it working live in an sqlfiddle

I am trying to select all the values from a table using date in SQL Server 2008

I am trying to select all the values from a table using date in SQL Server 2008. The problem is the date column in the DB is in Datetime format. But the thing is I have to select all the values from the database by using date
I used the following query
select *
from Table
where subdate = '2012-12-12'.
It won't return any value....
My table is like this
Subdate val1 val2 val3 name
-------------- ----- ----- ---- -----
2012-12-12 12:32:12.000 2 1 2 ben
2012-12-27 15:17:32.533 2 1 2 yen
2012-12-27 15:20:06.660 2 1 2 sun
Thanks in advance.........
select * from Tble where subdate>='20121212' and subdate <'20121213'
would be my usual recommended approach(*). If it's a parameter, it would be:
select * from Tble where subdate>=#Parm and subdate <DATEADD(day,1,#Parm)
'2012-12-12' and '2012/12/12' can be subject to weird conversion issues, depending on your language/date settings on the server. '20121212' will always be converted as YYYYMMDD.
(*) I don't use BETWEEN for such comparisons, because you either include two midnights , or you have to calculate the last moment before midnight of the following day. Which would be at 23:59:59.997 if you're using datetime, but suddenly changes to being something else for datetime2.
SELECT *
FROM Table
WHERE CAST(subdate AS DATE)='2012-12-12'
Raj
The date in the table also contain Time so you have to convert that date to the format suitable for the date you enter.
You have to use: Sql Server Date Formats
select * from Tble where CONVERT(VARCHAR(10), subdate, 111) = '2012/12/12'
Demo SQLFiddle