Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Start a field with integer 1

Posted on 2016-10-12
7
Medium Priority
?
79 Views
Last Modified: 2016-10-12
Hello,

How do I write a query to return value 1 when it is the beginning of the year as a Reference number.
The requirement is the Reference number should start with 1 at the beginning of the year.

Cheers
0
Comment
Question by:RIAS
  • 3
  • 2
  • 2
7 Comments
 
LVL 52

Expert Comment

by:Vitor Montalvão
ID: 41839922
Without you giving more information I can only provide the following query:
SELECT CASE
	WHEN CAST(GETDATE() AS DATE) = DATEADD(yy, DATEDIFF(yy, 0, GETDATE()), 0) THEN 1
	ELSE 999
END

Open in new window

NOTE: Replace 999 with the value or formula that you want when the date is not the beginning of the year.
0
 

Author Comment

by:RIAS
ID: 41839944
Thanks Vitor,
My earlier query was to find max value
	SET NOCOUNT ON;  	
	
	   declare @SQL varchar(500) = null
	  -- NONCLUSTERED INDEX [NIX__UNQ__UID_]

	SET @SQL = 'SELECT MAX ('    
        
			SET @SQL = @SQL +  '[' +  @ColumnName1 + ']'  + ' +1 ) ' +   + ' FROM  [dbo].[' + @tableName + '] '  		
	
	  SET @SQL = @SQL  	
	  
	  EXEC(@SQL)

Open in new window

But, now the need is give value refno 1 if it a new year and refno 1 is not found for new year
0
 
LVL 52

Assisted Solution

by:Vitor Montalvão
Vitor Montalvão earned 1000 total points
ID: 41839968
Can't see where you're storing the MAX value so I invented a value called @NextValue but you should use the correct name:
IF CAST(GETDATE() AS DATE) = DATEADD(yy, DATEDIFF(yy, 0, GETDATE()), 0)
	SET @NextValue = 1
ELSE 
	BEGIN
		SET NOCOUNT ON;  	
	
		declare @SQL varchar(500) = nullI

		SET @SQL = 'SELECT MAX ([' +  @ColumnName1 + ']'  + ' +1 ) ' +   + ' FROM  [dbo].[' + @tableName + ']'  		  
		EXEC(@SQL)
	END

Open in new window

1
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 35

Expert Comment

by:Pawan Kumar
ID: 41839971


But, now the need is give value refno 1 if it a new year and refno 1 is not found for new year


Can you explain your requirement in detail ?
0
 

Author Comment

by:RIAS
ID: 41839985
Vitor I am trying your solution.

Pawan,
To explain:
The query needs to return refno:
The  refno in table should begin with 1 in a table every year.
If refno 1 is present then give the next value.
0
 
LVL 35

Accepted Solution

by:
Pawan Kumar earned 1000 total points
ID: 41840024
May be this...try

DECLARE @SQL VARCHAR(1000) = ''
DECLARE @ColumnName AS VARCHAR(100) = 'Qty01'
DECLARE @tableName AS VARCHAR(100) = 'table2'

SET @SQL = '
	
		SELECT 
		
				CASE 
						WHEN CAST(GETDATE() AS DATE) = DATEADD(yy, DATEDIFF(yy, 0, GETDATE()), 0) THEN 1 
				ELSE 
				
						MAX ([' +  @ColumnName + ']'  + ' +1 ) END Output'  
				
				+ ' FROM  [dbo].[' + @tableName + ']'  
			  
EXECUTE(@SQL)

Open in new window

0
 

Author Comment

by:RIAS
ID: 41840083
Any suggestion for this one
ALTER PROCEDURE  [dbo].[L_SelectLastVal_FrmCol]
(
     @tableName varchar(100) = null,
	 @ColumnName1 varchar(100) = null
	
)
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;  	
	
	   declare @SQL varchar(500) = null
	  -- NONCLUSTERED INDEX [NIX__UNQ__UID_]



	SET @SQL = 'SELECT TOP 1 '    
        
			SET @SQL = @SQL +  '[' +  @ColumnName1 + ']'     + ' FROM  [dbo].[' + @tableName + ']  ORDER BY '  	+  '[' +  @ColumnName1 + ']'     +'DESC'	
	
	  SET @SQL = @SQL  	
	  
	  EXEC(@SQL)



		
   
	
END

Open in new window



And


ALTER PROCEDURE  [dbo].[MEDICAL_FindMaxSPRSpPayReq]
AS
BEGIN

        SELECT MAX(SPR#)+1
               FROM SIAL_PAT_REQT
		
				
END

Open in new window

0

Featured Post

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
One of the most important things in an application is the query performance. This article intends to give you good tips to improve the performance of your queries.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

916 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question