Get an integer and convert it to a date - mysql - mysql

I am trying to extract the date from a members CPR numbers in my Database, its stored as an integer. (CPR is Danish social security number and the first 6 characters are your date of birth)
Here is my SQL statement however it doesn't quite work. The SELECT LEFT bit works its just the STR_TO_DATE which returns an error.
SELECT LEFT(cpr, 6) AS #iCPR FROM members
STR_TO_DATE(iCPR,'p%m%Y')

It looks like you have a mistake in your STR_TO_DATE format. Try changing it to '%d%m%Y'.
See here for the list of valid specifiers for that function:
https://dev.mysql.com/doc/refman/5.7/en/date-and-time-functions.html#function_date-format

Related

In SQL Server getting month or date using LIKE and Wildcard Characters gives error

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

Between mysql date_format not working

I have this query
SELECT *
FROM `users_profile`
WHERE DATE_FORMAT(dob,'%d-%m-%Y') BETWEEN '05-03-1996' AND '05-03-1915'
which should return two results which both have these dates in the dob column
08-02-1996
14-02-1996
But it dosen't return anthing!! What am I doing wrong!!??
Why would you take a perfectly good date and convert it to a (bad) string for comparison?
Do the comparison as dates and put the constants in the right order:
SELECT *
FROM `users_profile`
WHERE dob BETWEEN date('1915-03-05') and date('1996-03-05');
Also note that I changed the date format for the date constants to YYYY-MM-DD. This is the ISO standard format for dates. (Despite that), it is a really good idea to use.
I am assuming that dob really is a date, because that is what the function date_format() takes for its first argument.

Why i got null from this query

Following is my sql query kindly let me know why is it returning null
Select STR_TO_DATE ('11-APR-74','%e%b%Y')
OR
Select DATE_FORMAT ('11-APR-74','%e%b%Y')
From MySQL STR_TO_DATE function:
The server scans str attempting to match format to it. ... Scanning
starts at the beginning of str and fails if format is found not to
match.
This is why your first query fails: 11-APR-74 does not look like %e%b%Y, so date cannot be parsed. You should do instead
SELECT STR_TO_DATE ('11-APR-74','%e-%b-%Y')
From MySQL Date and Time types:
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').
This is why your second query fails: 74 is not a valid day of month, you should do instead
SELECT DATE_FORMAT ('74-APR-11','%e%b%Y')
Note, that DATE_FORMAT is usually used on DB values, not string literals as you do - to get an output different from the default one.
If you want to convert from string to date
Select STR_TO_DATE ('11-APR-74','%d-%b-%y')
use it like ::
Select STR_TO_DATE ('11-APR-74','%e-%b-%Y')
Because '%e%b%Y' format does not correspond to '11-APR-74' string value (as STR_TO_DATE function expects), and because '11-APR-74' value is of type CHAR, but not DATETIME (as DATE_FORMAT function expects).
If you want to reformat a date represented by a CHAR value, convert it from its original format to DATETIME first, and then convert it to a string of desired format:
SELECT DATE_FORMAT(STR_TO_DATE('11-APR-74','%e-%b-%Y'),'%e%b%Y');
By the way, you could strip dashes with a plain string sunction:
SELECT REPLACE('11-APR-74','-','');

MYSQL Select Rows after specific Date (row type is varchar)

I'm trying to select all columns after a specific date but the trick is the "date" column is varchar
I have this
SELECT * FROM `users` WHERE STR_TO_DATE(birthday,'%m/%d/%Y') > '10-10-2000'
as an example and I want to select all users whos birthday is after the 10/10/2000 but this just returns all rows.
Anybody got a clue what's wrong?
You are comparing a data and a string. I would use CAST:
WHERE STR_TO_DATE(birthday,'%m/%d/%Y') > CAST('2000-10-10' AS date)
Also note that I changed the format of the second to match what mysql expects. You could also use STR_TO_DATE here.
I think mysql dates are usually yyyy-mm-dd which means you should change the string at the end to 2000-10-10.
Try that and let me know if that works.
We need to know what format the string birthday is in, now you're saying it is e.g. 12/31/2001. Is that right?
If you have multiple strings, consider using the same STR_TO_DATE-formatting:
WHERE STR_TO_DATE(birthday, '%m/%/d/%Y') BETWEEN STR_TO_DATE(some_date, '%m/%/d/%Y')
AND STR_TO_DATE(another_date, '%m/%/d/%Y')
More info:
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_str-to-date

MS Access SELECT and WHERE

I am having trouble getting records out of a database for a certain date. This this the SQL command I have at the moment:
SELECT * FROM ict1 WHERE date='26/03/1992'
It should return one record from my database. "date" is a column and one of the records has '26/03/1992' as the value. I have tested the db connection and I can use:
SELECT * from ict1
So I know it's not that. It's probably just the SQL syntax being a lot different, I'm used to MySQL :#)
Should probably mention that I'm using .NET with an OleDbConnection.
Thanks.
Usually dates need to be formatted as for access like the following
Select * from ict1 where date= #03/26/1992#
The # denotes a date in access.
Be very careful with dates formatted as '10/03/1992' as it could mean 10th of march or 3rd of october depending on where you are.
Also #1992/03/26# also works
You may want to use a date comparison function instead of date = #xxxx/xx/xx#. Date comparisons do not yield expected results because of formatting and data type issues. In SQL Server your date might be stored as a datetime or a date data type. You need to make sure you are comparing things in the same type and format.
DateDiff ("d", #1992/03/26#, date) = 0
Use the date as YYYY/MM/DD format:
SELECT * FROM ict1 WHERE date='1992/03/26'
SELECT * FROM ict1 WHERE date=#1992/03/26#