Does anyone know a function I can use to extract the date after the last ',' or last '|' in a field?
Values are in the below format:
ON24_NEW_LEADS_29032020
Newsletter|20210803
Many thanks,
JD
Related
I have a table with a column start_date. Unfortunately, the datatype of the column is VARCHAR. Values in this column are either null or a string date in any format.
I have a requirement to extract the data from this table to a CSV file using BCP command. But while extracting, start_date value is expected to be in YYYYMMDD format in the output file.
The problem here is for some records, start_Date will be in DD/MM/YYYY format and for some others it may be in another format. For example : YYYY-MM-DD.
Is there any way so that I can convert the start_date value to the format YYYYMMDD irrespective of the actual format using the select query itself.
select convert(DATE,'13/12/2019',112) as 'AliasName'
This function will help u in fetching the data in the format of YYYYMMDD irrespective of how the format of ur input column is..This function uses 3 arguments 1.THE DATA TYPE WHICH U WANT TO CONVERT 2. YOUR INPUT COLUMN WHICH U WANT TO CONVERT 3.STYLE (style number is: Each format has its own style number for example if you give style number as 103 it will convert to dd/mm/yyyy or if 112 it will convert to YYYYMMDD ..there are many style You will find it if u search about convert function) . Hope this will help!
Hope this helps!!
select
*,
(CASE WHEN [Date] like '%/%' then TRY_CONVERT(datetime,[Date],103)
else [Date] END) as [New_DATE]
from TableName;
I imported a CSV file into MySQL. The CSV file contains Date/Time in the format 'mm/dd/yyyy hh:mm'.
It gets imported into MySQL only as text. So I did import it as text. I then created another field hoping to convert this text value to date time in this format 'mm-dd-yyyy hh:mm' OR in this format 'YYYY-mm-dd hh:mm'. But it does not work. Below is my code.
ALTER TABLE table1 ADD COLUMN StartDateNEW DATETIME;
SET SQL_SAFE_UPDATES = 0;
UPDATE table1 SET StartDateNEW = STR_TO_DATE(StartDate, '%m/%e/Y %H:%i');
SET SQL_SAFE_UPDATES = 1;
Sample Data:
Some more sample data:
I have been trying this for over an hour now. Can someone please help?
If you are loading data using the LOAD DATA INFILE syntax, it should be possible to handle conversion on the fly.
Assuming that your source csv file looks like:
StartDate, EndDate, Value
"1/10/2012 10:05", "1/11/2012 11:51", abc
"1/13/2012 08:00", "1/15/2012 09:01", abc
You can defined columns StartDate and EndDate as datetime datatype, and simply do:
LOAD DATA INFILE '/path/to/my/file.csv' INTO TABLE mytable
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY '\r\n' -- or '\n'
IGNORE 1 LINES
(#StartDate, #EndDate, Value)
SET
StartDate = STR_TO_DATE(#StartDate, '%c/%e/%Y %k:%i'),
EndDate = STR_TO_DATE(#EndDate, '%c/%e/%Y %k:%i')
;
NB: and if you are curently not using LOAD DATA INFILE... I would still recommend migrating your code to use it. This is the proper way to do this in MySQL, it works on all clients.... and it is very fast (see this link).
Based on your sample data, the correct format string for STR_TO_DATE is
%c/%e/%Y %k:%i
%c allows for single digit month numbers
%e allows for single digit day numbers
%Y four digit year
%k allows for single digit hours
%i two digit minutes
All the format strings are described in the manual for DATE_FORMAT.
Demo on dbfiddle
A quick hack is as follows:
Open CSV File in Excel
Select the date column
Change date format to yyyy-mm-dd using cell format popup
Save file
Import to MySQL
This was you don't need to import in text form and then try to convert the data into DateTime.
I have a date field create_at in Y-m-d format in my table. I want to extract only the year from that field.
I tried YEAR(create_at) which gives result in comma separated value.
Eg.
2017-5-12 outputs 2,017. I need without the comma.
The YEAR() will provide you integer output only.
SELECT YEAR("2017-06-15");
2017
Ref https://www.w3schools.com/sql/trymysql.asp?filename=trysql_func_mysql_year
I have a number of values stored as varchar, where values are stored in the string form:
10,000,000.00
I would like to convert this number to a double to display:
10000000.00 AND then SUM it with the rest of the values of that format
As these fields don't have types there may be the case where inputted data is of the form:
10000.00 and this should be accepted as well.
How would I go about converting the unstructured numbers.
I tried:
SELECT SUM(CAST(value as DECIMAL(10,2))) FROM meta_bs
WHERE bs_id = 361
In the above case:
10000000.00 becomes 10.00
Please assist, thanks!
You can use REPLACE function to remove the commas:
SELECT SUM(CAST(REPLACE(value, ',', '') as DECIMAL(10,2)))
FROM meta_bs
WHERE bs_id = 361
Please see fiddle here.
I have some data saved as txt file. I am saving the txt file as csv in order to import it into a database using my sql workbench.
what I am doing is the following:
LOAD DATA LOCAL INFILE '/path/to/csv/file.csv' INTO TABLE mytable FIELDS TERMINATED BY ',' ENCLOSED BY '"' lines terminated by '\n';
But one of my column is a date, and it is imported as 0000-00-00
How to import it in a good way ?
Edit
Here is what my csv contains:
id task hoursWorked begindate enddate
0 task1 15 11/17/2012 11/18/2012
1 task2 20 11/18/2012 11/20/2012
2 task3 20 12/4/2012 12/5/2013
3 task4 22 1/5/2013 1/7/2013
Please have a try with this one:
LOAD DATA LOCAL INFILE '/path/to/csv/file.csv'
INTO TABLE mytable
LINES TERMINATED BY '\n'
(id, task, hoursWorked, #var1, #var2)
SET begindate = STR_TO_DATE(#var1, '%m/%d/%Y'),
enddate = STR_TO_DATE(#var2, '%m/%d/%Y');
For more info see LOAD DATA and STR_TO_DATE
Note: I deleted the FIELDS TERMINATED BY ',' ENCLOSED BY '"' part, cause I neither see , nor " in your CSV. But if it works fine for you the way it is, feel free to revert :)
The default date format is YYYY-MM-DD:
mysql> SELECT ##date_format;
+---------------+
| ##date_format |
+---------------+
| %Y-%m-%d |
+---------------+
... thus MySQL won't recognise stuff like 11/17/2012 as a proper date. In theory, you should be able to change the default format, but I'm not sure it can be done in session scope and I wouldn't recommend to change it for the whole server. It's better to make the transformation yourself. The trick is to insert the value into a variable rather than a column.
Additionally, there're two other issues:
Your CSV file contains a header line.
Your fields are not separated by ,.
Assuming your file uses tabs as separators, the complete command would be:
LOAD DATA LOCAL INFILE '/path/to/csv/file.csv'
INTO TABLE mytable
FIELDS TERMINATED BY '\t' ENCLOSED BY '"' LINES TERMINATED BY '\n'
IGNORE 1 LINES
(id, task, hoursWorked, #tmp_begindate, #tmp_enddate)
SET begindate = STR_TO_DATE(#tmp_begindate, '%m/%d/%Y'),
enddate = STR_TO_DATE(#tmp_enddate, '%m/%d/%Y');
MySQL doesn't actually allow to change ##date_format anyway:
mysql> SET ##date_format='%d/%m/%Y';
ERROR 1238 (HY000): Variable 'date_format' is a read only variable
As the MySQL 5.6 manual explains:
This variable is unused. It is deprecated as of MySQL 5.6.7 and will
be removed in a future MySQL release.
Also, at Date and Time Types we can read:
MySQL retrieves values for a given date or time type in a standard
output format, but it attempts to interpret a variety of formats for
input values that you supply (for example, when you specify a value to
be assigned to or compared to a date or time type). For a description
of the permitted formats for date and time types, see Section 10.1.3,
“Date and Time Literals”. It is expected that you supply valid values.
Unpredictable results may occur if you use values in other formats.
Although MySQL tries to interpret values in several formats, date
parts must always be given in year-month-day order (for example,
'98-09-04'), rather than in the month-day-year or day-month-year
orders commonly used elsewhere (for example, '09-04-98', '04-09-98').
If it didn't work, just add columns to your CVS for year, month and day and separate day, month and year of your date, and use the following:
set date_column = concat(#year , '-' , #month , '-' , #day)