Solved

Start a field with integer 1

Posted on 2016-10-12
7
36 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 45

Expert Comment

by:Vitor Montalvão
Comment Utility
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
Comment Utility
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 45

Assisted Solution

by:Vitor Montalvão
Vitor Montalvão earned 250 total points
Comment Utility
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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 17

Expert Comment

by:Pawan Kumar Khowal
Comment Utility


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
Comment Utility
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 17

Accepted Solution

by:
Pawan Kumar Khowal earned 250 total points
Comment Utility
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
Comment Utility
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

743 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now