sql server - Add current date to SQL query followed by number -


i have sql query returns transactions previous day, every morning query runs , exports transactions file previous day. have column called id , in here @ moment 0, insert here date , number forms unique id each transaction in file.. example, first transaction id 27071500001 , last transaction id 27071511234. date need previous day , not current 1 how this?

here select id..

select  id,             paidat    = case                             when not(l_pay.shortname null)    l_pay.shortname                             else ''                         end,             datein    = case                             when not(tr.entrydate null) convert(varchar, tr.entrydate, 112)                             else ''                         end,                                               timein    = case                             when not(tr.entrytime null)  replace(tr.entrytime, ':', '')                             else ''                         end,             dateout   = case                             when not(tr.exitdate null) convert(varchar, tr.exitdate, 112)                             else ''                         end,                                     timeout   = case                             when not(tr.exittime null) replace(tr.exittime, ':', '')                             else ''                         end, 

any appreciated.

you can use convert function different date formats

1) convert(varchar(20), getdate()-1, 112) /* returns date 20150726 */ 2) replace(convert(varchar(20), getdate()-1, 104),'.','') /* returns date 26072015 */ 3) replace(convert(varchar(20), getdate()-1, 104),'.','') +     replace(convert(varchar(20), getdate()-1, 108),':','') /* returns date , time 26072015170032*/ 

update

for have use row_number() function below :

select replace(convert(varchar(20), getdate()-1, 104),'.','') +  cast(row_number() over(order id) varchar(20)) tablename 

to add leading zeros in number need pad string below :

select replace(convert(varchar(20), getdate()-1, 104),'.','') +  right('00000'+ cast(row_number() over(order id) varchar(20)),5) tablename 

Comments

Popular posts from this blog

yii2 - Yii 2 Running a Cron in the basic template -

asp.net - 'System.Web.HttpContext' does not contain a definition for 'GetOwinContext' Mystery -

mercurial graft feature, can it copy? -