Solved

how to get year part from nvarchar  date column

Posted on 2012-04-11
11
435 Views
Last Modified: 2012-04-26
how to get year part from nvarchar column having dates in the format 29/10/2008. I just need '2008' from this date
0
Comment
Question by:mmalik15
  • 5
  • 3
  • 2
  • +1
11 Comments
 
LVL 74

Accepted Solution

by:
sdstuber earned 400 total points
ID: 37832802
right(yourcolumn,4)
0
 

Author Comment

by:mmalik15
ID: 37832887
excellent sdstuber.

Can I also ask you (though its not part of orignal question) how to check if the nvarchar column has the format 'dd/mm/yyyy' before using the right function as i have just seen some of the values are not in the right format they are like 'American Journal of Gastroenterology 2005;100(4):936-971' etc for these values I want to keep a blank value for the year
0
 
LVL 18

Assisted Solution

by:deighton
deighton earned 100 total points
ID: 37832958
DECLARE @YOURCOLUMN nvarchar(20);

set @YOURCOLUMN = '29/02/2006';

SELECT CASE WHEN
            ISNUMERIC(LEFT(@YOURCOLUMN,2)) = 1 AND
            ISNUMERIC(SUBSTRING(@YOURCOLUMN, 4,2)) = 1 AND
            ISNUMERIC(RIGHT(@YOURCOLUMN,4)) = 1 AND
            ISDATE(RIGHT(@YOURCOLUMN,4)  + SUBSTRING(@YOURCOLUMN, 4,2) + LEFT(@YOURCOLUMN,2)) = 1
            
            THEN
                  RIGHT(@YOURCOLUMN,4)
            ELSE
                  ''      
            END
0
Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

 
LVL 74

Assisted Solution

by:sdstuber
sdstuber earned 400 total points
ID: 37833046
probably should have been a new question, but you could try something like this...


       CASE
           WHEN     len(yourcolumn) = 10
                AND patindex('__/__/____', yourcolumn) = 1
                AND isnumeric(substring(yourcolumn, 1, 2)) = 1
                AND isnumeric(substring(yourcolumn, 4, 2)) = 1
                AND isnumeric(substring(yourcolumn, 7, 4)) = 1
           THEN
               substring(yourcolumn, 7, 4)
       END
0
 
LVL 74

Expert Comment

by:sdstuber
ID: 37833049
oops too slow, looks like somebody else came up with something similar
0
 

Author Comment

by:mmalik15
ID: 37833240
i m tyring to create a small function for this but getting incorrect syntax error

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

-- =============================================
-- Author:		Attila Kocsis
-- =============================================
Create FUNCTION dbo.GetYearSafe
(
	@datestring as varchar(4000)
)
RETURNS VARCHAR(4)
AS
BEGIN

	IF (ISDATE(@datestring)=0)
	BEGIN
		RETURN '';
	END
   
SELECT CASE WHEN 
            ISNUMERIC(LEFT(@datestring,2)) = 1 AND
            ISNUMERIC(SUBSTRING(@datestring, 4,2)) = 1 AND
            ISNUMERIC(RIGHT(@datestring,4)) = 1 AND
            ISDATE(RIGHT(@datestring,4)  + SUBSTRING(@datestring, 4,2) + LEFT(@datestring,2)) = 1
            
            THEN 
                  return RIGHT(@datestring,4);
END
                 ELSE 
                 RETURN '';
            END

END

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

Open in new window

0
 
LVL 74

Expert Comment

by:sdstuber
ID: 37833287
don't put the return inside the case and you don't want a select, just return the case itself


Create FUNCTION dbo.GetYearSafe
(
      @datestring as varchar(4000)
)
RETURNS VARCHAR(4)
AS
BEGIN

      IF (ISDATE(@datestring)=0)
      BEGIN
            RETURN '';
      END
   
return CASE
           WHEN     len(@datestring) = 10
                AND patindex('__/__/____', @datestring) = 1
                AND isnumeric(substring(@datestring, 1, 2)) = 1
                AND isnumeric(substring(@datestring, 4, 2)) = 1
                AND isnumeric(substring(@datestring, 7, 4)) = 1
           THEN
               right(@datestring,4)
       END            
END
0
 
LVL 74

Expert Comment

by:sdstuber
ID: 37833291
I demonstrated with my case statement but you could use the other case as well
0
 
LVL 9

Expert Comment

by:sarabhai
ID: 37836683
Can you try this,

SELECT YEAR( CONVERT(DATETIME ,'29/10/2008' ,103) ) AS V_Year
0
 
LVL 18

Expert Comment

by:deighton
ID: 37836731
Create FUNCTION dbo.GetYearSafe
(
      @datestring as varchar(4000)
)
RETURNS VARCHAR(4)
AS
BEGIN

      IF (ISDATE(@datestring)=0)
      BEGIN
            RETURN '';
      END

DECLARE @RETVAL varchar(4000);
   --set the return value in the select statement
SELECT @RETVAL = CASE WHEN
            ISNUMERIC(LEFT(@datestring,2)) = 1 AND
            ISNUMERIC(SUBSTRING(@datestring, 4,2)) = 1 AND
            ISNUMERIC(RIGHT(@datestring,4)) = 1 AND
            ISDATE(RIGHT(@datestring,4)  + SUBSTRING(@datestring, 4,2) + LEFT(@datestring,2)) = 1
           
            THEN
                   RIGHT(@datestring,4)

                 ELSE
                  ''
            END

RETURN @RETVAL;

END

GO
0
 

Author Closing Comment

by:mmalik15
ID: 37896709
sorry for delay but excellent comments.
0

Featured Post

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

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

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
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.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

679 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