Data string-to-date Procedure

Asked

Viewed 104 times

1

I am creating a Trial, which receives a date string '2015-09-11', checks if it is indeed a Monday, if not, it takes this date and play for Monday of the week itself. But my concept with sql is very weak, my basis is this way.

    DROP PROCEDURE IF EXISTS SP_DATA_PARA_SEGUNDA;
DELIMITER //
CREATE PROCEDURE SP_DATA_PARA_SEGUNDA (IN SPA_DATA_CONVERTER DATE,INOUT SPA_DATA_CONVERTIDA INT)
    BEGIN            
         SELECT DATE_FORMAT(SPA_DATA_CONVERTER,'%w');
    END//
DELIMITER ;

Now it works, but now I want to return this dateformat na in my argument SPA_DATA_CONVERTIDA, as I do?

2 answers

0


  • I see some functions, but I don’t know if there are restrictions, I can use both mysql and sql functions within the procedures?

  • WOULD THAT be? DROP PROCEDURE IF EXISTS SP_DATA_PARA_SEGUNDA;
DELIMITER //
CREATE PROCEDURE SP_DATA_PARA_SEGUNDA (IN SPA_DATA_CONVERTER DATE, OUT SPA_NOME_DIA VARCHAR(20)
 BEGIN 
 SPA_NOME_DIA = SELECT DATENAME(weekday,SPA_DATA_CONVERTER);
 END//
DELIMITER ;


  • The one I made would work on Sqlserver.. For mysql, From a Look at: [http://stackoverflow.com/questions/23235023/1305-function-datename-does-not-exist]

  • I hope it helps you! (http://stackoverflow.com/questions/17975359/sql-group-by-datename)(http://www.suhailkaleem.com/mssql-datename-alternative-for-mysql/)

  • I changed my question, now it’s working, but how do I return qd I give the call?

  • I changed the Answer.. With some Links. You probably need to use an OUT variable. or save to some table..

  • I created a variable out, but how do I pass the value of the select into this var? I tried so, but it wasn’t.. SET SPA_DATA_CONVERTIDA = SELECT DATE_FORMAT(SPA_DATA_CONVERTER,'%w') as SPA_DATA_CONVERTIDA;

Show 3 more comments

0

I assume that the first day of the week is Sunday, meaning that if you pass as a parameter a date whose corresponding day of the week is Sunday, the stored Procedure will return the next day (Monday).

The function DAYOFWEEK returns the day of the week to which the date passed as parameter corresponds. In your example the day of the week corresponding to the date '2015-09-11' is 6 (1 = Sunday, 2 = Monday, ... , 7 = Saturday)

Knowing this, and as according to this function, the day corresponding to Monday is 2, we only have to add (subtract) the number of days necessary to obtain the desired date.

I did so, surely there are other alternatives:

DROP PROCEDURE IF EXISTS SP_DATA_PARA_SEGUNDA;
DELIMITER //
CREATE PROCEDURE SP_DATA_PARA_SEGUNDA (IN SPA_DATA_CONVERTER DATE,
                                       OUT SPA_DATA_CONVERTIDA DATE)
BEGIN            
     SELECT ADDDATE(SPA_DATA_CONVERTER, INTERVAL 2 - DAYOFWEEK(SPA_DATA_CONVERTER) DAY) 
     INTO SPA_DATA_CONVERTIDA
END//
DELIMITER ;

You got a little example. If you have any questions leave a comment.

  • Excellent.. Also has the DAYOFWEEK ... I thought I needed the Name in full.. But I’m glad you decided, and I liked the Solution! Hug

Browser other questions tagged

You are not signed in. Login or sign up in order to post.