5
We work with several tables that contain a date field, sometimes we need to separate the date string into 3 columns (day/month/year).
I made a function where you pass the date string and it returns these 3 columns, but how do I return them within another Query?
example:
select *, separadata(REP_DATA_INICIO) from intranet_reportmensal
The function would apply to each row of that query.
the function is this
FUNCTION [dbo].[separadata](@data varchar(10))
returns TABLE
AS RETURN SELECT Parsename(Replace(@data, '/', '.'), 3) as DIA, Parsename(Replace(@data, '/', '.'), 2) as MES, Parsename(Replace(@data, '/', '.'), 1) as ANO
MS SQL Server? It seems so but it’s good to add the database tag.
– Caffé
If I’m not mistaken, you can join with the return of the function (which returns table) in SQL Server. But the @ramaral solution seems more sensible in this particular case.
– bfavaretto
@bfavaretto You can join with a function that returns TABLE. Only this asks for a parameter that is a column of the SELECT main table. I was curious how to do that... Anyway, it’s probably not a worthwhile optimization - it’s not so much trouble to use the date functions.
– Caffé
@Caffé I think it would be okay to pass the column of the main table and at the same time do the JOIN. And totally agree with you, to separate the date is better to use DATEPART even.
– bfavaretto
@bfavaretto I tried and I couldn’t. See if you can and put it there please: http://www.sqlfiddle.com/#! 6/b287d/10/0
– Caffé
@bfavaretto Hmm found :-) I will post here.
– Caffé