Using: MySQL Workbench & MySQL for Excel
I have a number returned that is 12 digits long, however when imported in to excel using any manner this results in: 1.20523E+11.
I want (need) to specify that the value needs to be returned as text so to complete the query via MySQL for Excel without this error.
My existing query:
Select
-- Get Order data --
T5.orders_id As OID,
DATE_FORMAT(T5.date_purchased, '%Y-%m-%d') As ODate,
T3.products_name As PName,
T3.products_id As PID,
error:
DATE_FORMAT(T5.date_purchased,'%y%m%d%H%i%s') As DecRepID,
-- This results in 1503070244
-- Transaction ID to match with Payments
I need the 1503070244 to return as TEXT so excel imports it without me needing to then convert the text.
I've looked at CONVERT & CAST however I can't get the query to work correctly & they resolve in to syntax errors.
CONVERT(VARCHAR(12),(DATE_FORMAT(T5.date_purchased,'%y%m%d%H%i%s'))) As DecRepID,
CAST returns the correct values, however Excel doesn't recognise as text.
cast(DATE_FORMAT(T5.date_purchased,'%y%m%d%H%i%s') as char) As DecRepID
I know it's something simple, however I can't find what is the cause of the error.
Is there a way to correct this through code, or am I stuck with post-formatting after import in to Excel?
Thank you.
Using the idea that Shahkalpesh had, you could do 2 different things:
1) You could try escaping the ' by using: '\''
concat('\'', DATE_FORMAT(T5.date_purchased,'%y%m%d%H%i%s')) As DecRepID,
2) You could try escaping the ' by using: "'"
concat("'", DATE_FORMAT(T5.date_purchased,'%y%m%d%H%i%s')) As DecRepID,
By escaping the ' the column will import in to Excel as: '1503070244 which is what you will need to have Excel recognise it as text to stop the shortened result.
You can read more here: http://dev.mysql.com/doc/refman/5.7/en/string-literals.html
Related
I am working on a batch script where I am parsing IIS logs using Log Parser.
So, I have 2 questions here:
Q1. What I want is, not to see the entries having username written in a text file. So, one can update that text file without worrying about the code syntax.
In other words, Instead of putting every username(could be around 30-50) in 'WHERE' clause using 'AND' again & again, I will have a text file having list of usernames.
Code Example:
"LogParser.exe" -i:csv "SELECT DISTINCT date, cs-username, clientun, cs-uri-stem FROM D:\temp.csv WHERE NOT cs-username IN ('NULL';'abc';'def';'hij';'and_so_on')" >D:\final_output.txt -o:NAT -rtp:-1
I was trying to do this via sub-query first, but it is not supported in Log parser it seems. I found OPENROWSET as a solution here but that doesn't seems to be working for me or may be I am not getting how to make it work.
Other solution I found were for SQL and were not working for log parser.
Q2. I want the logs to be read between the dates mentioned. So, I am taking a start and an end date from user in YYYY-MM-DD format and putting them in query
"LogParser.exe" -i:iisw3c "SELECT DISTINCT cs-username, REVERSEDNS(C-IP), date, cs-uri-stem, FROM \logs\location\* WHERE date BETWEEN %date_1% AND %Date_2%" >D:\temp.csv -o:csv
The error I get here is:
Error: WHERE clause: Semantic Error: left interval of BETWEEN operator ("'2016-02-15'") has a different type than operand ("date")
Please note, the output file generated from the Q2 is used as input in Q1.
Q1: Instead of filtering the data using logparser use findstr and the /g:file /v switches to filter input files or output lines (depending on the case)
Q2: Strings are not timestamps. Use
BETWEEN TO_TIMESTAMP('%date_1%','yyyy-MM-dd') AND TO_TIMESTAMP('%date_2%','yyyy-MM-dd')
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"))
I came across an issue when running a procedure that shreds an XML file and imports the data into SQL server.
It has been running successfully for a few months, but today I got an error:
Conversion failed when converting date and/or time from character
string.
This is the line where it fails
SELECT
ltrim(rtrim(T.X.value('Cell[4]/Data[1]','varchar(max)'))) AS StartDate
,ltrim(rtrim(T.X.value('Cell[5]/Data[1]','varchar(max)'))) AS EndDate
FROM #xml.nodes('/Workbook[1]/Worksheet1]/Table[1]/Row') as T(X)
When I looked at the XML file, I noticed that some of the dates were written like this:
01/12/2016 
This character   is a Non-breaking space.
I would like to know if there is any way in SQL Server to account for these types of issues? For this specific problem, I can use REPLACE:
SELECT
REPLACE(ltrim(rtrim(T.X.value('Cell[4]/Data[1]','varchar(max)'))),' ','') AS StartDate
,ltrim(rtrim(T.X.value('Cell[5]/Data[1]','varchar(max)'))) AS EndDate
FROM #xml.nodes('/Workbook[1]/Worksheet1]/Table[1]/Row') as T(X)
but if other XML/HTML characters come up, is there a way to universally check for/deal with them?
I'd imagine you could create an auxiliary table of strings that you wish to replace/remove and join that to your query. This would be preferred over hard-coding each character, and would allow you to expand on the functionality easily. The caveat is that I'd expect it to slow down you query based on the number of characters you need to replace/remove.
SELECT
REPLACE(ltrim(rtrim(T.X.value('Cell[4]/Data[1]','varchar(max)'))),
StringsToRemove.string,'') AS StartDate,
REPLACE(ltrim(rtrim(T.X.value('Cell[5]/Data[1]','varchar(max)'))),
StringsToRemove.string, '') AS EndDate
FROM
#xml.nodes('/Workbook[1]/Worksheet1]/Table[1]/Row') as T(X),
StringsToRemove
I have a string such as,
"This is a sting and I dont know how long I am"
I want to turn every word in string into a row for my sql table so that I get:
ThisIs a string and I dont know etc...
I need to be able to do this with the MySql command line. (I also need an adjacent column to all be filled with ones on every row, incase that helps/changes your answer) I was thinking I could somehow use INSERT String (Words, num) Values (#words, 1) but I dont know how to get it to add every word. Is there any easy way to do this? If not, how would it be done?
MySQL does not have a function to split a delimited string. This problem is heavily discussed on MySQL manual page (search "split"), although there is no direct solution to handle variable number of elements.
Instead of that, I would help myself to generate such a query:
SELECT CONCAT('INSERT INTO t1 VALUES ("', REPLACE(REPLACE(TRIM(string_column), '"', '\\"'), ' ', '", "'), '")') FROM t2_with_string
Working with a MS Access database, using one particular table, and scattered throughout the table at varying positions in date columns (which themselves can be in varying orders as a result of the data import) is the text "Not known". I want to replace occurrences of that text string across the whole data table.
The only way I can think of doing it is export to a csv format, and do a REReplace then import the data again, but I would like to know if there is a 'slicker' way?
The columns contain data which is a data import from a csv file so all the columns are text, they can contain a mix of "date string", text, numbers (as string) and null.
You can use replace, it follows basic TSQL implementation :
http://msdn.microsoft.com/en-us/library/ms186862.aspx
Here is an example I did updating the customers table of the Northwind sample database:
update customers set Customers.[Job Title] = replace( Customers.[Job Title], 'Purchasing', 'Manufacturing');
So to distill it into a generic example :
update TABLENAME set FIELD =
replace( FIELD, 'STRING_TO_REPLACE', 'STRING_TO_REPLACE_WITH' )
That updates the entire table in one statement. Be careful ;)
You can do this using Access, running edit-replace command. If you need to do this in code - you can open recordset, loop through records and for each field run:
rst.fields(i)=replace(rst.fields(i),"Not known","Something")
this is how it works in VBA, beleive you can do something similar in coldfusion
Why not just open the CSV file in Notepad++ (or similar) and do a Find/Replace?