converting varchar to date - SQL Server - sql

I'm using varchar datatype for my StartDate and EndDate fields in SQL server 2005. The sample query is given here:
*I'm following dd/MM/yyyy format for date.
select * from dbo.SubDetails
where
((
DATEADD(DAY,DATEDIFF(DAY, 0, convert(datetime,StartDate,105)), 0) <=
DATEADD(DAY,DATEDIFF(DAY, 0, convert(datetime,'16/11/2012',105)), 0)
)
AND
(
DATEADD(DAY, DATEDIFF(DAY, 0, convert(datetime,EndDate,105)), 0) >=
DATEADD(DAY, DATEDIFF(DAY, 0, convert(datetime,'11/9/2012',105)), 0)
))
This query is giving me all records irrespective to records between date range.
Any help would be appreciated. Thanks

first of all - do not store date values in varchar columns, if you have to - use YYYYMMDD format.
second - you can compare dates:
select *
from dbo.SubDetails
where
convert(datetime, StartDate, 105) <= convert(datetime,'16/11/2012', 105) and
convert(datetime, EndDate, 105) >= convert(datetime,'11/9/2012', 105)
Also, your query will return all data ranges intersecting given data range (11 Sep 2012 - 16 Nov 2012). Is that what you want to do?
update: If you need records where StartDate and EndDate between 9 Nov 2012 to 16 Nov 2012, you can do this (supposing that StartDate <= EndDate):
select *
from dbo.SubDetails
where
convert(datetime, StartDate, 105) >= convert(datetime,'09/11/2012', 105) and
convert(datetime, EndDate, 105) <= convert(datetime,'16/11/2012', 105)

Related

Sybase - Filter records with current date efficiently

I am trying to filter records based on current date (date part only) against a column "date_sent" which is a DateTime datatype. Though this can be written in multiple ways, I want to know which method will be the most efficient. The table has 11-12 millions of records ant any given time.
Let's say the current date is 16th April 2018
SELECT *
FROM TABLE_NAME
WHERE datepart(YEAR, date_sent) = 2018
AND datepart(MONTH,date_sent) = 4
AND datepart(DAY,date_sent) = 16;
SELECT *
FROM TABLE_NAME
WHERE convert(char(8), date_sent, 112) = convert(char(8), getdate(), 112);
Any other suggestions.
I would start with:
select *
from table_name
where date_sent >= dateadd(day, datediff(day, 0, getdate()), 0) and
date_sent < dateadd(day, 1 + datediff(day, 0, getdate()), 0)
The right hand side removes the time component of the current date. The comparisons should allow Sybase to still us an index on date_sent.
EDIT:
Perhaps Sybase doesn't permit 0 as a date value. You can also do:
select *
from table_name
where date_sent >= dateadd(day, datediff(day, cast('2000-01-01' as date), getdate()), cast('2000-01-01' as date)) and
date_sent < dateadd(day, 1 + datediff(day, cast('2000-01-01' as date), getdate()), cast('2000-01-01' as date))

SQL Server finding end date of the month - month range

