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

  • Home
  • SEARCH
  • 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 7458683
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T13:28:50+00:00 2026-05-29T13:28:50+00:00

I need to filter a field as date, but the field is not typed

  • 0

I need to filter a field as date, but the field is not typed date or datetime.

SELECT champ_save_id, element_id, valeur
FROM g_champ_save AS cs
INNER JOIN g_champ AS c ON cs.champ_id = c.champ_id
WHERE cs.valeur <> '' AND NOT cs.valeur IS NULL
AND c.champ_code = 'qualif_valide_qualif_fin'
AND CONVERT(datetime, cs.valeur, 103) <= @filterdate

It throws an error (Conversion failed when converting datetime from character string) unless I comment the last line.
As I understand it, the SQL optimizer first executes the CONVERT function on every valeur field and , then filters on the other conditions, then joins. So it tries to CONVERT fields with “regular” text in it, so of course it does not work.

I tried with a CTE but it does exactly the same thing.

WITH valeurs AS (
    SELECT champ_save_id, element_id, valeur
    FROM g_champ_save AS cs
    INNER JOIN g_champ AS c ON cs.champ_id = c.champ_id
    WHERE cs.valeur <> '' AND NOT cs.valeur IS NULL
    AND c.champ_code = 'qualif_valide_qualif_fin'
    AND ISDATE(convert(datetime, cs.valeur, 103)) = 1
)
SELECT * FROM valeurs
WHERE CONVERT(datetime, valeur, 103) <= @filterdate

Do you have any idea which would allow me to be absolutely sure to first return the fields containing dates, and then filter them ?

The tables are designed like that (shortened for clarity) :

CREATE TABLE g_champ (
    champ_id int IDENTITY(1,1) NOT NULL,
    type_id int NOT NULL, -- defines the type : text, checkbox, date, etc
    champ_code nvarchar(50) NULL,
    champ_label nvarchar(max) NULL
)

CREATE TABLE g_champ_save (
    champ_save_id int IDENTITY(1,1) NOT NULL,
    champ_id int NOT NULL,
    element_id int NULL,
    valeur nvarchar(max) NULL
)

Thanks

  • 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-05-29T13:28:51+00:00Added an answer on May 29, 2026 at 1:28 pm

    I think you need to use IsDate:

    SELECT champ_save_id, element_id, valeur
    FROM g_champ_save AS cs
    INNER JOIN g_champ AS c ON cs.champ_id = c.champ_id
    WHERE cs.valeur <> '' AND NOT cs.valeur IS NULL
    AND c.champ_code = 'qualif_valide_qualif_fin'
    AND CONVERT(datetime, case IsDate(cs.valeur) when 1 then cs.valeur end, 103) 
        <= @filterdate
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to filter alphabetic and non alphanumeric characters from a string to make
I need to filter multiple rows in dataview. Here I used rowfilter. But, I
We have a date field in one of our tables and i need to
My table has a Date field, from which I would like to query all
I need filter this view: function (doc) { if (doc.doc_type == 'asd'){ emit([doc.date, doc.string_key_0,
Here is what I have currently that does not filter anything by createdDate. SELECT
I have a Post model that has a datetime.date field for posted_date . I
I have a table with five fields, and I need to filter it to
I need to filter out strings that contain anything other than letters, numbers, dots,
I need to filter the data using a filter where CategoryId = 75 and

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.