Solved

Start a field with integer 1

Posted on 2016-10-12
7
51 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 47

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 47

Assisted Solution

by:Vitor Montalvão
Vitor Montalvão earned 250 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
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 28

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 28

Accepted Solution

by:
Pawan Kumar earned 250 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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
What's wrong with this T-SQL Foreign Key? 7 46
SQL Improvement  ( Speed) 14 28
How to construct an if else statement from existing code 6 21
sql server query 6 9
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

809 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