Date Range in RazorSQL - mysql

How to display results with date range?
For example:
I want to show results for sept. 15, 2011 to oct. 20, 2011?
Thanks

This has little to do with RazorSQL and almost everything to do with the data that you're querying. Does the data set that you are querying have a date column that you can key off of? If so, you have two options.
Use a where clause in your SQL query to only bring back data from your source database that is within the range that you want.
Retrieve all of the data in a table and then use RazorSQL's filter feature to whittle down the data.
Of course, from a performance perspective, the first option is the best.

try this hope it will help you
select * from urtable where cast (DATEPART(year, datatimeclm) as varchar(50))+'-'+cast (DATEPART(month, datatimeclm)as varchar(50))+'-'+ cast (DATEPART(day, datatimeclm)as varchar(50)) between '2011-09-15' and '2011-10-29'

For SQL-Server try this
DECLARE #tbl table(dtm datetime)
insert into #tbl
values ('20110914 00:59:00'),
('20110915'),
('20110915 05:10:00'),
('20110916 05:10:00'),
('20111029 05:10:00'),
('20111029'),
('20111030')
SELECT *
FROM #tbl
WHERE dtm>='20110915' AND dtm<'20111030'
To return date without time from a datetime value you can use this CONVERT(datetime,CONVERT(varchar,#date,1),1)
In SQL SERVER 2008 + you can use DATE data type in that case you could use dtm<='20111029'.

Related

Mysql date select from string

i've issue I try this query, do not return any rows. just 0 rows. Even tho there is data matching the request..
select * from repairshop_reservations where date = DATE_FORMAT("11/06/2017 20:00:00", '%d/%m/%Y %H:%i:%s"');
Currently my content of the selected table look like this
The data value of column Date is datetime
you could use str_to_date in this way you can control the proper formatting of the date when you don't use the standard mysql format
select * from repairshop_reservations
where date = str_to_date('11/06/2017 20:00:00', '%d/%m/%Y %H:%i:%s');
You are not inserting a column in your table, so you won't have to define a data type for it. That means that, you are not making changes to the conceptual scheme of your database.
Considering that your table is implemented correctly, the SQL query you would need to give you the desirable result would be:
SELECT * FROM repairshop_reservations
WHERE date = "11/06/2017 20:00:00";
You use the WHERE clause, to filter your record and get an output with a
specified condition. In plain English, what you want to do is:
Select and print for me, every column from the repairshop_reservations table, that has listed date as "11/06/2017 20:00:00"

SQL Server: Want to use between clause with dates, but dates in string form (YYYY.MM.DD)

Help! One column in my database is for dates. All of my dates are unfortunately in the String form (YYYY.MM.DD). I have a MASSIVE database (300+GB) so ideally would like to avoid transformations.
Is there a way I can select rows for dates in between YYYY.MM.DD and YYYY.MM.DD? What would the script look like?
Thank you!
If the months and days are stored with leading zeroes, the BETWEEN operator will work as expected. So will ORDER BY.
create table your_table (
date_value varchar(10) not null
);
insert into your_table values
('2013.01.01'), ('2013.01.13'), ('2013.01.30'), ('2013.01.31'),
('2013.02.01'), ('2013.02.13'), ('2013.02.28'), ('2013.02.31'),
('2013.03.01'), ('2013.03.15'), ('2013.03.30'), ('2013.03.31');
select date_value
from your_table
where date_value between '2013.01.01' and '2013-01-31'
order by date_value;
2013.01.01
2013.01.13
2013.01.30
One of the main problems with your structure is that you lose type safety. Look at this query.
select date_value
from your_table
where date_value between '2013.02.01' and '2013.02.31'
order by date_value;
2013.02.01
2013.02.13
2013.02.28
2013.02.31
If you'd used a column of type date or datetime or timestamp, the dbms would not have allowed inserting the values '2013.02.31', because that's not a value in the domain of date. It is a value in the domain of varchar. (And so is "Arrrrgh!", unless you've got a CHECK constraint on that column that severely restricts the acceptable values.)
Not good solution, but works (cost much performance).
You have formated date in order year, month, day (good order to compare strings, without transformation to datetime), so you can try
SELECT * FROM Table WHERE StringDate > '2013.07.10' AND StringDate < '2013.07.14'
It returns bad results if there are dates before year 1000 without leading zero ('999.07.14').
But I dont know how it works on big database.
SQL Fiddle
Between in SQL is inclusive of both bounds. If that is what you want, you can just use between:
where col between 'YYYY.MM.DD' and 'YYYY.MM.DD'
Where the two constants are whatever values you are looking for.
If you have an index on the column, then between (as well as >, >=, and so on) will use the index. You do not need to transform the values. If your constants are dates of one form or another, then you can use date_format() to create a string in the right format. For instance, to get dates within the past week:
where col >= date_format(adddate(now(), -7), '%Y.%m.%d')

Filter dates stored as varchar in mysql

I am working with a MySQL database where dates are stored as varchar like this:
'2013-01-31' in column cl_223
I need to select only records from 2013 so I tried:
SELECT ..
FROM ....
Where cl_223 Like '2013'
But that does not seem to work.
Thanks for all help!
You must add % as a wildcard :
SELECT ..
FROM ....
WHERE cl_223 LIKE '2013%'
Storing a datettime value in a varchar column complicates some functionality on date time operations. But of course you can select your values writing such a query as follow
SELECT * FROM table_name WHERE cl_223 LIKE '2013%'
But if you don't have any performance issue you can convert the varchar column to a datetime value and write stronger typed query like this:
SELECT * FROM table_name WHERE STR_TO_DATE(cl_223,'%Y-%m-%d2') BETWEEN '2013-01-01' AND '2013-12-31'
But if you need a date time value as a date time in your process you'd better store it in a datetime column instead of a varchar column.
The query should be
SELECT ..
FROM ....
Where cl_223 Like '2013%'
However, the better solution would be to store the dates as DATE data types. If the dates in that column are always used in the format they're in now, the change would be backwards compatible. It would also allow for easier processing of the date values.

How to OrderBy Date stored in a Varchar field in EF4

We have a legacy database (SQLServer 2008) with thousands of rows in it. Each record has a logdate field which is a date but stored as a varchar in the format 21/04/2010 16:40:12.
We only need to return the rows where the logdate is in the future, and order them by date. We could pull back all the rows and filter on the server but this seems wrong and won't scale.
Is there a way of doing the filtering and ordering in Entity Framework 4.
This is what we thought might work but it's failed.
from c in db.changes
where [DateTime]c.logdate > DateTime.Today()
orderby [DateTime]c.logdate
select c;
Any help is appreciated.
You can't parse a string into a date on the DB server with any built-in L2E function.
You can:
map a DB function yourself,
write SQL and execute it with ObjectContext.ExecuteStoreQuery, or
fix the metadata.
I'd pick the latter, if it were me.
I'm not sure you can do it through pure LINQ unless you create your own LINQ functions. If you can execute an ad-hoc query and have EF4 translate it back into objects, like you can on the DataContext.Translate LINQ2SQL method, you can convert it like this:
CONVERT(datetime, logdate, 103)
And thus your query would be:
SELECT
*
FROM
changes
WHERE
CONVERT(datetime, logdate, 103) > GETDATE()
ORDER BY
CONVERT(datetime, logdate, 103)
Alternatively, if you can add to the schema (I assume you can't modify the varchar column to store it as a datetime natively), you could add a computed column like so:
ALTER TABLE
changes
ADD
logdateDatetime AS CONVERT(datetime, logdate, 103) PERSISTED
And then query the logdateDatetime column instead of logdate.
The order in a varchar field will be considerably different than the order in date field. Fix your structure to correctly store dates or add an additional date field that is populated through a trigger. Likely you have bad dates in there as well since there are no controls on a varchar field to diallow dates from being put in. You will need to fix these as well.

Find invalid dates in SQL Server 2008

I have a 300.000 rows table; one of the columns is a varchar() but it really contains a date xx/xx/xxxx or x/x/xxxx or similar. But executing the following test yields an error:
SELECT CAST(MyDate as Datetime) FROM MyTable
The problem is that it doesn’t tell me in which row…
I have executed a series of “manual” updates by trial an error and performed simple updates to fix those, but there’s got to be some weird values that need to either be deleted or fixed.
For example I performed a simple test that fixed about 40 rows:
UPDATE MyTable SET MyDate = REPLACE(MyDate, '/000','/200') FROM MyTable WHERE MyDate like ('%/000%’)
UPDATE MyTable SET MyDate = REPLACE(MyDate, '/190','/199') FROM MyTable WHERE MyDate like ('%/190%’)
This fixed quite a few weird rows that had dates like 01/01/0003 and such. (Dates range from 1998 to 2010).
However, I’d like to know which rows are failing in the above select.
What would be the best way to print those so I can either delete them, edit them or see what to do? Thanks.
SELECT
*
FROM
MyTable
WHERE
ISDATE(MyDate) = 0
From the ISDATE(Transact-SQL) documentation ISDATE(...)
Returns 1 if the expression is a valid datetime value; otherwise, 0.
ISDATE returns 0 if the expression is a datetime2 value.
Did you try the ISDATE function?
Careful with IsDate. I have 1 bad record in a table of thousands. It says 8201-11-30 is a valid date. IsDate should have a YEAR limitation.
ISDATE doesn't seem to be working always.
the following returns 1 in SQL server 2008 R2
Select ISDATE('04- December 20')
But trying to cast the same value to date will fail.
Select cast('04- December 20' as date)
Conversion failed when converting date and/or time from character string.