change select statement value using sql - sql

I have some SQL below which is programmatically generated:
INSERT INTO TABLE (COMID, NAME, DATE)
SELECT DISTINCT 'COM001', 'John', '01-Jan-4501 00:00:00'
How can i amend this so that if it finds the date time of : 01-Jan-4501 00:00:00 then it replaces it with todays date?

You probably want to avoid some wrong or irrelevant values.
SELECT
'COM001',
'John',
case
when cast('01-Jan-4501 00:00:00' as date) > cast('01-Jan-2100 00:00:00' as date)
then cast(getdate() as varchar(30))
else '01-Jan-4501 00:00:00'
end
Another way is to create a trigger on that table that automatically makes the date field correction.

SELECT DISTINCT 'COM001', 'John',
case when date = '01-Jan-4501 00:00:00' then convert(date,getdate()) end

Related

Convert Dates in SQL Server

I'm trying to perform a query in SQL Server. I'm having trouble filtering the date. The output is always the same, the data doesn't get filter by date.
The date comes in the following format:
29-12-2020 16:38
31-12-2020 17:43
I tried to filter doing all the following but none worked out:
select
start_date
from table_1
where start_date between '01-01-2021 00:00:00' and '31-01-2021 00:00:00'
select
start_date
from table_1
where start_date between '01-01-2021 00:00' and '31-01-2021 00:00'
select
start_date
from table_1
where start_date between '01-01-2021' and '31-01-2021'
I also tried to cast it but I didn't have luck:
select
cast(start_date as date) as start_date
from table_1
where start_date between '2021-01-01' and '2021-01-31'
Can anyone help me?
Regards
You have a string. I strongly suggest making it a date/time of some sort. The following conversion works:
select convert(datetime, left(str, 10), 105) + convert(datetime, right(str, 5))
from (values ('29-12-2020 16:38')) v(str);
One operation is a computed column:
alter table table_1 start_date_dt as
( convert(datetime, left(start_date, 10), 105) + convert(datetime, right(start_date, 5)) )
Then you can use this value for your where clause. Or better yet. Fix the data! Don't store values in strings when there is an appropriate data type.

SQL Server : not getting last date with between query

What am I doing wrong? I have an query to get data between two dates, but it does not get the last date.
I tried
select *
from data
where dates between '2016-01-01' and '2016-01-02'
and
select *
from data
where dates >= '2016-01-01' and dates <= '2016-01-02'
I need SQL Server to return data between 2016-01-01 and 2016-01-02
Output should be
pid name date
-------------------------------
1 test2 2016-01-02
2 test2 2016-01-01
3 test3 2016-01-02
This works
select *
from data
where dates >= '2016-01-01' and dates <= '2016-01-03'
but why do I need to add an extra day?
Presumably your column (mis)named dates has a time component. This should work for what you want:
select *
from data
where dates >= '2016-01-01' and dates < '2016-01-03';
Aaron Bertrand, a SQL Server guru, has an excellent blog post on this subject, What do BETWEEN and the devil have in common?. That is a good place to start.
Alternatively, you could write the query as:
select *
from data
where cast(dates as date) >= '2016-01-01' and dates <= '2016-01-02';
Once you remove the time component, the comparisons will work. However, I'm reluctant to use functions on columns, because that can preclude the use of indexes in the query (SQL Server makes an exception for conversion to date).
I agree with #GordonLinoff and was devising an example to show you so I will put it here anyway for you. note you can also just drop off the time component from your field by casting to a date and it will work.
WHERE CAST([date] as DATE) BETWEEN '2016-01-01' and '2016-01-02'
Example so it is apparent
DECLARE #Data AS TABLE (pid INT, name VARCHAR(10), [date] date, [datetime] DATETIME)
INSERT INTO #Data (pid, name, [date], [datetime])
VALUES
(1, 'test1', '2016-01-02', '2016-01-02')
,(2, 'test2', '2016-01-01', '2016-01-01')
,(3, 'test3', '2016-01-02', '2016-01-02')
,(3, 'test3', '2016-01-02', '2016-01-02 11:00:00.000')
SELECT *
FROM
#Data
WHERE
[date] BETWEEN '2016-01-01' and '2016-01-02'
SELECT *
FROM
#Data
WHERE
[datetime] BETWEEN '2016-01-01' and '2016-01-02'
SELECT *
FROM
#Data
WHERE
CAST([datetime] AS DATE) BETWEEN '2016-01-01' and '2016-01-02'
Are your columns datetime or just dates? did you try with convert functions?
e.g.
CONVERT(VARCHAR(10),dates,110) --> 16-06-2016
i guess you have datetime columns which means that comparing with today's date,
if the column is today then >= will always return true and <= will always return false, because timestamp being persisted to DB will probably be >= of today 00:00:00
If the type of your column dates is DATETIME, the query
select * from data
where dates >= '2016-01-01' and dates <= '2016-01-02'
only pick up records that have dates between 2016-01-01 00:00:00.000 and 2016-01-02 00:00:00.000.
It will not pick up the records that have dates > 2016-01-02 00:00:00.000. If you want to the query to pick up all records until 2016-01-02 23:59:59.999, then you will have to use your this query:
select * from data
where dates >= '2016-01-01' and dates < '2016-01-03'

