How to correctly put date in SQL form - sql

I know nothing about SQL but am trying to order a memorial keychain for someone who DOES and I'd like to put the date of their pet's passing in SQL form ... Is this correct?
SELECT DATE("2019-11-05");

It depends on the database. In general, date and string constants should use single quotes. The SQL standard for a date literal is:
SELECT DATE '2019-11-05'
However, databases might also prefer:
SELECT DATE('2019-11-05')
SELECT CAST('2019-11-05' as DATE)
Or perhaps something else.

Related

Display the full Month name from string representation of date stored in “Jan/10/2015” format IN SQL

I want to find the month name in the result from the date given as Jan/10/2015 in SQL.
You can try the DATENAME function in SQL
SELECT DATENAME(month, '2017/09/25');
which in this case will return September.
Date/time functions are notoriously databases dependent -- and you haven't specified the database. That said, your value looks like a string and not a date/time value.
Although you should always store date/time values using appropriate types (which is NOT a string), it looks like you can use string manipulation to do what you want. Most databases support left(), so:
select left(datecol, 3)
In those that don't, use substr() or substring().
You can try this using Oracle:
SELECT TO_CHAR(TO_DATE('Jan/10/2015', 'MM/DD/YYYY'), 'MONTH') as MonthName FROM DUAL;

convert SQL string into a date that can be queried

I am pulling from a database using the following:
SELECT
ID, CUSTOMER, NAME, etc, etc, TERM_INDEX
FROM "DAB055.ADT" DAB055
Now I want to limit the date range we pull, so would like to do something like:
WHERE TERM_INDEX >= '01.01.2015'
but the output in that field looks like:
20150629W
How can I convert that into a usual date field within the same statement, so that it can be filtered on?
Thanks.
It looks like your dates are stored in a format that will sort chronologically by using the alphabetical order. You can probably just say WHERE TERM_INDEX >= '20150101'.
Also it shouldn't be difficult to grab the first 8 characters and convert to a date type. Without knowing which platform you're on we'd have to guess at the syntax though.
One of these might work to figure out what your database server is:
select ##version
select * from v$version

Is this query correct? SELECT s FROM Salesmen s WHERE s.dateOfEmployment < '26-06-2012' ORDER BY s.salepersonId ASC

I am not sure how to query for a date and I have tried it without the ' ' but it does not work. I was wonder if this is correct. Dateofemployment is a Date in the database and the date I put there is the date.
SELECT s
FROM Salesmen s
WHERE s.dateOfEmployment < '26-06-2012'
ORDER BY s.salepersonId ASC
Never assume there's a specific date format (dmy,ymd,mdy,etc.), always use the functions of your DBMS for writing dates instead of writing a string literal, e.g. TO_DATE in Oracle (and some others).
In best case you can use Standard SQL's date literals:
WHERE s.dateOfEmployment < DATE '2012-06-26'
Most DBMSes support it and there's no ambiguity because there's only one format allowed: yyyy-mm-dd
Your query is correct and you must put the two quotes since dateOfEmployment is a date. However, be careful with the position of the day and the month and make sure that your query does what you want.
When you leave out the quotes, you have a simple arithmetic expression: 26 - 06 - 2012. This is equal to something like -1992. That is an integer and not a date, so presumably the comparison will either generate an error or at least never return true.
When putting dates in queries, you should use ISO standard format: YYYY-MM-DD. Most databases will accept the following:
WHERE s.dateOfEmployment < '2012-06-26'
In Oracle, you need to add date before the constant.

Select data in date format

I have a query in which I want to select data from a column where the data is a date. The problem is that the data is a mix of text and dates.
This bit of SQL only returns the longest text field:
SELECT MAX(field_value)
Where the date does occur, it is always in the format xx/xx/xxxx
I'm trying to select the most recent date.
I'm using MS SQL.
Can anyone help?
Try this using ISDATE and CONVERT:
SELECT MAX(CONVERT(DateTime, MaybeDate))
FROM (
SELECT MaybeDate
FROM MyTable
WHERE ISDATE(MaybeDate) = 1) T
You could also use MAX(CAST(MaybeDate AS DateTime)). I got in the (maybe bad?) habit of using CONVERT years ago and have stuck with it.
To do this without a conversion error:
select max(case when isdate(col) = 1 then cast(col as date) end) -- or use convert()
from . . .
The SQL statement does not specify the order of operations. So, even including a where clause in a subquery will not guarantee that only dates get converted. In fact, the SQL Server optimizer is "smart" enough to do the conversion when the data is brought in and then do the filtering afterwards.
The only operation that guarantees sequencing of operations is the case statement, and there are even exceptions to that.
Another solution would be using PATINDEX in WHERE clause.
SELECT PATINDEX('[0-9][0-9]/[0-9][0-9]/[0-9][0-9][0-9][0-9]', field_value)
Problem with this approach is you really are not sure if something is date (e.g. 99/99/9999 is not date).
And problem with IS_DATE is it depends on configuration (e.g. DATEFORMAT).
So, use an appropriate option.

split string in sql query

I have a value in field called "postingdate" as string in 2009-11-25, 12:42AM IST format, in a table named "Post".
I need the query to fetch the details based on date range. I tried the following query, but it throws an error. Please guide me to fix this issue. Thanks in advance.
select postingdate
from post
where TO_DATE(postingDate,'YYYY-MM-DD')>61689
and TO_DATE(postingDate,'YYYY-MM-DD')<61691
As you've now seen, trying to perform any sort of query against a string column which represents a date is a problem. You've got a few options:
Convert the postingdate column to some sort of DATE or TIMESTAMP datatype. I think this is your best choice as it will make querying the table using this field faster, more flexible, and less error prone.
Leave postingdate as a string and use functions to convert it back to a date when doing comparisons. This will be a performance problem as most queries will turn into full table scans unless your database supports function-based indexes.
Leave postingdate as a string and compare it against other strings. Not a good choice as it's tough to come up with a way to do ranged queries this way, as I think you've found.
If it was me I'd convert the data. Good luck.
In SQL Server you can say
Select postingdate from post
where postingdate between '6/16/1969' and '6/16/1991'
If it's really a string, you're lucky that it's in YYYY-MM-DD format. You can sort and compare that format as a string, because the most significant numbers are on the left side. For example:
select *
from Posts
where StringDateCol between '2010-01-01' and '2010-01-02'
There's no need to convert the string to a date, comparing in this way is not affected by the , 12:42AM IST appendage. Unless, of course, your table contains dates from a different time zone :)
You will need to convert your string into a date before you run date range queries on it. You may get away with just using the string if your not interested in the time portion.
The actual functions will depend on your RDBMS
for strings only
select * from posts
where LEFT(postingDate,10) > '2010-01-21'
or
for datetime ( Sybase example)
select * from posts
where convert(DateTime,postingDate) between '2010-01-21' and '2010-01-31'