I have a set of date (start date & end date) in DB, how to validate for duplicate date set between user input and DB. (Mysql)
i have try a few method but it only compare either start or end date only. not both at the same time.
I think you are looking for the following logic.
Filter using between SQL command
SELECT column_name(s)
FROM table_name
WHERE column_name BETWEEN value1 AND value2;
use the following logic:
User registered new START DATE and END DATE
Search for START DATE using BETWEEN into DATABASE
Search for END DATE using BETWEEN into DATABASE
If those values exists into ranges you can show duplicated message.
Regards
C.
Related
I code in the SQL, but I want to change the date format from default of My SQL to different format "DD/MM/YYYY" to use this format to code, but I recieved an error.
More about version of SQL:
SQL: My SQL Workbench 8.0.30 build 2054668
Window 11 Pro
Language: English
So, how can I change the date format or what is the true type of "dmy"? Please help me.
This is code that I used:
set dateformat dmy
And error is:
"Error Code: 1064. You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'dmy' at line 1"
At the moment, we don't really know the reason why you want to change the date format, but I have a couple of assumption:
You want to insert date data from front end into the table but couldn't do so due to the mismatched date format.
You want the end output to show the date format of DD/MM/YYYY instead.
Although, it may look like you need to change the table column date format, there's a way to avoid that operation entirely. However, since you've mentioned changing from YYYY/MM/DD in your title, I'm not sure if your date column is actually DATE datatype because the default MySQL should be YYYY-MM-DD. Nonetheless, I'll address the matter in this answer altogether.
Modify the date format in query and leave the table date column datatype as it is:
From DD\MM\YYYY to MySQL DATE datatype format YYYY-MM-DD
... STR_TO_DATE(data, '%d/%m/%Y')
.. from YYYY-MM-DD to DD/MM/YYYY
... DATE_FORMAT(data, '%d/%m/%Y')
You can use any of that anywhere in a query; whether in SELECT or WHERE.
If "in query" is not what you want and you still want to update the table:
Well, you have two options here:
Directly modify the column datatype then update the value:
ALTER TABLE mytable MODIFY COLUMN date_col VARCHAR(255);
UPDATE mytable SET date_col =DATE_FORMAT(date_col , '%d/%m/%Y');
Or you can add another column, populate the desired date format there and keep the original date column as it is:
ALTER TABLE mytable ADD COLUMN my_date VARCHAR(255);
UPDATE mytable SET my_date =DATE_FORMAT(date_col , '%d/%m/%Y');
this way you have the option to directly use MySQL date functions on the default MySQL date column without the hassle of converting your desired date format into the default before you can use date functions. What I'm saying is something like this:
DAY(mysql_default_dateformat)
is similar to
DAY(STR_TO_DATE(your_dateformat, '%d/%m/%Y'))
which means that you can use DAY() (date function) on the default date format directly without the need to convert what is not default first.
Here's demo fiddle examples
I am a Newbee using MySQL Workbench. I have a table Called requests. It has a column called STATUS and another called EXPIREDDATE. I want to create a Stored procedure that Inputs the text "Exipred" into the column STATUS if the date in EXPIREDDATE exceeds Todays date. The begining of the code is below. Thanks.
CREATE PROCEDURE `Add Expired` ( IF expireddate => todays date THEN status = "expired")
BEGIN
END
Inputs the text "Expired" in to a column A if the date in column B exceeds Todays date
You are describing an update statement with filtering:
update mytable set a = 'expired' where b > current_date
You can easily turn this to a stored procedure - although it wouldn't be very helpful (you can just run the query).
this query is saving complete date and time. but i want to save only time not date in database. is there any query to do this?
update table set current_time=now();
Your column must be set to either DATETIME or TIMESTAMP.
If you use the TIME type then your query would work as expected.
If you are using any other type of column then you could use CURTIME() method or CAST(column AS TIME) as mentioned by other answers, however this would use more space on disk, and make for much slower queries if you use to select, and prevent you from various operators:
e.g. SELECT * FROM table WHERE current_time<'12:00'
You can see more information about the different DATE column types here: https://dev.mysql.com/doc/refman/5.7/en/date-and-time-types.html
Note that the CURTIME() method is not a standard SQL function, so this would only work on MySql
U can use CONVERT (time, SYSDATETIME()) as the value.
This automates your process without using Current_time=now();
INSERT INTO table SET current_time = CONVERT (time, SYSDATETIME());
You can also use curtime();
INSERT INTO table SET current_time = curtime();
Credits: Salmaan C
I have a database with several hundred fields but my data structure is wrong. It is currently in uk format as follows:
d/m /y
01/01/85
01/01/96
23/12/87
What would be the most efficient way to change the dates in bulk to sql standard of year/month/day
eg.
02/01/85 --> 1985/01/02
Create a new DATE type column in your table:
ALTER TABLE myTable ADD newColumn DATE;
Use MySQL's STR_TO_DATE() function to parse the existing values into your new column:
UPDATE myTable SET newColumn = STR_TO_DATE(oldColumn, '%d/%m/%y');
Change your codebase to use the new column. If this can't happen immediately, you could define BEFORE INSERT and BEFORE UPDATE triggers to keep the new column consistent with the old one:
CREATE TRIGGER myTable_bi BEFORE INSERT ON myTable FOR EACH ROW
SET NEW.newColumn = STR_TO_DATE(oldColumn, '%d/%m/%y');
CREATE TRIGGER myTable_bu BEFORE UPDATE ON myTable FOR EACH ROW
SET NEW.newColumn = STR_TO_DATE(oldColumn, '%d/%m/%y');
Drop the old column:
ALTER TABLE myTable DROP oldColumn;
select date_format(date, '%Y-%m-%d') use this to change it to required format.I have used date_format function. You can get more information about date_format here
I have a table called table1 with three columns, one of which is Date_Of_Call which is of datetime type with the data in PDT. I basically need to convert the data from PDT to UTC and put the UTC converted dates into a new column in the existing table. I added a new column with:
alter table table1 ADD Date_Of_Call_UTC DATETIME;
I am able to get the proper time conversion with this select statement:
select CONVERT_TZ(Date_Of_Call, '-7:00', '-0:00') from table1;
The issue I am having is trying to use an update command to take the results of the select statement and put them in the new Date_Of_Call_UTC column. Any thoughts of how to do this?
I tried the below statement and a few variations but can't quite figure out what I need to do:
update table1 set table1.Date_Of_Call_UTC = (select CONVERT_TZ(Date_Of_Call, '-7:00', '-0:00') from table1);
Any assistance is appreciated!
this one should work:
update table1
set table1.Date_Of_Call_UTC = CONVERT_TZ(Date_Of_Call, '-7:00', '-0:00');
NOTE: dates are usually stored already as UTC in mysql, but during output they can be displayed with offset applied, read about it: http://dev.mysql.com/doc/refman/5.0/en/datetime.html and http://dev.mysql.com/doc/refman/5.5/en/time-zone-support.html