how to call this function of date

  • http://www.tek-tips.com/faqs.cfm?fid=4191

    How to call that function, give me the sql.

  • First you have to create the function in your database, then you would put the function in the SELECT clause of your query with the datetime column/variable/parameter and one of the format types handled by the function. So it would look something like this:

    Select fn_formatdatetime (getdate(), 'm/d/yy')

    I'll add one caveat, scalar UDF's like this kill performance. You would be much better off handling formatting in the UI.

  • it's fairly simply, just pass in the format string you want to use; if you are familiar with the CONVERT(date,StyleNumber), many of the typical formats are built in:

    declare @TheDate datetime

    SET @TheDate = getdate()

    --it's simply

    SELECT dbo.fn_formatdatetime (@TheDate ,'YYYY-MM-DD')

    Lowell


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

  • Thanx guys

    but how to mark as answered so you get points in the forum.

  • just the "thank you" you already gave us is enough; we all apreciate receiving that!

    Lowell


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

  • I'll add one caveat, scalar UDF's like this kill performance. You would be much better off handling formatting in the UI.

    what you mean, do u mean that the fun is too big?

  • No I don't mean the function is too big, I mean that the UDF will have to be called for every row in the dataset and, depending on what it does it could cause problems. That one isn't too bad because it doesn't access another table. Just don't use it on a column in the where clause.

Viewing 7 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic. Login to reply