logo
down
shadow

sql query case dateadd


sql query case dateadd

By : Korey Graham
Date : November 22 2020, 01:01 AM
This might help you Can I use GetDate and DateAdd in a SQL Query Select Case statement? Part of my query is as follows but it only returns OVER60 as the result regardless of client's Date of Birth. Is there a better way to accomplish this? I'm a novice at this and it's my first question to Stack Overflow so I thank you for any insight. , Assuming SQL Server despite MySQL tag This should do it:
code :
SELECT  G.ClientID, CONVERT(VARCHAR(10), ClientDob, 101) AS ClientDob
, CASE 
    WHEN (G.ClientDob > dateadd(year, -13, GetDate()))THEN 'Under13' 
    WHEN (G.ClientDob > DATEADD(year, -20, GETDATE()))THEN '13-19'
    WHEN (G.ClientDob > DATEADD(Year, -30, GETDATE())) THEN '20-29'
    WHEN (G.ClientDob > DATEADD(YEAR, -45, GETDATE())) THEN '30-44'
    WHEN (G.ClientDob > DATEADD(YEAR, -60, GETDATE())) THEN '45-59'
    Else 'OVER60' 
END AS AgeCategory


Share : facebook icon twitter icon
SQL Efficiency - Query using dateAdd Function twice; or SubQuery and DateAdd Function once; on Date BETWEEN

SQL Efficiency - Query using dateAdd Function twice; or SubQuery and DateAdd Function once; on Date BETWEEN


By : user3140794
Date : March 29 2020, 07:55 AM
To fix the issue you can do I don't think it matters which of the two you use. And the Execution Plans agree.
But it seems you are doing calculations to column itsm_requiredbyx and then check if the result is between two external values, @DateFrom and @DateTo. This way, all datetimes from this field are processed by the functions before the WHERE conditions can be applied and no index can be used. The second link (Ten Common SQL Programming Mistakes) in @DOK's answer provide more detailed info on why and when this happens.
WHERE CASE with DATEADD

WHERE CASE with DATEADD


By : Ashwin Jeyaseelan
Date : March 29 2020, 07:55 AM
will be helpful for those in need I am trying to write a SQL the will pull up either all date ranges or those past 90 days. Everything work but the following code where the code desided what date to look at. , I think this will do it for you:
code :
WHERE 
    (@All_90 = 1 AND td.DateSeen NOT BETWEEN DATEADD(day,-90,GETDATE()) and GETDATE())
    OR (@All_90 = 0 AND td.DateSeen BETWEEN DATEADD(day,-90,GETDATE()) and GETDATE())
Strange behaviour in CASE expression using dateadd

Strange behaviour in CASE expression using dateadd


By : user3915079
Date : March 29 2020, 07:55 AM
wish helps you case is an expression whose type is determined at compile-time. However, the values are not actually evaluated until run-time.
By the rules of type-conversion, I think that the expression will return a datetime. That results in an error for the 'foo' and 'bar' values -- when the when conditions are met.
Using case expression in DATEADD interval

Using case expression in DATEADD interval


By : Szabó Márk
Date : March 29 2020, 07:55 AM
seems to work fine Is possible to use case expression inside of DATEADD interval parameter? , Try below..
code :
select * from T 
where case c1 when 1 then DATEADD(HOUR, c2, date) 
when 2 then DATEADD(DAY, c2, date) 
end < Getdate()
Can you use CASE inside another CASE and DATEADD() function in a T-SQL statement?

Can you use CASE inside another CASE and DATEADD() function in a T-SQL statement?


By : user3551495
Date : March 29 2020, 07:55 AM
To fix this issue I am creating a query that will report tools and equipment with their required maintenance status. , From what I understood of your question this is what you want:
code :
WHEN
  CASE MRB.MaintenanceReqName 
    WHEN 'Bi-Annual' THEN DATEADD(month, 6, MS.LastDateDue) 
    WHEN 'Monthly' THEN DATEADD(month, 1, MS.LastDateDue) 
    WHEN 'Weekly' THEN DATEADD(week, 1, MS.LastDateDue)  
  END < GETDATE() 
THEN 'PAST DUE'
Related Posts Related Posts :
  • How to handled the null values in WHERE IN condition in mysql
  • Is it okay to update primary key field to shift a record to the top?
  • Stop query through pdo
  • How to check whether particular value in field in MySQL exists
  • Execute a statement for every table in a database
  • insertion 74G data into mysql table cost more then 2 days, how to improve insert performance
  • Where can I see the differents "Hibernate Console Configurations" in Eclipse
  • MySql | relational database vs non relational database in terms of Performance
  • Exporting mysql database using mysqldump including procedures
  • Trigger MySQL not working
  • How do I update values for individual rows quickly?
  • MySQL join query with multiple where condition
  • MySQL stored procedure is not working
  • MySQL - Order By exact match at the end
  • Select max date or null date from same table MySQL
  • Best solution workaround for needing a mysql event on insert?
  • MySQL select several columns of several tables at the same time without using JOIN
  • Is it more efficient to have a table with one of its columns mostly empty or make a new table and refer to it?
  • mysql data truncated with ñ character
  • MySQL Distinct Active Users for the last month query
  • Get all rows that match another attribute in the same table
  • MySQL statement to get min value with max ID
  • DATEDIFF Current/Date for Last Record
  • What is the logic or procedure for combining multiple DB query's results into one elegant table?
  • Modify TIMESTAMP MySQL for use as Primary Key
  • Django - Efficiently bulk create inherited models
  • MySQL Dual table Join Dual table
  • Ruby on Rails - database where sorted by id
  • MySQL 5.6 : Access denied for user 'root' @ 'localhost' when password contains backslash
  • Click on one button multiple times and mySql
  • Select daily amount of specific values from same column
  • How to send e-mail to users within a database?
  • Updating self joined table
  • Convert query to a Left-Join (?) to include userid's without a match
  • How to Convert from .Net TimeZone to MySQL TimeZone
  • Syntax Error for MySQL Trigger
  • SQL Optimization - 3 Tables - Multiple SUMs - 20k records - 12 Seconds
  • MySQL: Why does query give NULL as a result of SUM?
  • Simple query issue with multiple tables and mismatching IDs
  • MySQL join multiple lines
  • Performing JOIN between two tables
  • Get specific rows from group by clause in PostgreSQL
  • MySQL query to get the sum of a column
  • Why mongoDB takes less time for Select than Fetch time?
  • Laravel 4.2 BIT datatype issue
  • MySQL Join Query confuse
  • Select distinct rows by count in descending order
  • Select query with comma and join
  • Adding a percentage (%) sign to each value while using select sql statement
  • How to access data from foreign key table
  • How to use the SQL MINUS keyword
  • Mysql Left Join statement ignored during query
  • Mysql version is different in phpmyadmin
  • MySQL: creating comma separated list and using in IN
  • calculating total time using timestamp
  • SQL LIKE REGEXP Matching Single Value and Comma'd String
  • one table or several table in mysql
  • Selecting rows with null in integer columns using mysql query
  • Using MySQL Database Triggers
  • Trying to get TOP 10 products per sales channel
  • shadow
    Privacy Policy - Terms - Contact Us © ourworld-yourmove.org