Input only month and year, stored procedure - sql-server-2008

In my stored procedure we have to input the 2013-12-12 to get data. Now with a new system we only want to use month and year to get the correct data. We use Microsoft SQL server 2008.
This is how we get data today:
exec dbo.Month '2013-12-12'
This is how we want the input to be to get data:
exec dbo.Month '2013-12'
-- #dateStr format yyyy-mm-dd
Possible to change?
I don't really know where in the database to start looking, and the stored procedure is to big to start adding here I guess.

There is no type for only a datapart of the date. You can of course change the input parameter to varchar() of some size and then do the necessary format inside the SP with 'Convert' (http://msdn.microsoft.com/en-us/library/ms187928.aspx). But it is not really that nice a solution.

Related

How to convert date from MM/dd/yyyy to yyyy-MM-dd in MySQL? Error Code: 1411. Incorrect datetime value: '' for function str_to_date

I have a large dataset with employees' time entries. The current date format is MM/dd/yyyy. However, I need to convert all the dates into yyyy-MM-dd format.
I have tried the following:
Update human_resources.timekeeping
Set Actual_Date = str_to_date(Actual_Date,'%d-%m-%Y');
Got the errror messsage Error Code: 1411. Incorrect datetime value: '' for function str_to_date.
My SQL version is 5.7.18-log.
I tried to view SQL mode using SELECT ##sql_mode; and I got NO ENGINE SUBSTITUTION.
I have tried to retrieve the value like shown below and it was working fine.
Converting varchar mm/dd/yy to date format yyyy-mm-dd
However, updating the data would not work. I need to update the actual records, not insert new records.
Hope someone can help me regarding this. Thank you in advance!
EDIT: The data type for Actual_Date is VARCHAR.
Apologies if my explanation may be a bit confusing. But I am using this data set to display and filter time entries in a gridview. When I am filtering dates, say for example (01/15/2022-01/25/2022), data from 2021 is also being displayed. When I tried to manually change the format of some of my data in sql to yyyy-MM-dd, my code seemed to be working fine. The problem is there are a lot of data in this table, which is why manually updating the format is impossible. What is the first thing that I need to do? I'm sorry this is all still a bit confusing for me.
My apologies if you have already taken the following things into consideration but I thought them worth mentioning.
Given that you say this is a "large dataset" I assume this is a table that is currently in use. Does the existing application rely on the Actual_Date being in that string format? Does it rely on a fixed number of columns in the table? Some poorly written applications can be very brittle when it comes to changing underlying data structure.
You may want to consider creating a copy of the current table, modifying the structure of the copy, and replacing the original with a view with the same columns and formats as the original. This way you get improved data but reduce risk to existing application.
In the title and first line of your question you state that the current format is MM/dd/yyyy
Update human_resources.timekeeping Set Actual_Date = str_to_date(Actual_Date,'%m/%d/%Y');
Your separator is / not -
%d-%m-%Y >> %d/%m/%Y

Error in procedure while changing string format

I have a procedure that accepts string as input in format dd/mm/yyyy but I need to call this procedure from another application and I am forced to give the format yyyy-mm-dd.
So I have extracted and changed the string to dd/mm/yyyy but the procedure is throwing incorrect syntax error.
Required format: dd/mm/yyyy e.g '11/04/2018'
Input format: yyyy-mm-dd e.g. '2018-04-11'
So my procedure would be
Required format is abcdprocedure('11/04/2018')
My formula
abcdprocedure(SUBSTRING('2018-04-11',9,2)+'/'+SUBSTRING('2018-04-11',6,2)+'/'+SUBSTRING('2018-04-11',1,4))
Error: Incorrect syntax near ''2018-04-11''
I am unable to understand the problem.
Edit
Procedure is being called in below way:
select * from abcdprocedure('11/04/2018')
When you execute a stored procedure within T-SQL, you may supply a value, a variable or DEFAULT for each parameter. You may not supply an arbitrary expression.
Move your expression out into a separate line that places the result into a variable and use that when calling the stored procedure.
(Also, seriously, please reconsider your use of strings here. T-SQL has perfectly good datetime related data types that are designed to hold datetimes. You only have formatting issues because you're working with strings)
EDIT
Procdure is being called in below way:
select * from abcdprocedure('11/04/2018')
Um, no. If that line of code works, then what we're talking about is not what T-SQL calls a stored procedure. Stored procedures are standalone blocks of code and cannot be integrated into larger queries. Again, if this works, please identify what abcdprocedure actually is (a table-valued function?) and update your question.
Documentation says -
A table-valued function returns a single rowset (unlike stored
procedures, which can return multiple result shapes). Because the
return type of a table-valued function is Table, you can use a
table-valued function anywhere in SQL that you can use a table. You
can also treat the table-valued function just as you would a table.
First of all you should check the function 'abcdprocedure'
You can store your conversion from yyyy-mm-dd to dd/mm/yyyy in a variable
Declare #v_date date;
#v_date=SELECT CONCAT( SUBSTRING('2018-04-11',9,2),'/',SUBSTRING('2018-04-11',6,2),'/',SUBSTRING('2018-04-11',1,4));
And then you can call your table valued funcyion
select * from abcdprocedure(#v_date)
you can use below query for this
SELECT CONCAT( SUBSTRING('2018-04-11',9,2),'/',SUBSTRING('2018-04-11',6,2),'/',SUBSTRING('2018-04-11',1,4));
It will work in mysql and sql server both.
OR
SELECT (SUBSTRING('2018-04-11',9,2)+'/'+SUBSTRING('2018-04-11',6,2)+'/'+SUBSTRING('2018-04-11',1,4))
**OUTPUT:**
11/04/2018

MySQL DATE type value gets displayed with hours/minits/seconds, however its is not stored like that in the database

I have DATE type column in a MySQL database table with dates stored in it. I am using the YYYY-MM-DD format when I upload the dates to the database.
When I check the values they are stored in the correct and expected format (2005-01-02), however when I am attempting to read these values with my C++ program I am receiving something like this: 02/01/2005 00:00:00
I don't really understand the reason, I thought in the DATE type only the YYYYMMDD gets stored without the time. The other weird thing is the reformatting I thought YYYY-MM-DD is an accepted format.
Here is my code:
String^ getDateOfBirth = myReader->GetString("player_date_of_birth");
DateOfBirthLabel->Text = getDateOfBirth;
I am using Visual Studio 2013.
Any help would be appreciated.

Format date in mysql query that uses cast

I've got this as the select part of my query:
SELECT cast(cast(exp_channel_titles.edit_date as char(14)) as datetime) AS Edit_Date
That takes data from a db in this format 20130501092128 and returns it in this format 2013-05-01 09:21:28
I can only assume it is some kind of magic as i don't fully understand how this works tbh.
But, i need to change the format of the date that it spits out to this format: %d/%m/%Y %k:%i:%s
I can honestly say i have no idea how to do this in that query, i've tried adding it as a param to datetime (is that even a mysql function?!?) but no joy and many other poor attempts that i wont go into.
If anyone can help, i'd be hugely grateful!
MySql automatically converts 20130501092128 to a date and time field, even if it is a VARCHAR or a INT, and you can just use this:
SELECT DATE_FORMAT(exp_channel_titles.edit_date, '%d/%m/%Y %k:%i:%s')
Please see fiddle here.
You can change output format using DATE_FORMAT() function from MySQL. Here is the documentation post about it.
https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-format
You can change the output format into whatever format you want, but if you recieve that data into an application, modifies it and return that data to server (editing a row for example). Remember to reformat it into a valid date for MySQL.
If you dont know how to do it, just have to do this into your query:
SELECT DATE_FORMAT(cast(cast(exp_channel_titles.edit_date as char(14))
as datetime), '%e/%m/%Y %k:%i:%s') AS Edit_Date

How to make the default date format dd/mm/yyyy in SQL Server 2008?

I have an Excel file that contains a column full of dates in the dd/mm/yyyy format. When I try to import it using openrowset, it said that there was a datatype mismatch. I have a table where the date is defined as type date. Now, I know that the default date format in SQL Server is yyyy-mm-dd. How can I avoid this conflict? Is there a way I can make the default date type be dd/mm/yyyy? I need to do this import operation everyday and it has to be automated and so I cannot afford it to fail in between. I tried using sp_addlanguage to make it British as the default date type is dd/mm/yyyy there, but it didn't work :(. I'm using SQL Server 2008 and Windows 7, if that is of any help. Please help me out! Thanks!
You could CONVERT the incoming data before you insert it. So, in the openrowset statement, where you select the field, you could surround it with a CONVERT statement. Here's an example:
print convert(date,'19/07/2010',103)
This is a UK style date, but if you run it you can see that it's converted it to SQL-friendly format.