Solved

Writing Functions in T-SQL

Posted on 2009-05-07
6
262 Views
Last Modified: 2012-05-06
I am having problems specifying the date argument in this question. Any idea

Write a function named twoDigitYearPart that accepts a datetime as an argument and returns the last two digits of the year as a char(2) type.
 

CREATE FUNCTION dbo.twoDigitYearPart (
 
@date date )
 
Returns Char (2)
 
as
 
BEGIN
 
RETURN (SELECT CASE WHEN @date THEN @date = @date char(2) END)
 
end

Open in new window

0
Comment
Question by:iolike
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
6 Comments
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 24333141
RETURN (SELECT YEAR(@Date) )
0
 

Author Comment

by:iolike
ID: 24333195
when I  execute the following,  I don't get 08. Any suggestions

SELECT dbo.twoDigitYearPart (2008)
0
 
LVL 3

Expert Comment

by:GregTSmith
ID: 24333205
The YEAR function returns an integer.

You can specify a format for the date using the CONVERT function.  (101 is MM/DD/YYYY)

Try this...
CREATE FUNCTION dbo.twoDigitYearPart(@date datetime) 
 
RETURNS char(2) 
 
AS 
 
BEGIN
 
	DECLARE @output char(2) 
	SET @output = RIGHT(CONVERT(varchar, @date, 101), 2) 
	RETURN @output 
 
END 
 
GO 

Open in new window

0
Why You Need a DevOps Toolchain

IT needs to deliver services with more agility and velocity. IT must roll out application features and innovations faster to keep up with customer demands, which is where a DevOps toolchain steps in. View the infographic to see why you need a DevOps toolchain.

 

Author Comment

by:iolike
ID: 24333264
Althoug I execute SELECT dbo.twoDigitYearPart5 (2008), I get 05 as result.  It returns 05 regardless what year I execute
0
 
LVL 75

Accepted Solution

by:
Aneesh Retnakaran earned 500 total points
ID: 24333298

CREATE FUNCTION dbo.twoDigitYearPart (
@date date )
Returns Char (2)
as
BEGIN
RETURN (SELECT RIGHT( YEAR(@Date),2) )
end
go
SELECT dbo.twoDigitYearPart('2008-02-02')
0
 
LVL 3

Expert Comment

by:GregTSmith
ID: 24333329
I wouldn't have expected the SQL Server to accept an integer.  Odd...

The SQL Server seems to be returning the date representing the number of days since 1/1/1900 (for 2008 that's 6/2/1905, so the function returns '05').  You should pass a datetime value instead.  (i.e. SELECT dbo.twoDigitYearPart('1/1/2008') )

Here, you can see the behavior of the SQL Server for yourself:
PRINT CONVERT(datetime, 0) 
PRINT CONVERT(datetime, 2008) 
PRINT dbo.twoDigitYearPart(GETDATE()) 

Open in new window

0

Featured Post

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Merige returns error code when updating 15 58
Restrict result set 1 47
Format Date fields 11 54
How to create and use encrypted columns in SQL 2005? 15 50
by Mark Wills PIVOT is a great facility and solves many an EAV (Entity - Attribute - Value) type transformation where we need the information held as data within a column to become columns in their own right. Now, in some cases that is relatively…
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

731 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