I have a 300.000 rows table; one of the columns is a varchar() but it really contains a date xx/xx/xxxx or x/x/xxxx or similar. But executing the following test yields an error:
SELECT CAST(MyDate as Datetime) FROM MyTable
The problem is that it doesn’t tell me in which row…
I have executed a series of “manual” updates by trial an error and performed simple updates to fix those, but there’s got to be some weird values that need to either be deleted or fixed.
For example I performed a simple test that fixed about 40 rows:
UPDATE MyTable SET MyDate = REPLACE(MyDate, '/000','/200') FROM MyTable WHERE MyDate like ('%/000%’)
UPDATE MyTable SET MyDate = REPLACE(MyDate, '/190','/199') FROM MyTable WHERE MyDate like ('%/190%’)
This fixed quite a few weird rows that had dates like 01/01/0003 and such. (Dates range from 1998 to 2010).
However, I’d like to know which rows are failing in the above select.
What would be the best way to print those so I can either delete them, edit them or see what to do? Thanks.
SELECT
*
FROM
MyTable
WHERE
ISDATE(MyDate) = 0
From the ISDATE(Transact-SQL) documentation ISDATE(...)
Returns 1 if the expression is a valid datetime value; otherwise, 0.
ISDATE returns 0 if the expression is a datetime2 value.
Did you try the ISDATE function?
Careful with IsDate. I have 1 bad record in a table of thousands. It says 8201-11-30 is a valid date. IsDate should have a YEAR limitation.
ISDATE doesn't seem to be working always.
the following returns 1 in SQL server 2008 R2
Select ISDATE('04- December 20')
But trying to cast the same value to date will fail.
Select cast('04- December 20' as date)
Conversion failed when converting date and/or time from character string.
Related
I'm using Access 2010 and have a table like the following called Table1 (the table is imported from Excel and the date column was formatted as date there):
date xy
---------------------------------
19.10.2016 14:10:51 jljh
19.10.2016 13:13:28 kgkhg
19.10.2016 12:53:15 asd
I've already accepted that I can't do normal SQL things in Access. But why does the following simple-as-hell query ends in the error data type mismatch in criteria expression?
SELECT DateValue(DATE) as dt, COUNT(CSID)
FROM Table1
GROUP BY DateValue(DATE)
First, you can do "normal SQL things" in Access.
Then, DateValue doesn't accept dot as the date separator, thus:
SELECT DateValue(Replace([DATE], ".", "/")) as dt, COUNT(*)
FROM Table1
GROUP BY DateValue(Replace([DATE], ".", "/"))
You -- that is me -- say that the table was imported from Excel. So go back to Excel, do the date truncation there, re-import it again, and your -- that is my -- query will run as if it were "normal" SQL. Nothing more intuitive than this shit.
EDIT: Meanwhile I also figured out what the problem was: there were unfilled values in the date column. So DateValue cannot know how to handle them and must throw an exception. However, what's still a bit annoying is that this error did not occur without the group by statement. So it's only 50% the blame of Access this time, the other part is on me :-)
I'm trying to select with reference to one column changing its time in MySQL, but I don't know how.
How will I do it?
Example:
Time original: 2015-07-20 22:10:52
Updated: 2015-07-20 23:59:59
You can use timestamp to join the current timestamp's date with the time you want to set:
UPDATE mytable
SET mytimestamp = TIMESTAMP(DATE(mytimestamp), '23:59:59')
To update with no reference to previous column value, it's no different from other columns. To update with it, and based on certain part (second, month, year, whatever), you can use DATE_ADD or DATE_SUB function. Any other function in the same page might be useful, too, depending on your needs.
It doesn't matter what type of column you have, your table can be modified with a standard UPDATE statement.
You can also use the some special time/date functions to help you get the right format.
UPDATE mytable SET mytimestamp = mytimestamp::date + '23:59:59'::time;
it should work.
I am working on a database which has date fields in 'yyyy-dd-mm hh:mm' format, I used STR_TO_DATE to change the field to 'yyyy-mm-dd hh:mm' but it is giving back an error.
Query:
UPDATE transaction
SET time_creation = STR_TO_DATE(time_creation, '%Y-%m-%d %H:%i');
Error:
Incorrect datetime value: '2005-08-06 15:57:00' for function str_to_date
I did a check with the following query too:
SELECT
time_creation,
STR_TO_DATE(time_creation,'%Y-%m-%d %H:%i') AS DATE_FORMATTED
FROM transaction;
and got NULL in the DATE_FORMATTED column for date values such as
'2007-22-11 15:32' but worked fine for '2007-09-11 13:12'. I don't understand what exactly is happening. Any help is appreciated.. thank you.
You have
STR_TO_DATE(time_creation,'%Y-%m-%d %H:%i')
and you give '2007-22-11 15:32' which means 'yyyy-dd-*mm* hh:mm'.
Sql is trying to parse a date that contains the number 22 as month...
Abyway
SELECT STR_TO_DATE('2005-08-06 15:57:00', '%Y-%m-%d %H:%i');
works fine in my mysql server.
First, you need to share the table definition with us. It sounds like the column type for the time_creation field is a VARCHAR and not a DATETIME. So you really should not try to store the result back into the same field. You want to save it into a valid DATETIME field so that the intrinsic column type is correct.
Second, it sounds like some of your values are stored as YYYY-DD-MM and some as YYYY-MM-DD. Is that the case? If it is, you'll need more logic in order to differentiate between the values that are already correct and those that are not. If they are mixed, however, you are in a bit of a pickle because how do you know the proper way to interpret 2012-01-03?
EDIT:
Create a new DATETIME field (for the example I just use new_time_creation) and update like this:
UPDATE transaction SET new_time_creation = STR_TO_DATE(time_creation, '%Y-%d-%m %H:%i');
How to display results with date range?
For example:
I want to show results for sept. 15, 2011 to oct. 20, 2011?
Thanks
This has little to do with RazorSQL and almost everything to do with the data that you're querying. Does the data set that you are querying have a date column that you can key off of? If so, you have two options.
Use a where clause in your SQL query to only bring back data from your source database that is within the range that you want.
Retrieve all of the data in a table and then use RazorSQL's filter feature to whittle down the data.
Of course, from a performance perspective, the first option is the best.
try this hope it will help you
select * from urtable where cast (DATEPART(year, datatimeclm) as varchar(50))+'-'+cast (DATEPART(month, datatimeclm)as varchar(50))+'-'+ cast (DATEPART(day, datatimeclm)as varchar(50)) between '2011-09-15' and '2011-10-29'
For SQL-Server try this
DECLARE #tbl table(dtm datetime)
insert into #tbl
values ('20110914 00:59:00'),
('20110915'),
('20110915 05:10:00'),
('20110916 05:10:00'),
('20111029 05:10:00'),
('20111029'),
('20111030')
SELECT *
FROM #tbl
WHERE dtm>='20110915' AND dtm<'20111030'
To return date without time from a datetime value you can use this CONVERT(datetime,CONVERT(varchar,#date,1),1)
In SQL SERVER 2008 + you can use DATE data type in that case you could use dtm<='20111029'.
I am getting this error while I am trying to execute a simple SELECT statement in Toad
MySql.Data.Types.MySqlConversionException
Unable to convert MySQL date/time value to System.DateTime
What could be wrong?
That could mean one of these two common issues:
1) Zero dates, which are 0000-00-00 in MySQL. MySQL allows you to store them to mark 0 dates, you can even use 0001-01-01, but not all drivers or downstream programs can handle them. Add to the connection string
Allow Zero Datetime=true;
The other choice is explicitly removing them, something like
SELECT IF(DateCol='0000-00-00' OR DateCol<'1970-01-01', NULL, DateCol) as DateCol,
Othercol1, ID ....
FROM TBL
2) Date formatting. For some driver/program combination, the dates are handled as strings. Explicit conversion is necessary:
SELECT DATE_FORMAT(DateCol, '%m/%d/%Y') as DateCol,
Othercol1, ID ....
FROM TBL