how to select all with to_char function in sql statement - sql

the code below is my sqlcommand for select statement. Inside have a lots of data including two date data inside.
string sql = "SELECT * FROM TASKMASTER WHERE TASKNAME ='" + TaskName + "'";
can I add in ('DD-MM-YYYY HH24:MI:SS')AS CREATEDATE into the sqlcommand and at the same time will call out all the column in the table?

You could use a table alias.
For example,
SELECT TO_CHAR(t.dt_column, 'DD-MM-YYYY HH24:MI:SS') as CREATEDATE,
t.*
FROM TASKMASTER t
WHERE t.TASKNAME = '" + TaskName + "'"
So, you added your desired column in the beginning of the column list, and also selecting all other columns followed by it.

Related

Type missmatch - Vba from Access Data

I have an error: Type missmatch. I want to get value from Database Access. I want to use Date() function from VBA.
Code:
SELECT TOP 5 Type FROM table Where Date Between #" + Date + "# AND #" + Date - 5 + "# Group By value1, value2 Order By SUM(value3) desc;
Do you have a field called [Date] ? Very bad idea: it's a reserved word.
In the meantime, you can try this way:
SELECT TOP 5 [Type] FROM table
Where [Date] Between Date() and Date()-5
ORDER BY value3 DESC
Your GROUP BY clause does not make sense in this context I think, nor does the SUM() in ORDER BY

Merge two sql queries into one

I've been working on a system service for a client for a while and need some help, I need to merge two sql queries into one. The first part of the query is to look at the master sequence number and count it, after which the query must update a field. I have the two queries below if anyone can help with this problem.
Count query
SELECT master_seq, count(master_seq) as NofH
FROM [ZS_CS_EVO_Integration].[dbo].[CS_Consolidation]
where delivery_date = '2016-07-01'
GROUP BY master_seq
order by master_seq
Update Query
(" UPDATE [dbo].[CS_Consolidation]"
+ " SET [split_dlv] = 1"
+ " FROM [dbo].[CS_Consolidation]"
+ " WHERE"
+ " [master_seq] <> 0 AND CONVERT(DATE,delivery_date) = '" + yesterday + "'", IntConnect);
You can put the first part into CTE, then join and UPDATE:
DECLARE #delivery_date DATE = '2016-07-01'
;WITH cte AS (
SELECT master_seq
FROM [ZS_CS_EVO_Integration].[dbo].[CS_Consolidation]
where delivery_date = #delivery_date and [master_seq] <> 0
GROUP BY master_seq
HAVING count(master_seq) > 1
)
UPDATE c
SET [split_dlv] = 1
FROM [dbo].[CS_Consolidation] c
INNER JOIN cte t
ON t.master_seq = c.master_seq and c.delivery_date = #delivery_date

SQL Select DISTINCT GROUP BY Weekday in Access

I am trying to Count the distinct Number of UserID's in a table for each Weekday (e.g. 545 UserID's on Weekday 1 = Monday, 120 UserID's on Weekday 2 = Tuesday etc.). I am doing this in Access Visual Basic, but the syntax should be universal to SQL. Here is my VB Code:
sSQL = " SELECT Weekday(" & tablename & ".[DATE]) AS WEEKDAY, Count(DISTINCT " & tablename & ".[UserID]) AS User_COUNT"
sSQL = sSQL & " FROM " & tablename
sSQL = sSQL & " GROUP BY Weekday(" & tablename & ".[DATE])"
qdf.SQL = sSQL
The plain SQL Syntax should look like this (edited based on comments & test):
SELECT Weekday(tbl.[Date]) AS WEEKDAY, Count(DISTINCT tbl.[UserID]) AS User_COUNT
FROM tbl
GROUP BY Weekday(tbl.[Date])
..whereas [Date] is a field in tbl formatted as Datetime and [UserID] is a field formatted as Long (with duplicates).
When I try to run the command it tells me "Missing Operator in Query-Syntax.."
Is this a problem of my VB Code or is the SQL Syntax wrong?
MS Access database engine does not support COUNT(DISTINCT ...).
To workaroud it, please see this thread: SQL : how can i count distinct record in MS ACCESS where author suggests to solve issue by using subquery:
SELECT
user_id
, COUNT(*) AS count_distinct_clients
FROM
( SELECT DISTINCT
user_id,
client_id
FROM tbl_sActivity
) AS tmp
GROUP BY
user_id ;
Change the query code to your needs.
[EDIT]
SELECT
wday, COUNT(UserId) AS count_distinct_users
FROM
( SELECT DISTINCT WEEKDAY([Date]) AS wday, UserId
FROM tblName
) AS tmp
GROUP BY
wday;

