Questions tagged [sql-function]

A function defined in a relational database system. Most RDBS's include numerous built-in functions and also allow for the creation of additional custom functions.

908 questions
6
votes
2 answers

Returning table from table-valued function and set that value in temp table

I have a table valued function. Also I have scalar valued function within I have declared a temp table. I want to execute the table valued function inside of scalar valued function and set that value in temp table soemething like this **Exec…
So_oP
  • 1,211
  • 15
  • 31
6
votes
2 answers

SQL Lead and Lag functions from C# code

Is it possible to use the LEAD or LAG SQL functions from C#? My preference of method is: Linq to SQL Entity Framework Dynamic Linq SQLFunctions class Manual TSQL via a SQLCommand I know it can be done via method 5, but what about 1 through 4?
OrdinaryOrange
  • 2,420
  • 1
  • 16
  • 25
6
votes
3 answers

Concatenating distinct column values in SQL Server

I am trying to concatenate many columns and separating it with a comma as below: Column ------ abc bcd bgd abc Expected output: abc,bcd,bgd I am using this code: CREATE FUNCTION concatinate(@PK uniqueidentifier) RETURNS varchar(max) AS BEGIN …
Gerald Baretto
  • 279
  • 2
  • 4
  • 20
6
votes
1 answer

How to generate SQL function calls with the CakePHP query builder?

I have a fullname column for authors and would like to extract the surname into another column. I do that with the following raw SQL: SELECT name, SUBSTRING_INDEX(`name`, ' ', -1) AS `surname` FROM qr.authors; Output: Under "Using SQL Functions"…
ndru
  • 83
  • 1
  • 10
5
votes
1 answer

Why can't I run INSERT EXEC on a table variable in a T-SQL function?

My function looks like this: CREATE FUNCTION fn_FileSys_DirExists(@dirName AS nvarchar(260)) RETURNS bit AS BEGIN DECLARE @dirExists int DECLARE @fileResults TABLE ( file_exists int, file_is_a_directory int, …
ProfK
  • 49,207
  • 121
  • 399
  • 775
5
votes
0 answers

PostgreSQL function returning composite type using within sqlalchemy

I have an issue with composite types of postgres within sqlalchemy. The function my_function() returns a composite type consisting of two float and one text type. I added the function to sqlalchemy with: class my_function(GenericFunction): …
5
votes
1 answer

create dataset in SSRS with inline function

I have a SQL based report that I am migrating from Crystal Reports to SSRS. The old method uses a stored procedure that calls a stored function. The intent in the new method is to embed all of the report logic in the SSRS report. The reason…
5
votes
1 answer

How to add non-standardized sql functions in Spring Boot application?

My app needs to be portable between Postgres, Mysql and for testing Hsqldb. I've setup Flyway to make some custom functions available on all three, that I now want to use in my SQL/HQL queries. My current setup is using separate Dialects that I…
Tim
  • 19,793
  • 8
  • 70
  • 95
5
votes
1 answer

sql function to change multiple dateformat into one dateformat

how to change multiple dateformat into one dateformat in sql? currently, i'm using my java code. however, I would like to change using sql function so that I don't need to consume another connection between my java program and database. My current…
goutthee
  • 329
  • 4
  • 14
5
votes
2 answers

How to run mySQL function to update all rows?

I am trying to strip HTML tags from all of my records directly through MySQL. Thanks to StackOverflow's this question, I found the following function, that kind of does strip html tags - SET GLOBAL log_bin_trust_function_creators=1; DROP FUNCTION IF…
Dr. Atul Tiwari
  • 1,085
  • 5
  • 22
  • 46
5
votes
4 answers

track revisions in postgresql

I have to keep track of revisions of records in a table. What I've done is create a second table that inherits from the first and adds a revision counter. CREATE TABLE A ( id SERIAL, foo TEXT, PRIMARY KEY (id)); CREATE TABLE B ( revision INTEGER…
veilig
  • 5,085
  • 10
  • 48
  • 86
5
votes
2 answers

Entity Framework - sort by SQL function

Is it possible to sort in Entity Framework by sql function from database? I mean something like: var xx = DbContext.Set
() .Where(x=>x.Name.Contains("X")).OrderBy("[dbo].[MySQLFunction]");
foxiter
  • 51
  • 4
5
votes
4 answers

Error using Common Table Expression in SQL User Defined Function

CREATE FUNCTION [dbo].[udfGetNextEntityID] () RETURNS INT AS BEGIN ;WITH allIDs AS ( SELECT entity_id FROM Entity UNION SELECT entity_id FROM Reserved_Entity ) RETURN (SELECT (MAX(entity_id) FROM allIDs ) END GO SQL…
Marcus K
  • 779
  • 1
  • 10
  • 22
5
votes
3 answers

TSQL Error: A RETURN statement with a return value cannot be used in this context

I am just trying to create a function that returns a select statement, but it gives the error: A RETURN statement with a return value cannot be used in this context. This is my code: CREATE FUNCTION [dbo].[Sample] (@SampleValue int) RETURNS…
HOY
  • 1,067
  • 10
  • 42
  • 85
4
votes
2 answers

Update field by a function

I have the function which gets ID and returns date from table if it exists or returns current date if isn't: CREATE FUNCTION [dbo].[CLOSEDATE] (@ID int) RETURNS datetime AS BEGIN DECLARE @closed int; DECLARE @result datetime; …
ceth
  • 44,198
  • 62
  • 180
  • 289