Results 1 to 26 of 26

Thread: SQL Server dates but text value

  1. #1

    Thread Starter
    Fanatic Member Ggalla1779's Avatar
    Join Date
    Feb 2006
    Location
    Glasgow
    Posts
    532

    SQL Server dates but text value

    Hi

    Am trying to filter on dates in a query in SQL Server...data has dates in text and my sql dont appear to be working

    ie
    Code:
    SELECT [Date signed] FROM tblContactsCC WHERE ([Date signed] < CAST(N'01/10/2008' AS datetime))
    
    SELECT [Date signed] FROM tblContactsCC WHERE [Date signed] <= convert(datetime, '01/10/2008',103)
    What am I doing wrong.....

  2. #2
    Superbly Moderated NeedSomeAnswers's Avatar
    Join Date
    Jun 2002
    Location
    Manchester uk
    Posts
    2,657

    Re: SQL Server dates but text value

    So is 'Date signed' a Date column or some sort of Text column ?
    Please Mark your Thread "Resolved", if the query is solved & Rate those who have helped you



  3. #3

    Thread Starter
    Fanatic Member Ggalla1779's Avatar
    Join Date
    Feb 2006
    Location
    Glasgow
    Posts
    532

    Re: SQL Server dates but text value

    this 1 is an nvarchar.....I have another table....its datetiem works perfect using routine above.

    Can I filter on text values??

  4. #4
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: SQL Server dates but text value

    If it is in NVarchar format you can't cast (or convert) the incoming filter to a date type it must stay string. This will cause issues with the results of course. You could use a Convert on the field and Cast (or Convert) that to a Date type but if the table is large you might have issues as the query will not use any index on the column. You could create an index again casting (or converting) the field to the Date data type and use a hint in the query to use that index if required.
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  5. #5

    Thread Starter
    Fanatic Member Ggalla1779's Avatar
    Join Date
    Feb 2006
    Location
    Glasgow
    Posts
    532

    Re: SQL Server dates but text value

    yes thats good idea...not my routines so not sure if I can modify import routines and field to timedate....thanks for help

  6. #6
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: SQL Server dates but text value

    Not saying to modify the field to datetime but cast it to datetime in the query

    SELECT [Date signed] FROM tblContactsCC WHERE convert(datetime, [Date signed] , 103) <= convert(datetime, '01/10/2008',103)
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  7. #7

    Thread Starter
    Fanatic Member Ggalla1779's Avatar
    Join Date
    Feb 2006
    Location
    Glasgow
    Posts
    532

    Re: SQL Server dates but text value

    yeah i got that, was just saying ideally would have liked to have fixed it by getting value in datetime

    I see what you mean about query...I was going to create a new field but your query saves me doing that.....

    OK ran it on the table its getting errors.... have filtered out nulls and 0's but its now saying it doesnt like a date "Conversion failed when converting the nvarchar value '28/03/2008' to data type int." User data you gotta love it!!

  8. #8
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: SQL Server dates but text value

    So the date value is in DD/MM/YYYY you will need to set the conversion to that type. Hopefully the data is all done the same way
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  9. #9

    Thread Starter
    Fanatic Member Ggalla1779's Avatar
    Join Date
    Feb 2006
    Location
    Glasgow
    Posts
    532

    Re: SQL Server dates but text value

    I dont think its is....... i suspect I might have to make a field up and populate it with values that can be converted...unless theres a way to error trap the offending strings that are non date

  10. #10
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: SQL Server dates but text value

    If the data is not consistant then I think you are in trouble. Maybe your only choice will be to add a field and convert into there using an SP to start then Use a trigger to fire after insert to continue poulating over time.
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  11. #11

    Thread Starter
    Fanatic Member Ggalla1779's Avatar
    Join Date
    Feb 2006
    Location
    Glasgow
    Posts
    532

    Re: SQL Server dates but text value

    How would I code a trigger? and what it it hits error?

    Never done a trigger before.....

  12. #12
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: SQL Server dates but text value

    A trigger is very similar to a stored procedure. You can code Try Catch blocks in SQL Server 2005 and up.

    If it errors on on version of the date type you try the other. But that still leaves a lot of holes.

    7/12/2010 this will convert both ways July 12th 2010 or Dec 7, 2010 which is it? How can you tell? Is there some thing in the row that shows where the data came from you could possible use that to decide which way the data should be converted.
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  13. #13

    Thread Starter
    Fanatic Member Ggalla1779's Avatar
    Join Date
    Feb 2006
    Location
    Glasgow
    Posts
    532

    Re: SQL Server dates but text value

    ok i think i will add extra field and populate..... and on next import check for new dates and change where possible.... if it cant be converted flag in report to user.

    its really upto the user to check and make sure the data in the table is right ie not mixed up date with dd mm

  14. #14
    Superbly Moderated NeedSomeAnswers's Avatar
    Join Date
    Jun 2002
    Location
    Manchester uk
    Posts
    2,657

    Re: SQL Server dates but text value

    If it is in NVarchar format you can't cast (or convert) the incoming filter to a date type it must stay string.
    Yep that's why i was asking, but it looks as though Gary has this all wrapped up :0)
    Please Mark your Thread "Resolved", if the query is solved & Rate those who have helped you



  15. #15

    Thread Starter
    Fanatic Member Ggalla1779's Avatar
    Join Date
    Feb 2006
    Location
    Glasgow
    Posts
    532

    Re: SQL Server dates but text value

    am still getting problems when I try to update the new date values into the new field

    How would I write a function to do igonre the problems...I wrote this in vba

    vb Code:
    1. If IsNull(myDate) Or myDate = "" Or Len(myDate) < 8 Then
    2.         'ConvertDate = Null
    3.     Else
    4.         ConvertDate = CDate(myDate)
    5.     End If

  16. #16

    Thread Starter
    Fanatic Member Ggalla1779's Avatar
    Join Date
    Feb 2006
    Location
    Glasgow
    Posts
    532

    Re: SQL Server dates but text value

    this is what I have so far, but need to throw out the 0 and nulls

    vb Code:
    1. set ANSI_NULLS ON
    2. set QUOTED_IDENTIFIER ON
    3. GO
    4. ALTER FUNCTION [dbo].[ConvertDate](@InDateTime nvarchar)
    5. RETURNS datetime
    6. AS
    7. BEGIN
    8.     DECLARE @MyOutput datetime
    9.     SET @MyOutput = CONVERT(datetime,@InDateTime,103)
    10.     RETURN @MyOutput
    11. END

  17. #17

    Thread Starter
    Fanatic Member Ggalla1779's Avatar
    Join Date
    Feb 2006
    Location
    Glasgow
    Posts
    532

    Re: SQL Server dates but text value

    This is where I got to, giving me errors...doesnt like the IF and isnull??

    vb Code:
    1. set ANSI_NULLS ON
    2. set QUOTED_IDENTIFIER ON
    3. GO
    4. ALTER FUNCTION [dbo].[ConvertDate](@InDateTime nvarchar)
    5. RETURNS datetime
    6. AS
    7. BEGIN
    8.     DECLARE @MyOutput datetime
    9.  
    10.     if isnull(@InDateTime) or Len(@InDateTime)< 8 then
    11.             --SET @MyOutput = ""
    12.             RETURN @MyOutput
    13.     else
    14.             SET @MyOutput = CONVERT(datetime,@InDateTime,103)
    15.             RETURN @MyOutput
    16.     end if
    17. END

  18. #18
    New Member
    Join Date
    Feb 2010
    Posts
    1

    Re: SQL Server dates but text value

    I accept with code: 1.
    set ANSI_NULLS ON
    2.
    set QUOTED_IDENTIFIER ON
    3.
    GO
    4.
    ALTER FUNCTION [dbo].[ConvertDate](@InDateTime nvarchar)
    5.
    RETURNS datetime
    6.
    AS
    7.
    BEGIN
    8.
    DECLARE @MyOutput datetime
    9.
    SET @MyOutput = CONVERT(datetime,@InDateTime,103)
    10.
    RETURN @MyOutput
    11.
    END


    __________________
    Watch The Wolfman Online Free

  19. #19
    Superbly Moderated NeedSomeAnswers's Avatar
    Join Date
    Jun 2002
    Location
    Manchester uk
    Posts
    2,657

    Re: SQL Server dates but text value

    How about this -

    set ANSI_NULLS ON
    set QUOTED_IDENTIFIER ON
    GO
    ALTER FUNCTION [dbo].[ConvertDate](@InDateTime nvarchar)
    RETURNS datetime
    AS
    BEGIN
    DECLARE @MyOutput datetime

    if @InDateTime = null or Len(@InDateTime)< 8
    begin
    RETURN @MyOutput
    end
    else
    begin
    SET @MyOutput = CONVERT(datetime,@InDateTime,103)
    end
    RETURN @MyOutput
    END
    obviously you still need to finish it off !
    Please Mark your Thread "Resolved", if the query is solved & Rate those who have helped you



  20. #20

    Thread Starter
    Fanatic Member Ggalla1779's Avatar
    Join Date
    Feb 2006
    Location
    Glasgow
    Posts
    532

    Re: SQL Server dates but text value

    thanks guys, I tried this morning needsomeanswers (its weird calling you that name!!) wee changes to my routine...and hey presto its now working!!

    I might go back and see if I can hook this up to Garys select SQL

    many thanks for your help guys

  21. #21
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: SQL Server dates but text value

    If it won't be a big effort to modify the client codes, I would suggest you convert it to a date field, instead of nvarchar. A date should be stored in date field and this is a perfect example of what happens if you don't.
    If there's a lot of effort involved or otherwise not possible due to any other reason, I'd suggest you add a calculated field to your table and work with that. Calculated fields are populated automatically, so no trigger etc. would be required (unless your data is really badly messed up).
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  22. #22

    Thread Starter
    Fanatic Member Ggalla1779's Avatar
    Join Date
    Feb 2006
    Location
    Glasgow
    Posts
    532

    Re: SQL Server dates but text value

    I am adapting a "user created mess" of access database and excel pivot tables into my lovely nicely designed database.... so at first I normally I take whats there change what I can..... of course sometimes time and users stop this happening 100&#37; but once its mine I can move it towards a cleaner normal database app.

  23. #23
    Superbly Moderated NeedSomeAnswers's Avatar
    Join Date
    Jun 2002
    Location
    Manchester uk
    Posts
    2,657

    Re: SQL Server dates but text value

    No problem,

    remember in TSql you need to have Begin / End tags around your If conditions and the ISNULL() function is for use in-line in a select statement so you can replace Nulls with another character(s) e.g

    SELECT name, age, ISNULL(height,'N/A') from person where id = 1

    Oh and my name is Rich by the way :0)
    Please Mark your Thread "Resolved", if the query is solved & Rate those who have helped you



  24. #24

    Thread Starter
    Fanatic Member Ggalla1779's Avatar
    Join Date
    Feb 2006
    Location
    Glasgow
    Posts
    532

    Re: SQL Server dates but text value

    ok this appeared simple but its not outputing the datetime value in Update or make table query.... am confused

    vb Code:
    1. UPDATE    tblContactsCC SET  DateSigned = dbo.ConvertDate([Date signed])

    Just getting nulls....field DateSigned is datetime output from function is @MyOutput datetime

    Any idea why its not liking it?

  25. #25

    Thread Starter
    Fanatic Member Ggalla1779's Avatar
    Join Date
    Feb 2006
    Location
    Glasgow
    Posts
    532

    Re: SQL Server dates but text value

    thanks Rich for help, Tsql is not 1 of my strengths

  26. #26
    Superbly Moderated NeedSomeAnswers's Avatar
    Join Date
    Jun 2002
    Location
    Manchester uk
    Posts
    2,657

    Re: SQL Server dates but text value

    before you try an Update why don't you try a Select statement. That should show you what you are getting out of Convert Date

    something like this -

    SELECT myDate, dbo.ConvertDate(MyDate) from table

    This way you get your initial Date, and also your converted date. This should give you an idea of why it is not working.
    Please Mark your Thread "Resolved", if the query is solved & Rate those who have helped you



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width