Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7865513
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T00:06:41+00:00 2026-06-03T00:06:41+00:00

I know how to convert to date in SQL Server (T-SQL) but how do

  • 0

I know how to convert to date in SQL Server (T-SQL) but how do I create a UDF so that I can call it each time in my code?

Example 1: The below code will format this 20120428 to 04/28/2012

SELECT CONVERT(CHAR(10), CONVERT(DATE, TEST_DATE), 101) AS MY_DATE
FROM
MEMBER
WHERE ISDATE(TEST_DATE) <> 0

Example 2: The below code will format this 20120428 to 2012-04-28

SELECT CONVERT(DATE, TEST_DATE) AS MY_DATE
FROM
MEMBER
WHERE ISDATE(TEST_DATE) <> 0

Thank for the input!

Guy

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T00:06:43+00:00Added an answer on June 3, 2026 at 12:06 am

    For the second example you can do this:

    CREATE FUNCTION dbo.ConvertDate(@d CHAR(10))
    RETURNS DATE
    AS
    BEGIN
        RETURN (SELECT CONVERT(DATE, @d));
    END
    GO
    

    But a more flexible approach might be:

    CREATE FUNCTION dbo.ConvertRegional
    (
      @d CHAR(10),
      @style TINYINT
    )
    RETURNS CHAR(10)
    AS
    BEGIN
        RETURN (SELECT CONVERT(CHAR(10), CONVERT(DATE, @d), @style));
    END
    GO
    
    DECLARE @d CHAR(10);
    SELECT @d = '20120428';
    
    SELECT 
     dbo.ConvertDate(@d),
     dbo.ConvertRegional(@d, 101),
     dbo.ConvertRegional(@d, 103),
     dbo.ConvertRegional(@d, 120);
    

    Results:

    ----------  ----------  ----------  ----------
    2012-04-28  04/28/2012  28/04/2012  2012-04-28
    

    If you don’t want to continue filtering out bad non-dates from your source table (keeping the ISDATE() in the WHERE clause should prevent the function from having to deal with those rows), you can change the function this way to avoid errors, if NULL is okay as a substitute:

    CREATE FUNCTION dbo.ConvertRegional
    (
      @d CHAR(10),
      @style TINYINT
    )
    RETURNS CHAR(10)
    AS
    BEGIN
        RETURN (SELECT CASE WHEN ISDATE(@d) = 1 THEN
          CONVERT(CHAR(10), CONVERT(DATE, @d), @style)
          END);
    END
    GO
    

    In SQL Server 2012, you can do this instead, which does the same thing without having to write your own CASE:

    CREATE FUNCTION dbo.ConvertRegional
    (
      @d CHAR(10),
      @style TINYINT
    )
    RETURNS CHAR(10)
    AS
    BEGIN
        RETURN (SELECT CONVERT(CHAR(10), TRY_CONVERT(DATE, @d), @style));
    END
    GO
    

    (In fact in SQL Server 2012 you can also use FORMAT() so that you don’t have to memorize the style numbers, but since I don’t know what version you’re using I’ll leave that for another day.)

    All that said, other than saving a few keystrokes in your queries, this encapsulation is actually going to make your queries perform worse (depending on where they are used). For simple conversions like this it is much better to just perform them inline in most cases.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i know there is a convert -swirl effect in imagemagick but can i create
I don't know how to convert the type for name so that each element
Anyone know of a nice efficient function that could convert, for example: HelloWorld -->
I would like to convert an Oracle SQL query into SQL server query. But
I need to know how to convert a date time of GMT time zone
So I know I can convert a string to a hashcode simply by doing
I would like to know how to convert parent object that was return by
I get a String data from Cursor, but I don't know how to convert
Does any on know how can I convert this $events = array( array(type =>
I am using the exec(query_string) command on sql server. The matter is that the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.