Why is this statement throwing OleDbException

I am trying to create a paging system and came across a post in SO that included code for achieving this. However, when I run my query, it throws the quoted error. I have double checked code but cannot see error. I am using access 2010 as db. Can someone point out my mistake. Thanks
The SELECT statement includes a reserved word or an argument name that
is misspelled or missing, or the punctuation is incorrect.
Dim Row_Per_Page As Integer = 4
Dim TotRows As Integer = 17
Dim Page_Number As Integer = 2
Dim oledbCmd As OleDbCommand = New OleDbCommand("Select TOP '" & Row_Per_Page & "' *, Count(*) As '" & TotRows & "' From [Select Top('" & TotRows & "' - (('" & Page_Number & "' - 1) * '" & Row_Per_Page & "'))From Postings Order By [Date] DESC] Order By [Date] ASC", oledbCnn)
It looks to me like you have a problem towards the end of your query, specifically this part.
From Postings Order By [Date] DESC] Order By [Date] ASC"
First you have two Order By's, and second I think you forgot a ', [' before the 'DESC]' at the '[Date] DESC]' of your first Order By.
Edit: That link was very helpful. It's hard to read such a long query like the one you posted on SO when it's just one line long.
Forget what I said before. I think the problem is in bold here.
"Select TOP '" & Row_Per_Page & "' , Count() As '" & TotRows & "' From [Select Top('" & TotRows & "' - (('" & Page_Number & "' - 1) * '" & Row_Per_Page & "'))From Postings Order By [Date] DESC] Order By [Date] ASC"
You're Naming your count field 17, and screwing up the code listed at that site. A select Count query should look like this.
SELECT Count(*) AS TOTAL FROM Table1
That would return a field called 'TOTAL' in Table1 that tells you how many records exist there.
Use the below for your query and it should work.
Select TOP Row_Per_Page *, Count(*) AS TOTAL From [Select TOP (TotRows - ((Page_Number - 1) * Row_Per_Page) From Postings Order By ColumnName DESC] Order By ColumnName ASC

Select 1 from dual where exists (select ....). Returns 1 even if the table is completely empty?

I'm using a Select 1 from dual statement to see if new data that comes into my system is actually new or not, if it is new then it's gonna be inserted, if it is not then it's gonna be updated in the database.
sql.CommandText = "select 1 from dual where exists (select * from my table where hour = " + hour + " and zone = '" + zone+ "' and date = TO_DATE('" + mydate + "','DD-MM-YY'))"
The problem however is that after running the statement, it returns the 1 value even if the conditions for it aren't met, even if the table is completely empty. How could this happen?
I'm using VB, .NET framework 3.5 and Oracle 10g.
I would suggest something like
SELECT COUNT(*) AS cnt
FROM mytable m
WHERE m.hour = hour
AND m.zone = zone
AND m.date = TO_DATE( mydate,'DD-MM-YY'))
AND ROWNUM = 1
The result will be either 0 or 1. Since you only care about the existence of the row, the ROWNUM = 1 will cause the query to quit as soon as it finds a matching entry and will prevent you from scanning the whole table.
You can use sign(count(1)) to get the count.
select sign(count(1)) cnt
from my_table
where hour = " + hour + " and zone = '" + zone+ "'
and date = TO_DATE('" + mydate + "','DD-MM-YY'))"