I have an sql DateTime (ms sql server) and want to extract the same date without the seconds: e.g. 2011-11-22 12:14:58.000 to become: 2011-11-22 12:14:00.000 How can I do this? I was thinking to use DATEADD in combination with DATEPART but seems very error prone (besides performance issues) Answer For a solut…
Creating a UDF(User Define Function) if is does not exist and skipping it if it exists
Hi and thanks for reading this. I am trying to use the IF EXISTS/IF NOT EXISTS statement to check if an Object exist. Basically I want to skip it if it is there or create it if it is not there. I …
Selecting by month in PostgreSQL
I want to select rows according to the month of a date or timestamp column like this: But I only get error messages in PostgreSQL. How can this be done? Answer You can use EXTRACT function, like this: Your problem comes from the fact that there is no such thing as Month function in PostgreSQL. Check online do…
Postgres window function and group by exception
I’m trying to put together a query that will retrieve the statistics of a user (profit/loss) as a cumulative result, over a period of time. Here’s the query I have so far: SELECT p.name, e.date, …
Comparing two tables in SQLite
I have two tables and want to compare rows on sqlite like this and I want to produce result like this How is the syntax in sqlite? Thanks Answer
Changing the maximum length of a varchar column?
I’m trying to update the length of a varchar column from 255 characters to 500 without losing the contents. I’ve dropped and re-created tables before but I’ve never been exposed to the alter statement which is what I believe I need to use to do this. I found the documentation here: ALTER TAB…
OPENROWSET – how to read everything as text?
I am using the following command to load data into SQL Server: INSERT INTO [NewTable] SELECT * FROM OPENROWSET ( ‘MSDASQL’, ‘Driver={Microsoft Text Driver (*.txt; *.csv)};DBQ=c:SomeFolder;’ , ‘…
SQL Top 10 Sales Every Month
Greeting all. I have a SQL 2008 express database, lets name is tbl_Merchant, similar as following: Merchant | Sales | Month Comp.1 100 1 Comp.2 230 1 Comp.3 120 1 Comp.1 …
How to activate the hr schema in Oracle 11g
I am learning PL/SQL and am using SQL Developer cause I created many users with hr schema. But when a user logs in to SQL Developer give an error like the users is invalid. but When I use the system user, it works perfectly but without hr schema. So: How can I activate the hr schema in system user or
SQL grammar for SELECT MIN(DATE)
I have a table with structure: id(INT PK), title(VARCHAR), date(DATE) How do I select all distinct titles with their earliest date? Apparently, SELECT DISTINCT title, MIN(date) FROM table doesn’t …