I have this query, and in the table/column ttransactionlog_1/occurdatetime, it returns the time value with a .000 at the end, can someone tell me how to remove that from each row? here is a line from the outputfile showing the .000 at the end of the time
0,112213,2021-03-11 14:00:00.000,Santiago,Melody,AdminClock.
Here is the query:
sqlcmd -S clock\punch -d test -U xxx -P xxxxx -Q "Select Distinct TTransactionLog_1.DecisionTimeInterval, TTransactionLog_1.UserID, TTransactionLog_1.OccurDateTime, TTransactionLog_1.lastname, TTransactionLog_1.firstname, TSystemLog1.Name
From TTransactionLog_1 Inner join TSystemLog1 On TTransactionLog_1.NodeID=TSystemLog1.NodeID where TSystemLog1.NodeID = 3 and TTransactionLog_1.OccurDateTime > = dateadd(hh, -1, getdate())
" -s "," -h-1 -W -o "C:\ATR\adminreport.csv"
Thanks in advance!
Does just using Convert on the OccurDateTime value work for you?
Example
declare #OccurDateTime datetime='20210311 14:00:00.000'
select Convert(varchar(19),#OccurDateTime,121)
Related
I'm keen to export a SQL Server query result to an XML file.
I seem to get carriage returns in the resulting file.
I'm wondering what approach I should take to remove the carriage returns from the XML results file?
What I have tried is:
DOS command:
sqlcmd -S HSL-PC0242 -U sa -P PasswordX -i "D:\SQL\auditlog_query1.sql" -C -o "D:\SQL\auditlog_query1_out.xml"
D:\SQL\auditlog_query1.sql contains:
SELECT
A.*
FROM
H2PenguinDev.[dbo].[AuditLog] A
JOIN H2PenguinDev.dbo.ImportProviderProcesses IPP ON IPP.ImportType = 'Z'
AND A.OperatorID = IPP.OperatorID
AND A.AuditTypeID in ( '400','424','425' )
WHERE
A.[PostTime] >= IPP.StartTime
AND A.[PostTime] <= dateadd(second, 90, IPP.StartTime)
FOR XML PATH('Record'), ROOT('AuditLog')
This seems to work.
2Gb output limit tho .. which is fine for this case.
Can open resulting XML in excel ..
and/or use notepad XML plugin and pretty print option to view ..
Note the requirement for ## temp tables rather than single # temp table name.
SELECT A.MyXML
INTO ##AuditLogTempTable
FROM
(SELECT CONVERT(nvarchar(max),
(
SELECT
A.*
FROM
[dbo].[AuditLog] A
JOIN ImportProviderProcesses IPP ON IPP.ImportType = 'Z'
AND A.OperatorID = IPP.OperatorID
AND A.AuditTypeID in ( '400','424','425' )
WHERE
A.[PostTime] >= IPP.StartTime
AND A.[PostTime] <= dateadd(second, 90, IPP.StartTime)
FOR XML PATH('Record'), ROOT('AuditLog')
)
, 0
) AS MyXML
) A
EXEC xp_cmdshell 'bcp "SELECT MyXML FROM ##AuditLogTempTable" queryout "D:\bcptest1.xml" -T -c -t,'
Trying to assign output from SQL query on an object containing special characters to a variable in shell script.
Running directly on the database:
db2 -x 'select count(*) from <SCHEMA>."/BIC/TEST"'
11000
Yet when I include this in script I need to use double quotes as I am using variables passed into the sql. Using single quotes
Output=$(db2 -x 'select count(*) from ${_SCHEMA}."/BIC/TEST"')
echo -e "$Output"
Results in:
SQL20521N Error occurred processing a conditional compilation directive near
"_". Reason code="7". SQLSTATE=428HV
When I use double quotes I hit:
SQL0104N An unexpected token "'/BIC/TEST'" was found following "ount(*)
Tried to escape the double quotes using another set of double quotes:
db2 -x 'select count(*) from ${_SCHEMA}.""/BIC/TEST""'
But this doesn't seem to work in script. It works for tables where there is no special characters/requirement to encase in quotations.
Any help is appreciated.
The code below works fine for me, notice the escaped quotes. If it fails for you, you need to give more details of your DB2-version and the DB2-server operating system platform.
#!/bin/ksh
db2 connect to sample
(($? > 0 )) && print "Failed to connect to database" && exit 1
db2 -o- "drop table \"/bin/test\" "
db2 -v "create table \"/bin/test\"(a integer)"
(($? > 0 )) && print "Create table failed" && exit 1
db2 -v "insert into \"/bin/test\"(a) values(1),(2),(3),(4)"
(($? > 0 )) && print "insert rows failed" && exit 1
db2 -v describe table \"/bin/test\"
typeset -i count_rows=$(db2 -x "select count(*) from \"/bin/test\"" )
(($? > 0 )) && print "query count rows failed" && exit 1
print "\nRow count is: ${count_rows}\n"
db2 -o- connect reset
I am having trouble writing up a batch script. I've narrowed it down to what seems to break and I am providing an example. The following code, when pasted into a console returns 10 rows:
set TESTRUNID=111222
set QUERY="select distinct col1 from Table where col2='%TESTRUNID%' and col3 LIKE '%es'"
start /B /wait sqlcmd -S dahost -U usr -P pwd -Q %QUERY% -o resfile.txt
When I put it in a batch script, it returns 0 rows!
#echo off
setlocal EnableDelayedExpansion
REM remark
REM remark
set TESTRUNID=111222
set QUERY="select distinct col1 from Table where col2='%TESTRUNID%' and col3 LIKE '%es'"
start /B /wait sqlcmd -S dahost -U usr -P pwd -Q %QUERY% -o resfile.txt
I think you are mixing up the use of the percent sign to mean (1) batch variable expansion, and (2) SQL wildcard. Inside a batch file, use a double %% sign for an SQL wildcard:
set QUERY="select distinct col1 from Table where col2='%TESTRUNID%' and col3 LIKE '%%es'"
The double % sign gets translated to a single % sign before it's passed to SQLCMD.
I've struggled with other StackOverflow responses. I would like to save the output of a query to a local text file - it doesn't really matter where the text file is located as long as it is on my local machine.
Code I am using:
\COPY (
select month,count(*) as distinct_Count_month
from
(
select UNIQUE_MEM_ID,to_char(transaction_date, 'YYYY-MM') as month
FROM yi_fourmpanel.card_panel WHERE COBRAND_ID = '10006164'
group by UNIQUE_MEM_ID,to_char(transaction_date, 'YYYY-MM')
) a
group by month) TO 'mycsv.csv' WITH CSV HEADER;
Error with this code is:
<!-- language: none -->
An error occurred when executing the SQL command:
\COPY (
ERROR: syntax error at or near "\"
Position: 1
\COPY (
^
Execution time: 0.08s
(Statement 1 of 2 finished)
An error occurred when executing the SQL command:
select month,count(*) as distinct_Count_month
from
(
select UNIQUE_MEM_ID,to_char(transaction_date, 'YYYY-MM') as month
FROM yi_fourmpanel.card_panel...
ERROR: syntax error at or near ")"
Position: 260
group by month) TO 'mycsv.csv' WITH CSV HEADER
^
Execution time: 0.08s
(Statement 2 of 2 finished)
2 statements failed.
Script execution finished
Total script execution time: 0.16s
1.For server COPY remove \ and run in psql following:
COPY (
WITH data(val1, val2) AS ( VALUES
('v1', 'v2')
)
SELECT *
FROM data
) TO 'yourServerPath/output.csv' CSV HEADER;
cat yourServerPath/output.csv :
val1,val2
v1,v2
2.For client COPY:
psql -h host -U user -d database -c "\copy \
( \
WITH data(val1, val2) AS ( VALUES \
(1, 2) \
) \
SELECT * FROM data) TO 'yourClientPath/output.csv' CSV HEADER;"
cat yourClientPath/output.csv:
val1,val2
1,2
UPDATED
As for example provided, on your client machine in terminal you need to run following script with absolute path to your mycsv.csv :
psql -h host -U username -d db -c "\COPY ( \
select month,count(*) as distinct_Count_month \
from \
( \
select UNIQUE_MEM_ID,to_char(transaction_date, 'YYYY-MM') as month \
FROM yi_fourmpanel.card_panel WHERE COBRAND_ID = '10006164' \
group by UNIQUE_MEM_ID,to_char(transaction_date, 'YYYY-MM') \
) a \
group by month) TO 'path/mycsv.csv' WITH CSV HEADER;"
I am using this;
mysqldump -u userabc -pabc123 dbname |
gzip > /var/backups/archives/mysql/dbname_$(date +\%d-\%m-\%Y_\%T).sql.gz
This works but if the password contains a ^ for example it fails, how can I escape this character and still have mysqldump work with the -p flag;
mysqldump -u userabc -pabc^123 dbname |
gzip > /var/backups/archives/mysql/dbname_$(date +\%d-\%m-\%Y_\%T).sql.gz
quote the password
mysqldump -u fred7 -p'asdf^555^666'
if any of the following * ? [ < > & ; ! | $ ( ) perhaps ^ too