I'm trying to add a computed column into an existing database table that will calculate age e.g. "23" from field "DOB" stored as YEAR (4) e.g. 1988.
Using the below, I get a
1582 error - an incorrect parameter count in the call to native function 'DATEDIFF'.
ALTER TABLE names ADD Age INT GENERATED ALWAYS AS (DATEDIFF('year', DOB, CURDATE))
Many examples I've found online have similar layout in terms of parameter count (some use GETDATE or NOW instead of CURDATE).
Anyone have any idea what I'm doing wrong?
Thanks to RigsFolly & Barmar for the feedback. I made a fresh cup of tea and looked at it again.
With DATEDIFF, it seems the MYSQL Server syntax can use the datepart and then use two dates to calculate (including GETDATE). I'm using MySQL through myphp admin which doesnt seem to like the datepart.
I dropped the datepart and changed the date format of the stored DOB field from "YEAR" to "DATE". As a result I no longer received the parameter error and by default the output of the DATEDIFF was returned in days. I then divided the output by 365 to get years.
My original order was wrong too which gave me the DATEDIFF output as a negative value, so changed the order and it works a charm. Thank you.
ALTER TABLE names ADD Age INT GENERATED ALWAYS AS (DATEDIFF(CURDATE(), DOB) / 365);
Related
Probably it's super simple but i've been stuck some hours on this.
I have a column called "Publish_Date" which is a varchar, but my date shows like this: 17.01.11 (year.day.month) and I want to convert it to a date (at this point, any date format it's ok).
Every time i tried to use "convert" or "cast" it gives me a syntax error or the data doesn't change or all the data in the column changes to "null" values.
I'd appreciate if you can help me.
Assuming your data is all greater than 2000 then you can add missing part of YEAR then cast it.
SELECT CAST(CONCAT('20', Publish_Date) AS DATETIME);
You can use STR_TO_DATE with the format %y.%m.%d since this is how your date value is stored
select
str_to_date(birth_date, '%y.%m.%d')
from
mytable
Here is an SQL Fiddle I created for this case
I was trying to find customers born in month of december from a table customers using this query :
SELECT *
FROM customers
WHERE date_of_birth LIKE '____-12-%' ;
But it throws an error : ER_WRONG_VALUE: Incorrect DATE value: '____-12-%'
The type of date_of_birth value is DATE that is it's defined in table as date_of_birth DATE,
Snapshot of the error that i got :
What exactly is wrong here? I remember earlier ones i have used this and it worked fine but now (may be due to new updates) it's not working. Doesn't even seem to be because of MySQL Strict Mode. I can't get what exactly is causing problem.
Also please don't suggest me alternatives i already have those like using MONTH() works fine :
SELECT *
FROM customers
WHERE MONTH(date_of_birth) = 12;
Do not use string functions on date data types. Period. Mixing data types just leads to problems in SQL. Using like on a date requires converting the date to a string and the exact format depends on the locality and settings of the server.
If your data of birth is a string, fix it:
alter table modify column date_of_birth date;
Then the correct way is to use the syntax which you -- for some unknown reason -- specifically do not want to use:
where month(date_of_birth) = 12
If date_of_birth is stored as a string and for some reason cannot be stored using a native format, then you are depending on the unspecified format of the string. Presumably, you want something like this:
where date_of_birth like '%-12-%' -- assuming date_of_birth is a string
assuming that the month is in the middle of the date string, surrounded by hyphens, and the day is either at the beginning or end.
try to where month(date_or_birth)=12
i did not try yet..but there month function in sql
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 have an app that uses a table that has a varchar column [BadColumn] which is populated by a date in the format MM/DD/YYYY or at least that's what most of the rows contain.
I have no control on modifying this table and changing the data type.
I need to report on this data to show rows that are within a week.
Here's my problem:
every time I use something to compare/filter the date I get an error
Conversion failed when converting date and/or time from character string
So here's what I have tried so far :
Tried to create a view with the [BadColumn] being converted to a Date type column [NewBadColumn] and convert the value from the original table to a date type using Convert(date, BadColumn). The view gets populated, but now when I still try to query using DateADD or do any date comparisons I get the same error.
I have also tried to use Convert(Date, BadColumn, 105), but same problem.
I have tried other formats, but still the same issue cannot do a date comparison on that column.
I am not sure which row is throwing the error, how do I find it and fix this issue.
Thank you for the responses. I have tried using ISDate function to find the bad records.
But I am still getting the same error "Conversion failed when converting date and/or time from character string." when I try to use DateAdd.
Here's the code I am using :
Select t.*
from
(Select * from dbo.BadTableName q with (nolock)
where ISDate(BadColumn)=1 and ISDATE(BadColumn) Is Not Null
) t
where t.BadColumn > DATEADD(dd,-2, GetDATE())
Any help is appreciated.
Try checking the column using the ISDATE() function. You can do this without creating a view first.
IF ISDATE(YourColumn) = 1
DATEADD (datepart , number , YourColumn )
ELSE 'No valid date provided'
Look at this other stack over flow question and answer
Find invalid dates in SQL Server 2008
You can use the ISDATE function to test the individual rows.
I've got a really random bug in my code somewhere but can't figure it out. I'm inserting data into a MySQL Database based the current date and use the following statement;
INSERT INTO table VALUES (NULL,495297,str_to_date('19/01/2013 10:55:25','%d/%m/%y'),'English - UK',1,Str_to_date('17/01/2013','%d/%m/%y'),str_to_date('18/01/2013','%d/%m/%y'))
none of the dates work as the 19/01/2013 for some reason it becomes 19/01/2020, the next become 17/01/2020 and 18/01/2020! The latter two dates are listed as Date in data Type whereas the first date is DateTime so I don't know why this bizarre problem is happening.
Any help would be greatly appreciated.
Thanks
Maudise
Use a capital %Y as identifier for the year:
INSERT INTO table VALUES (NULL,495297,str_to_date('19/01/2013 10:55:25','%d/%m/%Y'),'English - UK',1,Str_to_date('17/01/2013','%d/%m/%Y'),str_to_date('18/01/2013','%d/%m/%Y'))
The lower case %y specifies a year given by two digits (see MySQL documentation here and here). The uper-case %Y, however, is the four-digit version.