In my SQL Server procedure, I have one input parameter like created_date.
Based on that created date I have to find out first and last date of the month.
For example,
If created_date is 12-08-2016,
start_date should be '01-08-2016'
end_date should be '31-08-2016'
If Created_date is 15-06-2016
start_date should be '01-06-2016 00:00:00'
end_date should be '30-06-2016 23:59:59'
Since your input is in DD-MM-YYYY format, so it is need to covert first as MM-DD-YYYY then only it is easy to find the first and last day of the month.
The below query will accept the input and return the result in your expected format:
DECLARE #datevalue AS VARCHAR(20) = '15-06-2016'; --12-08-2016';
SELECT CONVERT(VARCHAR(10), DATEADD(M, DATEDIFF(M, 0, CONVERT(DATETIME, #datevalue, 105)), 0), 105) [start_date],
CONVERT(VARCHAR(10), DATEADD(D, -1, DATEADD(M, DATEDIFF(m, 0, CONVERT(DATETIME, #datevalue, 105)) + 1, 0)), 105) [end_date]
Output:
start_date end_date
----------------------
01-06-2016 30-06-2016
select
DATEADD(Month, DATEDIFF(Month, 0, GETDATE()), 0) as monthfirstdate,
CAST(EOMONTH(GETDATE()) as DATETIME) as monthlastdate
EOMONTH works from SQL server 2012

how can I get last 7 days of data being my date field a number?

I need to get last 7 days data excluding Sunday being my date field a number. How can I do it? Field structure 20140425. For example is I run the statement today it should give me date range between 20140424 - 20140417 excluding 20140420.
The hitch is of course converting the number based date to a real date. This seems to work:
select convert(datetime, convert(char(10), 20140425))
To expand, your query would look like this:
select *
from [Table]
where convert(datetime, convert(char(10), [columnname])) between convert(varchar, getdate() - 8, 101) and convert(varchar, getdate() - 1, 101)
and datepart(DW, convert(datetime, convert(char(10), [columnname]))) <> 1
The convert(varchar, getdate - 1, 101) will return you 12:00am yesterday morning. My first pass didn't include that and would've only given a 6 day range.
SELECT *
FROM Table_Name
WHERE CAST(DateField AS DATE) >= DATEADD(DAY, -7, GETDATE())
AND CAST(DateField AS DATE) <= GETDATE()
AND DATEPART(DW,CAST(DateField AS DATE)) <> 1

SQL Get data in this year

i have a DB with a atribute "Date" which is a string ...
its format is:
"Jan 5 2014 6:26 PM"
and would like to get the number of rows where the date is this year.
already know how to convert the date:
SELECT convert(datetime, 'Oct 23 2012 11:01AM')
i found this code but I do not know how to join the two
select * from datetimes2
where dtm2 >= CAST(CURRENT_TIMESTAMP AS DATE)
and dtm2 < DATEADD(DD, 1, CAST(CURRENT_TIMESTAMP AS DATE))
;
Now I do not know how to do what I want :(
Is this what you looking for ?
DECLARE #datetime DATETIME = 'Jan 5 2014 6:26 PM'
SELECT
*
FROM datetimes2
WHERE [Date] >= CONVERT(DATETIME, CAST(DATEPART(YEAR, #datetime) AS VARCHAR) + '-01-01')
AND [Date] <= CONVERT(DATETIME, CAST(DATEPART(YEAR, #datetime) AS VARCHAR) + '-12-31')
This will get the records for complete year 2014. Hope this helps!
As simple as (assuming SQL server, given convert() )
select * from mytable where year( convert(datetime, date) ) = year( getDate() )
?

SQL query in SQL SERVER 2005 - Comparing Dates

I'm having a hard time doing this query.
I want to compare dates in my query, dates from my DB are in this format:
(MM/DD/YYYY HH:MM:SS AM)
I want to compare this date with tomorrow's day, today plus one.
My questions are:
How do I declare tomorrow's date in sql server?
How would you compare these two dates?
Thank you!! =D
EDIT : DATES in DB are VarChar =S
declare tomorrow's date : DATEADD(dd,1,getdate())
compare dates :
WHERE column >= CONVERT(datetime, CONVERT(varchar, DATEADD(day, 1, GETDATE()), 102))
AND column < CONVERT(datetime, CONVERT(varchar, DATEADD(day, 2, GETDATE()), 102))
Assumes datetime datatype for columns
WHERE
MyCol >= DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()), 1)
AND
MyCol < DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()), 2)
This (yyyy-mm-dd) removes the time component to test of MyCol is tomorrow
2009-10-06 00:00:00 <= MyCol < 2009-10-07 00:00:00
You don't strip time from MyCol: this will affect performance and disallow index usage etc
Efficiency of remove time from datetime question, which is why I used this format and avoid varchar conversions...
Edit:
Avoiding implicit conversions and string matching
10/06/2009 <= MyCol < 10/07/2009
WHERE
MyCol >= CONVERT(char(10), DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()), 1), 101)
AND
MyCol < CONVERT(char(10), DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()), 2), 101)
Of course, it'll fail at the end of December...
I would think your dates are most likely to be in SQL Server's datetime datatype, and that the format you give is just the default string representation.
Typically, I use something like:
SELECT *
FROM tbl
WHERE datecol = CONVERT(datetime, CONVERT(varchar, DATEADD(day, 1, GETDATE()), 101))
However, if your datetimes include a time piece, you need to use something like this:
SELECT *
FROM tbl
WHERE datecol >= CONVERT(datetime, CONVERT(varchar, DATEADD(day, 1, GETDATE()), 101))
AND datecol < CONVERT(datetime, CONVERT(varchar, DATEADD(day, 2, GETDATE()), 101))
There are other date arithmetic tricks you can use. There are plenty here on SO if you look for SQL dates
SQL Server allows you to declare variables
DECLARE #TomorrowsDate DateTime
And you can set it to the current date and time
SET #TomorrowsDate = DATEADD (Day, 0, GETDATE())
For tomorrow's date (without time)
SET #TomorrowsDate = DATEADD (Day, 1, CONVERT (VARCHAR, GETDATE(), 101))
To use it in a query with a column without declaring a variable
SELECT Column1, Column2
FROM YourTableName
WHERE DateColumn BETWEEN DATEADD (Day, 0, CONVERT (VARCHAR, GETDATE(), 101))
AND DATEADD (Day, 1, CONVERT (VARCHAR, GETDATE(), 101))