Check date between two dates in T-SQL

I have a stored procedure where want check that a date is between a fixed date and the current date/time (with GETDATE()):
SELECT
a, b
FROM myTbl
WHERE
DATE BETWEEN 'Day start datetime' AND GETDATE()
...for example :
WHERE
DATE BETWEEN '2013-09-10 00:00:00.00' AND 'GETDATE()'
How to do it?
A pair of DATEADD/DATEDIFF calls will round a date down to the previous midnight:
SELECT a , b
FROM myTbl
WHERE DATE BETWEEN DATEADD(day,DATEDIFF(day,0,GETDATE()),0) and GETDATE()
Alternatively, if you're on SQL Server 2008 or later:
SELECT a , b
FROM myTbl
WHERE DATE BETWEEN CONVERT(date,GETDATE()) and GETDATE()
'GETDATE()' is a string literal, GETDATE() is a T-SQL function.
Your query should look like:
SELECT a , b
FROM myTbl
WHERE DATE BETWEEN '2013-09-10 00:00:00.0' and GETDATE()
I think WHERE DATE BETWEEN '2013-09-10 00:00:00.00' and GETDATE() (without the single quotes around the GETDATE() call) should work just fine.
You can get Day start date time by converting Getdate() return value to a Date type and again to Datetime as below.
select a,b
from myTbl
where [date] between convert(datetime,convert([date], getdate()))
and getdate()

SQL group by month and add 15mins

I have a table with data every 15mins.
I have this query :
Select Sum(Value) From MyTable Group By month(myDate), year(myDate)
However the result is not what i want because I have values from
'2013-01-01 00:00:00.000' to '2013-01-31 23:45:00.000' and I need to have values from
'2013-01-01 00:15:00.000' and '2013-02-01 00:00:00.000'
How can I change the request to have the correct date range ?
Thanks
Use DATEADD method:
SELECT Sum(Value)
FROM MyTable
GROUP BY MONTH(DATEADD(minute,15,myDate)), YEAR(DATEADD(minute,15,myDate))

How to display result in two column form one column resource with different where

I try to count record which is on current date and pass date from date column
DATE
'2013-03-04 00:00:00'
'2013-02-04 00:00:00'
'2013-02-04 00:00:00'
if today is '2013-03-04 00:00:00'
the result should be
CURRENT_DATE = 1
PASS_DATE = 2
I Don't know how to query it form one resource but different where condition
1st - date >= '2013-03-04 00:00:00'
2nd - date < '2013-03-04 00:00:00'
PLEASE HELP
I suggest using two sub-queries within a SELECT clause
select
(select count(*) from MyDates where DateValue < getdate()) as PastDate,
(select count(*) from MyDates where DateValue >= getdate()) as CurrentDate
You can replace getDate() with a date parameter or a hard-coded value such as '2013-03-04' if you wish.
One way to do it is to query for all dates and place 1\0 according to your condition
then just query again from the table an sum the columns.
SELECT sum([CURRENT_DATE]), sum([PASS_DATE])
FROM
(
SELECT
ID,
case when myDate >= getDate() then 1 else 0 end as [CURRENT_DATE],
case when myDate < getDate() then 1 else 0 end as [PASS_DATE]
FROM mytable
) as SourcTbl
Here is a SQL Fiddle http://sqlfiddle.com/#!3/e9c19/8